将System.Drawing.Bitmap转换为Direct2D.D2DBitmap
最近在尝试Direct2D编程,挺好玩的。
但是有时候还是会用到GDI+来生成图片,但D2D绘图需要用到自己的D2DBitmap类。
因此需要转换,查阅了下网上的资料,写了这么一个方法:
using System;
using System.Windows.Forms;
using System.Linq;
using System.Text;
using System.Diagnostics;
using DX = SharpDX;
using D2D = SharpDX.Direct2D1;
using WIC = SharpDX.WIC;
using DDW = SharpDX.DirectWrite;
using DXGI = SharpDX.DXGI;
using SharpDX; public D2D.Bitmap ConvertFromSystemBitmap(System.Drawing.Bitmap bmp)
{
System.Drawing.Bitmap desBitmap;//预定义要是使用的bitmap
//如果原始的图像像素格式不是32位带alpha通道
//需要转换为32位带alpha通道的格式
//否则无法和Direct2D的格式对应
if (bmp.PixelFormat != System.Drawing.Imaging.PixelFormat.Format32bppPArgb)
{
desBitmap = new System.Drawing.Bitmap(bmp.Width, bmp.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(desBitmap))
{
g.DrawImage(bmp, , );
}
}
else
{
desBitmap = bmp;
} //直接内存copy会非常快
//如果使用循环逐点转换会非常慢
System.Drawing.Imaging.BitmapData bmpData = desBitmap.LockBits(
new System.Drawing.Rectangle(, , desBitmap.Width, desBitmap.Height),
System.Drawing.Imaging.ImageLockMode.ReadOnly,
desBitmap.PixelFormat
);
int numBytes = bmpData.Stride * desBitmap.Height;
byte[] byteData = new byte[numBytes];
IntPtr ptr = bmpData.Scan0;
System.Runtime.InteropServices.Marshal.Copy(ptr, byteData, , numBytes);
desBitmap.UnlockBits(bmpData); D2D.BitmapProperties bp;
D2D.PixelFormat pixelFormat = new D2D.PixelFormat(DXGI.Format.B8G8R8A8_UNorm, D2D.AlphaMode.Premultiplied); bp = new D2D.BitmapProperties(
pixelFormat,
desBitmap.HorizontalResolution,
desBitmap.VerticalResolution
);
D2D.Bitmap tempBitmap = new D2D.Bitmap(_renderTarget, new Size2(desBitmap.Width, desBitmap.Height), bp);
tempBitmap.CopyFromMemory(byteData, bmpData.Stride); return tempBitmap;
}
PS.这里我用的是SharpDX组件,因为微软的Windows API Code Pack,只更新到1.1,2010年后就不维护了
将System.Drawing.Bitmap转换为Direct2D.D2DBitmap的更多相关文章
- 类库探源——System.Drawing.Bitmap
一.System.Drawing.Bitmap Bitmap 类: 封装GDI+ 位图,此位图由图形图像及其属性的像素数据组成.Bitmap 是用于处理由像素定义的图像的对象 命名空间: System ...
- 让System.Drawing.Bitmap可以在linux运行
.net core的bitmap使用的是以下类库,但无法在linux运行 https://github.com/CoreCompat/CoreCompat 在linux运行需要安装runtime.li ...
- .net学习笔记----利用System.Drawing.Image类进行图片相关操作
C#中对图片的操作主要是通过System.Drawing.Image等类进行. 一.将图片转换为字节流 /// <summary> /// 图片处理帮助类 /// </summary ...
- System.Drawing.image 与ImageSource 互转
private BitmapSource bs(Bitmap bt) { IntPtr ip = bt.GetHbitmap(); BitmapSource bitmapSource = System ...
- .Net Core 之 图形验证码 本文介绍.Net Core下用第三方ZKWeb.System.Drawing实现验证码功能。
本文介绍.Net Core下用第三方ZKWeb.System.Drawing实现验证码功能. 通过测试的系统: Windows 8.1 64bit Ubuntu Server 16.04 LTS 64 ...
- 类库探源——System.Drawing
一.System.Drawing 命名空间简述 System.Drawing 命名空间提供访问 GDI+ 的基本功能,更高级的功能在 System.Drawing.Drawing2D,System.D ...
- [译]如何在.NET Core中使用System.Drawing?
你大概知道System.Drawing,它是一个执行图形相关任务的流行的API,同时它也不属于.NET Core的一部分.最初是把.NET Core作为云端框架设计的,它不包含非云端相关API.另一方 ...
- Asp.Net Core使用System.Drawing.Common部署到docker报错问题
Asp.Net Core 2.1发布后,正式支持System.Drawing.Common绘图了,可以用来做一些图片验证码之类的功能.但是把网站部署到docker容器里运行会遇到很多问题,也是非常闹心 ...
- System.Drawing.Image.cs
ylbtech-System.Drawing.Image.cs 1.程序集 System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyTok ...
随机推荐
- 浅谈React和VDom关系
组件化 组件的封装 组件的复用 组件的封装 视图 数据 视图和数据之间的变化逻辑 import React, {Component} from 'react'; export default clas ...
- hdu_5187_zhx's contest
Problem Description As one of the most powerful brushes, zhx is required to give his juniors n probl ...
- sublime text3配置python开发环境(windows版)
安装阶段: sublime text3的安装: 下载网址:https://www.sublimetext.com/ 下载完成后 ,点击安装即可. 安装Package Control: 点击 Tools ...
- angular2或angular4中使用ckplayer播放rtmp和m3u8视频直播流
1. 下载ckpalyer整个包并导入, 将ckplayer放到src/assets/下 2. 引入ckplayer.js angular2中,在angular-cli.json中找到script,添 ...
- IE8 如何 不显示 选项 卡,直接在任务显示 各个页面?
IE8 如何 不显示 选项 卡,直接在任务显示 各个页面? 在 工具->Internet 选项(o) ->常规--选项卡-设置->把第一个勾去掉 “启用选项卡浏览(需要重新启动 ...
- 集合之WeakHashMap
WeakHashMap 底层数据结构是哈希表结构 依赖于键的数据结构特点 不同于HashMap,该类的键是以弱键的形式存在 当该键成为垃圾对象,会被垃圾回收期空闲的时候回收,那么改键所对应值也会被回收 ...
- BZOJ1066_蜥蜴_KEY
题目传送门 经过长时间的旅行,很长时间没写过博客了,这次把上次WA的题目过了. 由于每次蜥蜴从石柱上跳下时,石柱的高度会-1,可以看做占了一格的流量. 建图: 1.建超级源和超级汇,设超级源连到每只蜥 ...
- CF 314 E. Sereja and Squares
E. Sereja and Squares http://codeforces.com/contest/314/problem/E 题意: 给你一个擦去了部分左括号和全部右括号的括号序列,括号有25种 ...
- LeetCode: 62. Unique Paths(Medium)
1. 原题链接 https://leetcode.com/problems/unique-paths/description/ 2. 题目要求 给定一个m*n的棋盘,从左上角的格子开始移动,每次只能向 ...
- nodejs 文件系统
nodejs访问文件系统 所有的文件系统的调用,都需要加载fs模块,即var fs=require('fs'); nodejs提供的fs模块几乎所有的功能都有两种形式选择:异步和同步,如异步的wr ...