【发布时间】: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();
}
}
}
}
【问题讨论】: