【问题标题】:Insufficient buffer size using WriteableBitmap?使用 WriteableBitmap 的缓冲区大小不足?
【发布时间】:2014-11-18 16:57:29
【问题描述】:

我正在修改 ColorBasic Kinect 示例,以便显示叠加到视频流的图像。所以我所做的是加载一个具有透明背景的图像(现在是一个 GIF,但它可能会改变),并写入显示的位图。

我得到的错误是我正在写入的缓冲区太小。

我看不到实际的错误是什么(我是 XAML/C#/Kinect 的新手),但 WriteableBitmap 是 1920x1080,而我要复制的位图是 200x200,那么为什么会出现此错误?我看不出透明背景有什么害处,但我开始怀疑...

请注意,如果没有最后一个 WritePixels,代码可以正常工作,并且我会看到网络摄像头的输出。我的代码如下。

叠加图:

public BitmapImage overlay = new BitmapImage(new Uri("C:\\users\\user\\desktop\\something.gif"));

显示 Kinect 网络摄像头的回调函数(请参阅默认示例 ColorBasic),并进行了非常小的修改:

private void Reader_ColorFrameArrived(object sender, ColorFrameArrivedEventArgs e)
{
    // ColorFrame is IDisposable
    using (ColorFrame colorFrame = e.FrameReference.AcquireFrame())
    {
        if (colorFrame != null)
        {
            FrameDescription colorFrameDescription = colorFrame.FrameDescription;

            using (KinectBuffer colorBuffer = colorFrame.LockRawImageBuffer())
            {
                this.colorBitmap.Lock();

                // verify data and write the new color frame data to the display bitmap
                if ((colorFrameDescription.Width == this.colorBitmap.PixelWidth) && (colorFrameDescription.Height == this.colorBitmap.PixelHeight))
                {
                    colorFrame.CopyConvertedFrameDataToIntPtr(
                        this.colorBitmap.BackBuffer,
                        (uint)(colorFrameDescription.Width * colorFrameDescription.Height * 4),
                        ColorImageFormat.Bgra);

                    this.colorBitmap.AddDirtyRect(new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight));
                }

                if(this.overlay != null)
                {
                    // Calculate stride of source
                    int stride = overlay.PixelWidth * (overlay.Format.BitsPerPixel / 8);

                    // Create data array to hold source pixel data
                    byte[] data = new byte[stride * overlay.PixelHeight];

                    // Copy source image pixels to the data array
                    overlay.CopyPixels(data, stride, 0);

                    this.colorBitmap.WritePixels(new Int32Rect(0, 0, overlay.PixelWidth, overlay.PixelHeight), data, stride, 0);
                }

                this.colorBitmap.Unlock();
            }
        }
    }
}

【问题讨论】:

    标签: c# wpf xaml bitmap kinect


    【解决方案1】:

    您的 overlay.Format.BitsPerPixel / 8 将为 1(因为它是 gif),但您试图将其复制到不是 gif 的东西,可能是 BGRA(32 位)。因此,您的尺寸有很大差异 (4x)。


    .WritePixels 应该接受目标缓冲区的步幅值,但是你超过了覆盖的步幅值(这也会导致奇怪的问题)。


    最后,即使它 100% 平滑,您的叠加层实际上也不会“叠加”任何东西,它会替换——因为我在您的代码中看不到任何 alpha 弯曲数学。

    将您的 .gif 转换为 .png(32 位),看看是否有帮助。

    另外,如果你正在寻找一个 AlphaBltMerge 类型的代码:我在这里写了整个东西。它很容易理解。


    Merge 2 - 32bit Images with Alpha Channels

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-07
      • 1970-01-01
      • 2020-09-05
      • 1970-01-01
      • 1970-01-01
      • 2012-06-16
      • 2014-06-10
      • 2015-06-05
      相关资源
      最近更新 更多