Capturing wpf and webcam image using aforge

I am creating a wpf application and embedding a webcam in my project. This is how I tried to capture images from a usb webcam.

public partial class CameraWindow : Window
{
    VideoCaptureDevice LocalWebCam;
    public FilterInfoCollection LocalWebCamsCollection;

    private BitmapImage latestFrame;
    Action<BitmapImage> captureImage;

    void Cam_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {
        try
        {
            System.Drawing.Image img = (Bitmap)eventArgs.Frame.Clone();
            MemoryStream ms = new MemoryStream();
            img.Save(ms, ImageFormat.Bmp);
            ms.Seek(0, SeekOrigin.Begin);
            BitmapImage bi = new BitmapImage();
            bi.BeginInit();
            bi.StreamSource = ms;
            bi.EndInit();
            bi.Freeze();
            this.latestFrame = bi;
            Dispatcher.BeginInvoke(new ThreadStart(delegate
            {
                previewWindow.Source = bi;
            }));
        }
        catch (Exception ex)
        {
        }
    }


    public CameraWindow(Window window)
    {
        this.Owner = window;            
        InitializeComponent();

        Loaded += CameraWindow_Loaded;
        Unloaded += CameraWindow_Unloaded;
    }

    private void CameraWindow_Loaded(object sender, RoutedEventArgs e)
    {
        LocalWebCamsCollection = new FilterInfoCollection(FilterCategory.VideoInputDevice);
        LocalWebCam = new VideoCaptureDevice(LocalWebCamsCollection[0].MonikerString);
        LocalWebCam.VideoResolution = LocalWebCam.VideoCapabilities[0];
        LocalWebCam.NewFrame += new NewFrameEventHandler(Cam_NewFrame);

        LocalWebCam.Start();
    }

    private void CameraWindow_Unloaded(object sender, RoutedEventArgs e)
    {
        LocalWebCam.Stop();
    }

    private void manualCapture_Click(object sender, RoutedEventArgs e)
    {
        if (captureImage != null)
        {
            captureImage(latestFrame);
        }
    }
}

      

XAML:

<Grid>  
<!--<ComboBox x:Name="camListCb" Margin="10,25,10,415" Height="26"></ComboBox>-->
    <Image x:Name="previewWindow" Margin="10,10,10,40"></Image>
    <Button x:Name="manualCapture" Height="26" Width="40" Content="CAP" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="5" Click="manualCapture_Click"></Button>
    <Label x:Name="testLabelAsd" Content="" HorizontalAlignment="Left" Margin="80,438,0,0" VerticalAlignment="Top"/>
</Grid>

      

What I want to do next is save the captured images to c: \ tmp and show the number of captured images shortcuts. How could I do this? Any hepls?

+3


source to share


1 answer


Keeping the problem with captured images solved by converting BitmapImage

to Bitmap

:

private Bitmap BitmapImageToBitmap(BitmapImage bitmapImage)
        {
            using (MemoryStream outStream = new MemoryStream())
            {
                BitmapEncoder enc = new BmpBitmapEncoder();
                enc.Frames.Add(BitmapFrame.Create(bitmapImage));
                enc.Save(outStream);
                System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(outStream);

                return new Bitmap(bitmap);
            }
        }

      



then adding the method BitmapImageToBitmap

tomanualCapture_Click

    private void manualCapture_Click(object sender, RoutedEventArgs e)
    {
        if (captureImage != null)
        {
            captureImage(latestFrame);
        }
        Bitmap bm = BitmapImageToBitmap(latestFrame);
        bm.Save(@"C:\tmp\test.jpg", ImageFormat.Jpeg);
    }

      

+1


source







All Articles