将WriteableBitmap转为byte[]

本文介绍了一个用于将Win8 Metro中WriteableBitmap转换为byte[]的方法。此函数适用于24位RGB格式的图像,并通过遍历像素缓冲区实现转换。

Win8 metro中的操作与之前的版本有所不同,因此作为一个新手,我将自己的一些问题解答记录了下来,希望与大家分享!!

下面是将WriteableBitmap转为byte[]的函数(默认的图像为24位RGB格式<wbr>):</wbr>

private byte[] WriteableBitmapToBytes(WriteableBitmap src)

{

Stream temp = src.PixelBuffer.AsStream();

byte[] tempBytes = new byte[src.PixelWidth * src.PixelHeight * 3];

for (int i = 0; i < tempBytes.Length; i++)

{

temp.Seek(i, SeekOrigin.Begin);

temp.Write(tempBytes, 0, tempBytes.Length);

}

temp.Dispose();

return tempBytes;

}

本文所写的所有程序都已成功实践,若有错误请大家留言更正,谢谢指教,共勉!!

### WriteableBitmap 的定义与用法 #### 1. WriteableBitmap 的定义 WriteableBitmap 是 WPF(Windows Presentation Foundation)中用于支持对像素写入以更改渲染的图片对象[^1]。它允许开发者直接操作位图中的像素数据,从而实现高性能的 CPU 渲染。WriteableBitmap 提供了对图像像素的直接访问能力,适用于需要动态生成或修改图像的应用场景。 #### 2. WriteableBitmap 的主要特性 WriteableBitmap 的核心特性在于其可变性,这意味着可以获取其单个像素并对其进行任意操作[^5]。这种灵活性使得 WriteableBitmap 成为处理动态图像的理想选择,例如实时绘制、动画生成或图像处理等任务。 #### 3. 使用 WriteableBitmap 的基本方法 在 WPF 中,WriteableBitmap 可以通过以下方式创建和使用: - **创建 WriteableBitmap 实例** WriteableBitmap 的构造函数接受宽度、高度以及可选的像素格式参数。以下是一个简单的创建示例[^2]: ```csharp System.Windows.Media.Imaging.WriteableBitmap writableBitmap = new System.Windows.Media.Imaging.WriteableBitmap(800, 600, 96, 96, PixelFormats.Bgra32, null); ``` - **绑定到 UI 元素** WriteableBitmap 通常绑定到 `Image` 控件的 `Source` 属性,以便在界面上显示: ```csharp Image imageControl = new Image(); imageControl.Source = writableBitmap; ``` - **修改像素数据** 使用 `Lock()` 和 `Unlock()` 方法锁定和解锁位图以进行像素数据修改。以下是修改像素的一个示例: ```csharp writableBitmap.Lock(); int stride = (writableBitmap.PixelWidth * writableBitmap.Format.BitsPerPixel + 7) / 8; byte[] pixels = new byte[writableBitmap.PixelHeight * stride]; // 修改像素数据 for (int y = 0; y < writableBitmap.PixelHeight; y++) { for (int x = 0; x < writableBitmap.PixelWidth; x++) { int index = y * stride + x * 4; pixels[index] = 255; // B pixels[index + 1] = 0; // G pixels[index + 2] = 0; // R pixels[index + 3] = 255; // A } } writableBitmap.WritePixels(new Int32Rect(0, 0, writableBitmap.PixelWidth, writableBitmap.PixelHeight), pixels, stride, 0); writableBitmap.Unlock(); ``` - **线程安全注意事项** 在多线程环境中使用 WriteableBitmap 时需要注意线程安全问题。如果在后台线程创建 WriteableBitmap 并尝试将其传递给主线程,可能会导致主线程被锁定[^4]。因此,建议在主线程中创建 WriteableBitmap,并仅在必要时通过冻结机制(如 `Freeze()` 方法)将图像数据传递给其他线程。 #### 4. 示例:使用 WriteableBitmap 绘制文字 以下代码展示了如何在 WriteableBitmap 上绘制文字[^2]: ```csharp System.Windows.Media.Imaging.WriteableBitmap writableBitmap = new System.Windows.Media.Imaging.WriteableBitmap(800, 600, 96, 96, PixelFormats.Bgra32, null); // 创建绘图上下文 System.Windows.Media.DrawingVisual visual = new System.Windows.Media.DrawingVisual(); using (System.Windows.Media.DrawingContext dc = visual.RenderOpen()) { dc.Clear(System.Windows.Media.Colors.White); dc.DrawText(new System.Windows.Media.FormattedText( "Hello, WriteableBitmap!", System.Globalization.CultureInfo.CurrentCulture, System.Windows.FlowDirection.LeftToRight, new System.Windows.Media.Typeface("Arial"), 48, System.Windows.Media.Brushes.Black), new System.Windows.Point(50, 50)); } // 将视觉内容渲染到 WriteableBitmap System.Windows.Media.RenderTargetBitmap rtb = new System.Windows.Media.RenderTargetBitmap(800, 600, 96, 96, PixelFormats.Pbgra32); rtb.Render(visual); writableBitmap = new System.Windows.Media.Imaging.WriteableBitmap(rtb); ``` ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值