Search code examples
c#wpfaforgeimage-capture

wpf capture and image from webcam using aforge


I'm creating wpf application and implementing a webcam to my project. Here is how I tried to capture images from 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 captured images to c:\tmp and show in a label amount of the captured images. How could I do that? Any hepls?


Solution

  • Saving captured images problem 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 BitmapImageToBitmap method to manualCapture_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);
    }