将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 ...
随机推荐
- C++ Primer学习笔记_1_快速入门
C++快速入门 一 编写简单程序 // main是操作系统唯一显示调用的函数int main() {/**return返回的值是一个状态指示器 0:成功 非0:返回错误给OS*以echo $?命令可以 ...
- c++11线程创建的三种方法
一.用一个初始函数创建一个线程 直接看代码:注意c++在运行一个可执行程序的时候(创建了一个进程),会自动的创建一个主线程,这个主线程和进程同生共死,主线程结束,进程也就结束了. #include & ...
- FreeImage 生成带透明通道的GIF
主要方法: 加载图像及读取参数 FreeImage_Load FreeImage_GetWidth FreeImage_GetHeight FreeImage_Allocate FreeImage_G ...
- Yii2 联表查询数据丢失,即出现主键覆盖情况的解决方法
前段时间做项目,遇到一个问题,用yii2的AR连表查询数据的时候,理应该查出来更多的数据,但是实际得到的只有部分数据: 例如,有这么一个查询: $query = OperaHotelRoom::fin ...
- js中回调函数写法
第一种方式 function studyEnglish(who){ document.write(who+"学习英语</br>"); } function study( ...
- operator.attrgetter() 进行对象排序
## 使用operator.attrgetter() 进行对象排序 from operator import attrgetter class Student: def __init__(self, ...
- python中函数参数的引用方式
值传递和引用传递时C++中的概念,在python中函数参数的传递是变量指向的对象的物理内存地址!!! python不允许程序员选择采用传值还是传引用.Python参数传递采用的肯定是“传对象引用”的方 ...
- QP-nano结构分析
QP-nano是QP的一个裁剪版本,是一个通用的.可移植的.超轻量级的事件驱动型框架.适用于像8051.PIC.AVR.MSP430.68HC01/11/12.R8C/Tiny等资源受限的8位和16位 ...
- C语言程序设计:现代方法(第2版)第二章全部习题答案
前言 本人在通过<C语言程序设计:现代方法(第2版)>自学C语言时,发现国内并没有该书完整的课后习题答案,所以就想把自己在学习过程中所做出的答案分享出来,以供大家参考.这些答案是本人自己解 ...
- (数据科学学习手札40)tensorflow实现LSTM时间序列预测
一.简介 上一篇中我们较为详细地铺垫了关于RNN及其变种LSTM的一些基本知识,也提到了LSTM在时间序列预测上优越的性能,本篇就将对如何利用tensorflow,在实际时间序列预测任务中搭建模型来完 ...