I'm trying to paint images on a PictureBox
from a camera that I insert with a WindowsFormsHost
. The code is as simple as follows:
<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>
And I paint from the camera like this:
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();
}
This code works perfectly.
But when I change the <Window>
tags to <UserControl>
(as I want it to be embeded on my own UserControl
), it doesn't paint!!
Why isn't it painting on a UserControl
?
Why I'm not using Image
from WPF?
Image
has a threading use really confusing, and whenever I try to paint on the Image
, the Bitmap
was disposed before.