C产品在市场上有哪些独特优势?

更新于
2026-07-30 12:47:53
18阅读来源:SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1167个文字,预计阅读时间需要5分钟。

C产品在市场上有哪些独特优势?

目录前言

一、WriteableBitmap是什么?

二、如何实现

1.创建WriteableBitmap

2.写入数据

3.完整代码

4.使用示例

1.直接转换

2.复用写入

总结前言在WPF中,我们有时需要截屏或获取鼠标位置,这时可以使用WriteableBitmap来实现。本文将介绍WriteableBitmap的概念、实现方法以及使用示例。

一、WriteableBitmap是什么?WriteableBitmap是WPF中用于像素操作的一个类,它允许开发者直接访问和修改像素数据。

二、如何实现

1.创建WriteableBitmap

csharpvar bitmap=new WriteableBitmap(width, height);

2.写入数据

csharpbitmap.WritePixels(new Int32Rect(0, 0, width, height), pixels, width * 4, 0);

3.完整代码

csharpprivate WriteableBitmap CreateWriteableBitmap(int width, int height){ return new WriteableBitmap(width, height);}

private void WriteDataToBitmap(WriteableBitmap bitmap, int[] pixels, int width, int height){ bitmap.WritePixels(new Int32Rect(0, 0, width, height), pixels, width * 4, 0);}

4.使用示例

1.直接转换

csharpvar source=new BitmapImage(new Uri(path/to/image.png));var bitmap=new WriteableBitmap(source);

2.复用写入

csharpvar bitmap=new WriteableBitmap(width, height);var pixels=new int[width * height];// ...获取像素数据WriteDataToBitmap(bitmap, pixels, width, height);

总结本文介绍了WPF中WriteableBitmap的概念、实现方法以及使用示例,帮助开发者更好地理解和使用这一功能。

目录
  • 前言
  • 一、WriteableBitmap是什么?
  • 二、如何实现
    • 1.创建WriteableBitmap
    • 2.写入数据
  • 三、完整代码
    • 四、使用示例
      • 1.直接转换
      • 2.复用写入
    • 总结

      前言

      在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产品在市场上有哪些独特优势?

      到此这篇关于C# wpf Bitmap转换成WriteableBitmap的方法的文章就介绍到这了,更多相关C# Bitmap转换成WriteableBitmap内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!

      本文共计1167个文字,预计阅读时间需要5分钟。

      C产品在市场上有哪些独特优势?

      目录前言

      一、WriteableBitmap是什么?

      二、如何实现

      1.创建WriteableBitmap

      2.写入数据

      3.完整代码

      4.使用示例

      1.直接转换

      2.复用写入

      总结前言在WPF中,我们有时需要截屏或获取鼠标位置,这时可以使用WriteableBitmap来实现。本文将介绍WriteableBitmap的概念、实现方法以及使用示例。

      一、WriteableBitmap是什么?WriteableBitmap是WPF中用于像素操作的一个类,它允许开发者直接访问和修改像素数据。

      二、如何实现

      1.创建WriteableBitmap

      csharpvar bitmap=new WriteableBitmap(width, height);

      2.写入数据

      csharpbitmap.WritePixels(new Int32Rect(0, 0, width, height), pixels, width * 4, 0);

      3.完整代码

      csharpprivate WriteableBitmap CreateWriteableBitmap(int width, int height){ return new WriteableBitmap(width, height);}

      private void WriteDataToBitmap(WriteableBitmap bitmap, int[] pixels, int width, int height){ bitmap.WritePixels(new Int32Rect(0, 0, width, height), pixels, width * 4, 0);}

      4.使用示例

      1.直接转换

      csharpvar source=new BitmapImage(new Uri(path/to/image.png));var bitmap=new WriteableBitmap(source);

      2.复用写入

      csharpvar bitmap=new WriteableBitmap(width, height);var pixels=new int[width * height];// ...获取像素数据WriteDataToBitmap(bitmap, pixels, width, height);

      总结本文介绍了WPF中WriteableBitmap的概念、实现方法以及使用示例,帮助开发者更好地理解和使用这一功能。

      目录
      • 前言
      • 一、WriteableBitmap是什么?
      • 二、如何实现
        • 1.创建WriteableBitmap
        • 2.写入数据
      • 三、完整代码
        • 四、使用示例
          • 1.直接转换
          • 2.复用写入
        • 总结

          前言

          在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产品在市场上有哪些独特优势?

          到此这篇关于C# wpf Bitmap转换成WriteableBitmap的方法的文章就介绍到这了,更多相关C# Bitmap转换成WriteableBitmap内容请搜索自由互联以前的文章或继续浏览下面的相关文章希望大家以后多多支持自由互联!