我正在尝试使用PictureBox
从我插入的相机在WindowsFormsHost
上绘制图像。代码简单如下:
<Window x:Class="videoTEst.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="583" Width="1132"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms">
<StackPanel Background="LightBlue" Height="513" Width="1077">
<StackPanel HorizontalAlignment="Center" Width="450" Orientation="Horizontal">
<Button Name="ButtonOpen" Content="Load" Click="ButtonOpen_OnClick" />
<Button Name="ButtonLoadImage" Content="Load image" Click="ButtonLoadImage_OnClick" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<WindowsFormsHost Name="WinFormsHost" Height="440" Width="690" HorizontalAlignment="Left">
<wf:PictureBox x:Name="PictureBoxvideo" SizeMode="Normal" Paint="PictureBoxvideo_Paint"></wf:PictureBox>
</WindowsFormsHost>
<ListBox Name="ListBoxLog" />
</StackPanel>
</StackPanel>
我从相机中画出这样的:
private void asyncVideoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
Image temp = PictureBoxvideo.Image;
Bitmap bitmap = eventArgs.Frame;
PictureBoxvideo.Image = new Bitmap(bitmap);
if (temp != null) temp.Dispose();
bitmap.Dispose();
}
此代码完美无缺。
但是,当我将<Window>
标签更改为<UserControl>
时(因为我希望将其嵌入到我自己的UserControl
中),它就不会绘制!!
为什么不在UserControl
上绘画?
为什么我没有使用WPF中的Image
?
Image
的线程使用确实让人感到困惑,每当我尝试在Image
上绘画时,Bitmap
就被处理掉了。
答案 0 :(得分:4)
而不是WindowsFormsHost中的PictureBox,您只需使用WPF图像控件:
<StackPanel Orientation="Horizontal">
<Image x:Name="image" Height="440" Width="690" HorizontalAlignment="Left"/>
...
</StackPanel>
现在,您将使用this answer或this answer中所示的代码从Bitmap对象创建BitmapImage,并对Freeze
进行额外调用,以便在UI线程中访问它:
Bitmap bitmap = eventArgs.Frame; // your NewFrameEventArgs
BitmapImage bitmapImage = new BitmapImage();
using (var stream = new MemoryStream())
{
bitmap.Save(stream, ImageFormat.Bmp);
stream.Seek(0, SeekOrigin.Begin);
bitmapImage.BeginInit();
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.StreamSource = stream;
bitmapImage.EndInit();
}
bitmapImage.Freeze();
最后,您将通过调用其Dispatcher:
来更新UI线程中的Image控件image.Dispatcher.Invoke((Action)(() => image.Source = bitmapImage));