最近在尝试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的更多相关文章

  1. 类库探源——System.Drawing.Bitmap

    一.System.Drawing.Bitmap Bitmap 类: 封装GDI+ 位图,此位图由图形图像及其属性的像素数据组成.Bitmap 是用于处理由像素定义的图像的对象 命名空间: System ...

  2. 让System.Drawing.Bitmap可以在linux运行

    .net core的bitmap使用的是以下类库,但无法在linux运行 https://github.com/CoreCompat/CoreCompat 在linux运行需要安装runtime.li ...

  3. .net学习笔记----利用System.Drawing.Image类进行图片相关操作

    C#中对图片的操作主要是通过System.Drawing.Image等类进行. 一.将图片转换为字节流 /// <summary> /// 图片处理帮助类 /// </summary ...

  4. System.Drawing.image 与ImageSource 互转

    private BitmapSource bs(Bitmap bt) { IntPtr ip = bt.GetHbitmap(); BitmapSource bitmapSource = System ...

  5. .Net Core 之 图形验证码 本文介绍.Net Core下用第三方ZKWeb.System.Drawing实现验证码功能。

    本文介绍.Net Core下用第三方ZKWeb.System.Drawing实现验证码功能. 通过测试的系统: Windows 8.1 64bit Ubuntu Server 16.04 LTS 64 ...

  6. 类库探源——System.Drawing

    一.System.Drawing 命名空间简述 System.Drawing 命名空间提供访问 GDI+ 的基本功能,更高级的功能在 System.Drawing.Drawing2D,System.D ...

  7. [译]如何在.NET Core中使用System.Drawing?

    你大概知道System.Drawing,它是一个执行图形相关任务的流行的API,同时它也不属于.NET Core的一部分.最初是把.NET Core作为云端框架设计的,它不包含非云端相关API.另一方 ...

  8. Asp.Net Core使用System.Drawing.Common部署到docker报错问题

    Asp.Net Core 2.1发布后,正式支持System.Drawing.Common绘图了,可以用来做一些图片验证码之类的功能.但是把网站部署到docker容器里运行会遇到很多问题,也是非常闹心 ...

  9. System.Drawing.Image.cs

    ylbtech-System.Drawing.Image.cs 1.程序集 System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyTok ...

随机推荐

  1. JSP Cookie 处理

    Cookie是存储在客户机的文本文件,它们保存了大量轨迹信息.在servlet技术基础上,JSP显然能够提供对HTTP cookie的支持. 通常有三个步骤来识别回头客: 服务器脚本发送一系列cook ...

  2. 03 Oracle分区表

    Oracle分区表   先说句题外话…   欢迎成都天府软件园的小伙伴来面基交流经验~ 一:什么是分区(Partition)? 分区是将一个表或索引物理地分解为多个更小.更可管理的部分. 分区对应用透 ...

  3. Xcode 之 Debug 和 Release 模式切换方式 - iOS

    一.选择项目工程,其次选择 Edit Scheme 二.在弹框中选择 Run => Info => Build Configuration 选择中进行 Debug 和 Release 的切 ...

  4. Rem实现自适应布局

    rem布局的目的是为了让我们可以用同一份代码,适应不同的移动终端(rem:就是css单位) 1.项目入口html文件<meta name="viewport" content ...

  5. mount/umount命令

    挂载及卸载指定的文件系统 mount [选项] [-L<标签>] [-o<选项>] [-t<文件系统类型>] [设备名] [挂载点] umount [挂载点] -a ...

  6. 支付宝H5、APP支付服务端的区别(php)

    php支付宝H5和APP支付1.准备工作需要前往 蚂蚁金服开放平台申请https://openhome.alipay.com/developmentDocument.htm 2.大致流程1.用户添加商 ...

  7. zkfc的znode不存在的问题

    cd /soft/hadoop/logs/hadoop-centos-zkfc-s101.log发现: 2018-09-29 12:42:03,616 FATAL org.apache.hadoop. ...

  8. HyperLedger Fabric 1.4 多机多节点部署(10.3)

    多机多节点指在多台电脑上部署多个组织和节点,本案例部署一个排序(orderer)服务,两个组织(org1,org2)和四个节点(peer),每个组织包括两个节点,需要五台计算机,计算机配置如下: 多机 ...

  9. mysql 长连接断开问题

    从MySQL 5.0.3开始,默认情况下禁止再连接,这是5.0.13中的新选项,提供了一种以显式方式设置再连接行为的方法. mysql应用程序建立的长连接,大约过8小时会断开[没测过,网上都是这么说的 ...

  10. 分布式专题(二)——Zookeeper的ZAB协议介绍