【发布时间】:2011-02-09 09:42:37
【问题描述】:
我正在使用 WPF 3.5。
我在 backgroundworker_DoWork 事件中有 while 循环,它将不断从 DSLR 流式传输图像。
最初,流媒体图像将显示在 PictureBox 中
<Grid>
<WindowsFormsHost VerticalAlignment="Top" Background="Transparent" Height="500">
<wf:PictureBox x:Name="picLiveView" />
</WindowsFormsHost>
</Grid>
下面是代码:
while (!liveViewExit)
{
try
{
if (picImage != null)
lock (mylock)
{
this.picLiveView.Image = (Image)picImage.Clone();
}
}
catch
{
}
}
这很好用。
但是,当我尝试将 PictureBox 更改为 WPF 图像控件时,将 BitmapImage 分配给 WPF 图像控件时出现此错误:
{“调用线程无法访问此对象,因为不同的线程拥有它。”}
MemoryStream ms = new MemoryStream();
bmp.Save(ms, ImageFormat.Png);
ms.Position = 0;
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = ms;
bi.EndInit();
try
{
if (bi != null)
{
this.imageBox.Source = bi;
}
}
catch
{
}
为什么 .NET 2 PictureBox 控件有效,而 .NET 3.5 WPF Image 控件无效?
我试过这段代码:
BackgroundWorker bg = new BackgroundWorker();
Dispatcher disp = Dispatcher.CurrentDispatcher;
bg.DoWork += (sender, e) =>
{
// load your data
disp.Invoke(new Action(/* a method or lambda that do the assignment */));
}
bg.RunWorkerCompleted += anotherMethodOrLambda; // optional
bg.RunWorkerAsync(/*an argument object that will be visible in e.Argument*/);
它没有任何错误,但图像没有刷新。 while 循环使应用程序没有响应。
【问题讨论】:
标签: wpf picturebox