使用可写位图的缓冲区大小不足



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

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

我看不到实际错误是什么(我是 XAML/C#/Kinect 的完全新手),但可写位图是 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();
            }
        }
    }
}

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


.WritePixels 应该采用目标缓冲区的步幅值,但您通过它跳过覆盖的步幅值(这也可能导致奇怪的问题)。


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

将.gif切换到.png(32位),看看是否有帮助。

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


将 2 - 32 位图像与 Alpha 通道合并

最新更新