C# wpf Bitmap转换成WriteableBitmap的方法
作者:CodeOfCC
本文主要介绍了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, src.PixelFormat); wb.WritePixels(new Int32Rect(srcRect.X, srcRect.Y, srcRect.Width, srcRect.Height), data.Scan0, data.Height * data.Stride, data.Stride, destinationX, destinationY); bitmap.UnlockBits(data);
三、完整代码
//将Bitmap 转换成WriteableBitmap public static WriteableBitmap BitmapToWriteableBitmap(System.Drawing.Bitmap src) { var wb = CreateCompatibleWriteableBitmap(src); System.Drawing.Imaging.PixelFormat format = src.PixelFormat; if (wb == null) { wb = new WriteableBitmap(src.Width, src.Height, 0, 0, System.Windows.Media.PixelFormats.Bgra32, null); format = System.Drawing.Imaging.PixelFormat.Format32bppArgb; } BitmapCopyToWriteableBitmap(src, wb, new System.Drawing.Rectangle(0, 0, src.Width, src.Height), 0, 0, format); return wb; } //创建尺寸和格式与Bitmap兼容的WriteableBitmap public static WriteableBitmap CreateCompatibleWriteableBitmap(System.Drawing.Bitmap src) { System.Windows.Media.PixelFormat format; switch (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(src.Width, src.Height, 0, 0, format, null); } //将Bitmap数据写入WriteableBitmap中 public static void BitmapCopyToWriteableBitmap(System.Drawing.Bitmap src, WriteableBitmap dst, System.Drawing.Rectangle srcRect, int destinationX, int destinationY, System.Drawing.Imaging.PixelFormat srcPixelFormat) { var data = src.LockBits(new System.Drawing.Rectangle(new System.Drawing.Point(0, 0), src.Size), System.Drawing.Imaging.ImageLockMode.ReadOnly, srcPixelFormat); dst.WritePixels(new Int32Rect(srcRect.X, srcRect.Y, srcRect.Width, srcRect.Height), data.Scan0, data.Height * data.Stride, data.Stride, destinationX, destinationY); 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内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!