C# wpf Bitmap转换成WriteableBitmap的方法
前言
在wpf中我们有时候需要截屏或者获取鼠标标时通常拿到的是Bitmap,如果要作显示,则需要将Bitmap转成wpf控件兼容的图像对象,比如转成BitmapSource在网上已经可以查到实现方法,这里提供一种将Bitmap转成WriteableBitmap的方法。
一、WriteableBitmap是什么?
WriteableBitmap是一个wpf对象,在命名空间System.Windows.Media.Imaging中,是BitmapSource的子类。如下图所示:
二、如何实现
1.创建WriteableBitmap
创建一个与Bitmap大小相同,像素格式兼容的WriteableBitmap。
示例如下:
WriteableBitmap wb = new WriteableBitmap(bitmap.Width, bitmap.Height, 0, 0, System.Windows.Media.PixelFormats.Bgra32, null);
2.写入数据
调用Bitmap的LockBits获取其内部的图像数据,通过WritePixels的方式写入WriteableBitmap,这样即完成了转换。
示例如下:
var data = bitmap.LockBits(new System.Drawing.Rectangle(new System.Drawing.Point(0, 0), bitmap.Size), System.Drawing.Imaging.ImageLockMode.ReadOnly, class="lazy" data-src.PixelFormat);
wb.WritePixels(new Int32Rect(class="lazy" data-srcRect.X, class="lazy" data-srcRect.Y, class="lazy" data-srcRect.Width, class="lazy" data-srcRect.Height), data.Scan0, data.Height * data.Stride, data.Stride, destinationX, destinationY);
bitmap.UnlockBits(data);
三、完整代码
//将Bitmap 转换成WriteableBitmap
public static WriteableBitmap BitmapToWriteableBitmap(System.Drawing.Bitmap class="lazy" data-src)
{
var wb = CreateCompatibleWriteableBitmap(class="lazy" data-src);
System.Drawing.Imaging.PixelFormat format = class="lazy" data-src.PixelFormat;
if (wb == null)
{
wb = new WriteableBitmap(class="lazy" data-src.Width, class="lazy" data-src.Height, 0, 0, System.Windows.Media.PixelFormats.Bgra32, null);
format = System.Drawing.Imaging.PixelFormat.Format32bppArgb;
}
BitmapCopyToWriteableBitmap(class="lazy" data-src, wb, new System.Drawing.Rectangle(0, 0, class="lazy" data-src.Width, class="lazy" data-src.Height), 0, 0, format);
return wb;
}
//创建尺寸和格式与Bitmap兼容的WriteableBitmap
public static WriteableBitmap CreateCompatibleWriteableBitmap(System.Drawing.Bitmap class="lazy" data-src)
{
System.Windows.Media.PixelFormat format;
switch (class="lazy" data-src.PixelFormat)
{
case System.Drawing.Imaging.PixelFormat.Format16bppRgb555:
format = System.Windows.Media.PixelFormats.Bgr555;
break;
case System.Drawing.Imaging.PixelFormat.Format16bppRgb565:
format = System.Windows.Media.PixelFormats.Bgr565;
break;
case System.Drawing.Imaging.PixelFormat.Format24bppRgb:
format = System.Windows.Media.PixelFormats.Bgr24;
break;
case System.Drawing.Imaging.PixelFormat.Format32bppRgb:
format = System.Windows.Media.PixelFormats.Bgr32;
break;
case System.Drawing.Imaging.PixelFormat.Format32bppPArgb:
format = System.Windows.Media.PixelFormats.Pbgra32;
break;
case System.Drawing.Imaging.PixelFormat.Format32bppArgb:
format = System.Windows.Media.PixelFormats.Bgra32;
break;
default:
return null;
}
return new WriteableBitmap(class="lazy" data-src.Width, class="lazy" data-src.Height, 0, 0, format, null);
}
//将Bitmap数据写入WriteableBitmap中
public static void BitmapCopyToWriteableBitmap(System.Drawing.Bitmap class="lazy" data-src, WriteableBitmap dst, System.Drawing.Rectangle class="lazy" data-srcRect, int destinationX, int destinationY, System.Drawing.Imaging.PixelFormat class="lazy" data-srcPixelFormat)
{
var data = class="lazy" data-src.LockBits(new System.Drawing.Rectangle(new System.Drawing.Point(0, 0), class="lazy" data-src.Size), System.Drawing.Imaging.ImageLockMode.ReadOnly, class="lazy" data-srcPixelFormat);
dst.WritePixels(new Int32Rect(class="lazy" data-srcRect.X, class="lazy" data-srcRect.Y, class="lazy" data-srcRect.Width, class="lazy" data-srcRect.Height), data.Scan0, data.Height * data.Stride, data.Stride, destinationX, destinationY);
class="lazy" data-src.UnlockBits(data);
}
四、使用示例
1.直接转换
//创建一个Bitmap对象
var bitmap = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
//绘制Bitmap
//略
//绘制Bitmap--end
//将Bitmap转换为WriteableBitmap
var wb=BitmapToWriteableBitmap(bitmap);
2.复用写入
//创建一个Bitmap对象
var bitmap = new System.Drawing.Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
//创建格式兼容的WriteableBitmap
var wb = CreateCompatibleWriteableBitmap(bitmap);
System.Drawing.Imaging.PixelFormat format = bitmap .PixelFormat;
if (wb == null)
//格式不兼容则强制使用bgr32
{
wb = new WriteableBitmap(bitmap .Width, bitmap .Height, 0, 0, System.Windows.Media.PixelFormats.Bgra32, null);
format = System.Drawing.Imaging.PixelFormat.Format32bppArgb;
}
while (true)
{
//绘制Bitmap
//略
//绘制Bitmap--end
//将Bitmap数据写入WriteableBitmap
BitmapCopyToWriteableBitmap(bitmap, wb, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), 0, 0, format);
}
总结
以上就是今天要讲的内容,本质上就是对图像数据的直接拷贝,刚好Bitmap有获取内部数据的接口,而WriteableBitmap也刚好有写入数据的接口。这种方法避免了新的句柄产生,不会出现内存泄漏。而且直接的数据拷贝,不需要编解码,性能是非常好的。
到此这篇关于C# wpf Bitmap转换成WriteableBitmap的方法的文章就介绍到这了,更多相关C# Bitmap转换成WriteableBitmap内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341