当前位置: 首页 > news >正文

完整教程:WPF WriteableBitmap 高性能双缓冲图片显示方案

完整教程:WPF WriteableBitmap 高性能双缓冲图片显示方案

WPF WriteableBitmap 高性能双缓冲图片显示方案

在实际的WPF应用开发中,我们经常需要处理实时图像显示需求,如视频流、摄像头画面、动态图表等。传统的图像显示方式在高频更新场景下往往存在性能瓶颈。本文将介绍如何使用WriteableBitmap实现高性能的双缓冲图片显示方案。

传统方式的性能问题

在介绍优化方案前,我们先看下常见的传统实现方式:

using (Bitmap bitmap = e.Bitmap.CreateDrawingBitmap())
{
var hBitmap = bitmap.GetHbitmap();
try
{
ImageSource imageSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
hBitmap,
IntPtr.Zero,
System.Windows.Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
imageControl.Source = imageSource;
}
finally
{
DeleteObject(hBitmap);
}
}

这种方式存在几个问题:

  • 每帧都创建新对象,增加GC压力
  • 频繁的跨平台调用(P/Invoke)
  • 可能产生图像撕裂和闪烁
  • 内存使用效率低下

双缓冲方案的优势

双缓冲技术通过使用两个缓冲区来解决这些问题:

  • 前台缓冲区:用于当前显示
  • 后台缓冲区:用于准备下一帧图像
  • 通过交换缓冲区实现平滑更新

完整实现方案

1. 类级别变量定义

private WriteableBitmap _frontBuffer;
private WriteableBitmap _backBuffer;
private readonly object _bufferLock = new object();
private int _bitmapWidth;
private int _bitmapHeight;
private PixelFormat _pixelFormat;
private BitmapPalette _palette;

2. 初始化方法

public void InitializeBuffers(int width, int height, PixelFormat pixelFormat, BitmapPalette palette = null)
{
_bitmapWidth = width;
_bitmapHeight = height;
_pixelFormat = pixelFormat;
_palette = palette;
// 创建前后缓冲区
_frontBuffer = new WriteableBitmap(
width, height, 96, 96, pixelFormat, palette);
_backBuffer = new WriteableBitmap(
width, height, 96, 96, pixelFormat, palette);
// 一次性设置UI绑定
imageControl.Source = _frontBuffer;
}

3. 核心更新方法

public void UpdateImage(Bitmap bitmap)
{
if (_frontBuffer == null ||
bitmap.Width != _bitmapWidth ||
bitmap.Height != _bitmapHeight)
{
// 重新初始化缓冲区(尺寸变化时)
var (pixelFormat, palette) = GetWpfPixelFormatAndPalette(bitmap);
InitializeBuffers(bitmap.Width, bitmap.Height, pixelFormat, palette);
}
lock (_bufferLock)
{
// 将Bitmap数据复制到后台缓冲区
CopyBitmapToWriteableBitmap(bitmap, _backBuffer);
// 交换缓冲区数据
SwapBufferData(_frontBuffer, _backBuffer);
}
}

4. 缓冲区数据交换

private unsafe void SwapBufferData(WriteableBitmap target, WriteableBitmap source)
{
target.Lock();
source.Lock();
try
{
// 交换后台缓冲区指针
IntPtr tempBuffer = target.BackBuffer;
target.BackBuffer = source.BackBuffer;
source.BackBuffer = tempBuffer;
// 标记整个区域为已更新
target.AddDirtyRect(new Int32Rect(0, 0, target.PixelWidth, target.PixelHeight));
}
finally
{
source.Unlock();
target.Unlock();
}
}

5. 内存复制实现

private unsafe void CopyBitmapToWriteableBitmap(Bitmap bitmap, WriteableBitmap writeableBitmap)
{
var bitmapData = bitmap.LockBits(
new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
ImageLockMode.ReadOnly,
bitmap.PixelFormat);
try
{
writeableBitmap.Lock();
try
{
int sourceStride = bitmapData.Stride;
int destStride = writeableBitmap.BackBufferStride;
int height = bitmapData.Height;
int minStride = Math.Min(sourceStride, destStride);
byte* sourcePtr = (byte*)bitmapData.Scan0;
byte* destPtr = (byte*)writeableBitmap.BackBuffer;
// 逐行复制,确保正确处理不同步长
for (int y = 0; y < height; y++)
{
Buffer.MemoryCopy(
sourcePtr + y * sourceStride,
destPtr + y * destStride,
minStride,
minStride);
}
}
finally
{
writeableBitmap.Unlock();
}
}
finally
{
bitmap.UnlockBits(bitmapData);
}
}

6. 像素格式转换辅助方法

public static (PixelFormat wpfPixelFormat, BitmapPalette palette) GetWpfPixelFormatAndPalette(Bitmap bitmap)
{
BitmapPalette palette = null;
if (bitmap.PixelFormat.HasFlag(System.Drawing.Imaging.PixelFormat.Indexed))
{
try
{
var colorPalette = bitmap.Palette;
var colors = colorPalette.Entries.Select(e =>
Color.FromArgb(e.A, e.R, e.G, e.B)).ToArray();
palette = new BitmapPalette(colors);
}
catch
{
palette = BitmapPalettes.WebPalette;
}
}
switch (bitmap.PixelFormat)
{
case System.Drawing.Imaging.PixelFormat.Format24bppRgb:
return (PixelFormats.Bgr24, null);
case System.Drawing.Imaging.PixelFormat.Format32bppArgb:
return (PixelFormats.Bgra32, null);
// 其他格式处理...
default:
return (PixelFormats.Bgr32, null);
}
}

使用示例

// 在图像帧到达事件中处理
private void OnFrameReceived(object sender, FrameEventArgs e)
{
using (Bitmap bitmap = e.Bitmap.CreateDrawingBitmap())
{
UpdateImage(bitmap);
}
}
// 初始化示例
private void InitializeCamera()
{
// 假设已知初始图像尺寸
InitializeBuffers(640, 480, PixelFormats.Bgr24);
}

性能优化要点

  1. 内存重用:避免频繁分配和释放内存
  2. 减少跨线程调用:最小化Dispatcher的使用
  3. 直接内存操作:使用unsafe代码进行高效内存复制
  4. 双缓冲设计:消除图像撕裂和闪烁
  5. 适当的锁定机制:确保线程安全

注意事项

  1. 启用不安全代码:在项目属性中启用"允许不安全代码"
  2. 异常处理:确保所有Lock操作都有对应的Unlock
  3. 资源释放:及时释放Bitmap资源
  4. 尺寸变化处理:处理图像尺寸变化的情况

总结

通过使用WriteableBitmap和双缓冲技术,我们能够显著提升WPF应用程序中图像显示的效率。这种方案特别适用于需要高频更新图像的场景,如视频监控、实时数据可视化等应用。

关键优势包括:

  • 大幅减少GC压力
  • 消除图像撕裂现象
  • 提高渲染性能
  • 更流畅的用户体验

希望本文对你在WPF高性能图像处理方面有所帮助!

http://www.wxhsa.cn/company.asp?id=757

相关文章:

  • PHP 性能优化实战 OPcache + FPM 极限优化配置
  • 多校 3 - 1001. 求和
  • cache的基本原理
  • 【办公自动化】如何使用Python脚本自动化处理音频?
  • 如何用 vxe-table 实现2个树表格可以互相拖拽数据
  • CSP 初赛必背
  • 最新安卓版16音轨简谱编辑器软件使用说明
  • 【URP】Unity超分辨率优化实践
  • 0125_命令模式(Command)
  • 通过 GitHub 仓库下载微信 Mac Windows 历史版本(Rodert 提供)
  • CSP 初赛整理
  • 使用GoLang执行Shellcode的技术解析
  • 【GitHub每日速递】想提升技术?这 些开源项目涵盖编程、服务器管理,别错过
  • cidr Not Available
  • 读人形机器人08制造行业
  • 现代Web应用渗透测试:JWT攻击实战指南
  • 分享10 个百度资源网盘搜索的网站大全
  • RST报文段的意义
  • Delphi TStringGrid控件学习笔记
  • 你可能不需要WebSocket-服务器发送事件的简单力量
  • JS 定时器 点击简书 button 加载更多 控制台触发
  • Oops! internal error 1341 occurred.
  • navicat查看mysql数据库大小
  • MyNetty Normal 规格池化内存分配在高并发场景的应用探讨
  • mongodb 慢查询模拟
  • Java第一次实验
  • HCIP回顾— BGP经典实验详解
  • 逆波兰表达式求值+滑动窗口最大值
  • 84. 柱状图中最大的矩形
  • 前k个高频元素