我有一个更新WriteableBitmap(FrameDataNew)内容的方法,它是我的viewmodel(VM)中的一个属性:
public WriteableBitmap FrameDataNew
{
get { return frameDataNew; }
set
{
frameDataNew = value;
OnProptertyChanged("FrameDataNew");
}
}
private WriteableBitmap frameDataNew = null;
当我从gstreamer类中收到一个新的Bitmap时,我写了更新FrameDataNew以便在屏幕上显示最新的帧。该窗口是一个简单的Image控件,其源代码绑定到FrameDataNew。
以下代码可以在我的事件处理程序中执行此操作:
/// <summary>
/// BitmapCaptured event handler for when a new video frame is received from the IP source
/// </summary>
/// <param name="sender">The originating source of the event</param>
/// <param name="NewBitmap">The new frame in Bitmap form</param>
private void PipeLine_BitmapCaptured(object sender, Bitmap NewBitmap)
{
// render to the screen
Dispatcher.Invoke(() =>
{
// check the existence of the WriteableBitmap and also the dimensions, create a new one if required
if ((VM.FrameDataNew == null) || (VM.FrameDataNew.Width != NewBitmap.Width) || (VM.FrameDataNew.Height != NewBitmap.Height))
VM.FrameDataNew = new WriteableBitmap(NewBitmap.Width, NewBitmap.Height, NewBitmap.HorizontalResolution, NewBitmap.VerticalResolution, PixelFormats.Bgr24, null);
// lock the bitmap data so we can use it
BitmapData data = NewBitmap.LockBits(new Rectangle(0, 0, NewBitmap.Width, NewBitmap.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
// lock the backbuffer of the WritebaleBitmap so we can modify it
VM.FrameDataNew.Lock();
// Copy the bitmap's data directly to the on-screen buffers
CopyMemory(VM.FrameDataNew.BackBuffer, data.Scan0, (data.Stride * data.Height));
// Moves the back buffer to the front.
VM.FrameDataNew.AddDirtyRect(new Int32Rect(0, 0, data.Width, data.Height));
// unlock the back buffer again
VM.FrameDataNew.Unlock();
//
//NewBitmap.UnlockBits(data);
});
}
[DllImport("Kernel32.dll", EntryPoint = "RtlMoveMemory")]
public static extern void CopyMemory(IntPtr Destination, IntPtr Source, int Length);
现在我想更新我的程序来处理多个管道和WriteableBitmaps,这样我就可以显示几个视频源。我做的第一件事就是创建一个静态实用程序类,这样我就可以传入我想要更新的新位图(NewBitmap)和WriteableBitmap(VM.FrameDataNew)。然后我在新帧到达时按要求调用:
实用程序类(只是与我的问题有关的代码):
public static class Utils
{
/// <summary>
/// Inject the source Bitmap into the Destination WriteableBitmap
/// </summary>
/// <param name="Src">The source Bitmap</param>
/// <param name="Dest">The destination WriteableBitmap</param>
public static void InjectBitmap(Bitmap Src, WriteableBitmap Dest)
{
if ((Dest == null) || (Dest.Width != Src.Width) || (Dest.Height != Src.Height))
Dest = new WriteableBitmap(Src.Width, Src.Height, Src.HorizontalResolution, Src.VerticalResolution, PixelFormats.Bgr24, null);
BitmapData data = Src.LockBits(new Rectangle(0, 0, Src.Width, Src.Height), ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
Dest.Lock();
// Copy the bitmap's data directly to the on-screen buffers
CopyMemory(Dest.BackBuffer, data.Scan0, (data.Stride * data.Height));
// Moves the back buffer to the front.
Dest.AddDirtyRect(new Int32Rect(0, 0, data.Width, data.Height));
Dest.Unlock();
//
Src.UnlockBits(data);
}
[DllImport("Kernel32.dll", EntryPoint = "RtlMoveMemory")]
public static extern void CopyMemory(IntPtr Destination, IntPtr Source, int Length);
}
......以及我称之为的方式:
/// <summary>
/// BitmapCaptured event handler for when a new video frame is received from the IP source
/// </summary>
/// <param name="sender">The originating source of the event</param>
/// <param name="NewBitmap">The new frame in Bitmap form</param>
private void PipeLine_BitmapCaptured(object sender, Bitmap NewBitmap)
{
// render to the screen
Dispatcher.Invoke(() =>
{
Utils.InjectBitmap(NewBitmap, VM.FrameDataNew);
});
}
图像不再出现在屏幕上。单步调试代码,每次调用InjectBitmap()时,目标WriteableBitmap都为null?代码在第一个if语句中创建一个新的,但VM.FrameDataNew仍为null?
我绝对处于经验的边缘,所以任何帮助都会非常感激。
编辑:目的是拥有一个可观察的WriteableBitmaps集合,以便我可以有效地处理其中的几个。
答案 0 :(得分:1)
原因很简单:您不会将新创建的对象分配给FrameDataNew
属性,因此其值仍为null
。
不要忘记在C#中引用类型实例是通过引用传递的。
因此,在您的方法中,您要执行以下操作:
void InjectBitmap(Bitmap Src, WriteableBitmap Dest)
{
if (Dest == null)
Dest = new WriteableBitmap(/* ... */);
}
但Dest
只是方法内部的局部变量 - 方法的参数可以看作是方法的局部变量。
因此,您将一个新创建的实例分配给一个局部变量,该变量当然会在方法返回时丢失。
这里需要的是ref
参数:
void InjectBitmap(Bitmap src, ref WriteableBitmap dest)
{
if (dest == null)
dest = new WriteableBitmap(/* ... */);
}
请注意,我更改了参数名称大小写以匹配C#样式指南。
但是,这对于属性不起作用,因此如果InjectBitmap(NewBitmap, VM.FrameDataNew)
是属性,FrameDataNew
将无法编译。
您可以将FrameDataNew
设为字段,但拥有非私有可变字段是一个坏主意。
你可以使用一个临时的局部变量,但它有些丑陋:
var copy = VM.FrameDataNew;
InjectBitmap(NewBitmap, ref copy);
if (copy != VM.FrameDataNew)
VM.FrameDataNew = copy;
我会重新考虑你的设计,以便你不需要所有这些东西。
顺便说一句,在多线程环境中多次调用Dispatcher.Invoke
(特别是如果调用的方法很慢,而你的方法很慢)会降低应用程序的性能并使UI无响应。