最近在尝试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. centos7 使用指定邮箱发送邮件

    一.安装sendmail与mail .安装sendmail: ) centos下可以安装命令:yum -y install sendmail ) 安装完后启动sendmail命令:service se ...

  2. 06.升级git版本及命令学习

    博客为日常工作学习积累总结: 1.升级git版本: 参考博客:https://blog.csdn.net/yuexiahunone/article/details/78647565由于新的版本可以使用 ...

  3. python和java,php,c,c#,c++的对比

    1.C语言,它既有高级语言的特点,又具有汇编语言的特点,它是结构式语言.C语言应用指针:可以直接进行靠近硬件的操作,但是C的指针操作不做保护,也给它带来了很多不安全的因素.C++在这方面做了改进,在保 ...

  4. Blender2.79建模快捷键

    快捷键 基本操作 滚动鼠标中键滚轮:视图放大或缩小 按住鼠标中键滚轮:视图旋转 单独鼠标右键:选择物体 单独鼠标右键:放置物体 shift+鼠标中键:视图平移 小键盘数字1:前视图:ctrl+1:后视 ...

  5. 手搓一个C语言简单计算器。

    #include <stdio.h> void xing(int shu); void biaoti(int kong,char * title); void zhuyemian(char ...

  6. vim 智能提示插件 YouCompleteMe安装

    按照网上的教程安装该软件,没有一篇是成功的(也有可能是没找对).自己从网上东拼西凑的,终于让自己的vim智能识别了. 1. 升级 vim: (ubuntu) sudo add-apt-reposito ...

  7. 156. Merge Intervals【LintCode by java】

    Description Given a collection of intervals, merge all overlapping intervals. Example Given interval ...

  8. 20145209刘一阳 《网络对抗》逆向及BOF基础实践

    直接修改程序机器指令,改变程序执行流程 在正式开始实践之前,先对pwn1文件做个备份,以便修改后可以及时恢复到初始状态: 使用指令objdump -d 20145209 | more对目标文件进行反汇 ...

  9. 【Hadoop】Seondary NameNode不是备份NameNode!!

    昨天和舍友聊天时无意中提起Secondary NameNode,他说这是备用NameNode.我当时就有点疑惑..之后查阅了相关资料和博客,算是基本理解了什么是Secondary NameNode. ...

  10. OKVIS(一)初始化流程及代码结构

    OKVIS代码结构: okvis_apps: your own app okvis_ceres: backend main code, estimator, error term;  okvis_co ...