【问题标题】:Problem with WriteableBitmap on big images大图像上的 WriteableBitmap 问题
【发布时间】:2020-05-05 03:12:45
【问题描述】:

在程序中,我需要在图像上快速绘制。为此,我选择了 WriteableBitmap,并使用 BackBuffer 执行必要的操作。并且一切都被安全地绘制,只要图像大小不超过 1500 像素区域内的某个限制。超过这个尺寸,就会出现其他东西,而不是所需的形状。

writeable.Lock();
IntPtr buff = writeable.BackBuffer;
unsafe
{
    byte* pbuff = (byte*)buff.ToPointer();
    ...
}
writeable.AddDirtyRect(new Int32Rect(0, 0, (int)writeable.Width, (int)writeable.Height));
writeable.Unlock();

At the top of an incorrect result of drawing down approximately as it should look (round brush).

如果我们不更新整个图像,而只更新您正在更改的区域,则结果与更新整个图像相同。问题是什么?这是 BackBuffer 的功能还是对 WriteableBitmap 大小的其他限制?如果我们需要对 15-30 兆像素的图像进行非常快速的渲染,该怎么办?请不要考虑 Direct2D 选项。

【问题讨论】:

  • 我将 WriteableBitmap 用于 20 MP 或更大的超大图像,没有任何问题。但也许不要使用 (int)Width 和 (int)Height。 WriteableBitmap 具有额外的 PixelWidth 和 PixelHeight 属性。
  • 您能否提供一个代码 sn-p,说明您实际写入缓冲区的方式?
  • 这可以在我下面的回答中看到。
  • @Quergo 顺便问一下,你有什么反馈吗? stackoverflow.com/q/61416524/1136211

标签: c# wpf


【解决方案1】:

基于post,我通过改变BackBuffer的访问方式解决了这个问题。

不适用于大图像的代码:

writeable.Lock();
IntPtr buff = writeable.BackBuffer;
unsafe
{
    byte* pbuff = (byte*)buff.ToPointer();

    pbuff[Index * 3 + 0] = (byte)(B);
    pbuff[Index * 3 + 1] = (byte)(G);
    pbuff[Index * 3 + 2] = (byte)(R);
}
writeable.AddDirtyRect(new Int32Rect(0, 0, writeable.PixelWidth, writeable.PixelHeight));
writeable.Unlock();

工作代码:

writeable.Lock();
unsafe
{
    long pBackBuffer = (long)writeable.BackBuffer;
    int stride = writeable.BackBufferStride;

    int offset = (int)(RelativeY * stride + RelativeX * 3);
    long pBackBufferWithOffset = pBackBuffer + offset;
    *((byte*)pBackBufferWithOffset + 0) = (byte)(B);
    *((byte*)pBackBufferWithOffset + 1) = (byte)(G);
    *((byte*)pBackBufferWithOffset + 2) = (byte)(R);
}
writeable.AddDirtyRect(new Int32Rect(0, 0, writeable.PixelWidth, writeable.PixelHeight));
writeable.Unlock();

writeable.PixelWidth OR (int)writeable.Width - 没关系。

重要的是使用这个 - long,而不是这个 - int

【讨论】:

  • "writeable.PixelWidth OR (int)writeable.Width - 无关紧要" - 只要它有 96 DPI。如果您处理像素,总是使用 PixelWidth/-Height。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多