使用 aforge 从网络摄像头捕获和图像的 wpf

wpf capture and image from webcam using aforge

我正在创建 wpf 应用程序并为我的项目实现网络摄像头。这是我尝试从 USB 网络摄像头捕获图像的方法。

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>

接下来我要做的是将截取的图片保存到c:\tmp,并在标签中显示截取的图片数量。我怎么能那样做?有帮助吗?

通过将 BitmapImage 转换为 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);
            }
        }

然后将 BitmapImageToBitmap 方法添加到 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);
    }