用于通过单击按钮从网络摄像头捕获图像序列的 c# 代码

c# code for capturing image sequences from a web cam with button click

我是 WPF 的初学者,我实现了一个 c# 代码来生成从 WPF 中的网络摄像头捕获的图像。到目前为止,我已经做到了,目前它可以通过单击按钮获取单个图像。但我想拍摄拍摄的图像序列(每张图像与前一张图像略有不同,具体取决于拍摄的 seconds/ms 数量)。在 javascript 中实现了相同的功能,但我了解到 javascript 不能在 WPF 应用程序中使用。

这是我到目前为止完成的代码片段

在XAML中:

<Border  Grid.Column="1" Grid.Row="1" BorderThickness="3" CornerRadius="3" Margin="0,0,0,1">
    <Border.BorderBrush>
        <RadialGradientBrush>
            <GradientStop Color="Black" Offset="0.047"/>
            <GradientStop Color="#FF00907A" Offset="1"/>
        </RadialGradientBrush>
    </Border.BorderBrush>
    <Image x:Name="imgVideo" Stretch="Fill"  />
</Border>
<Border  Grid.Column="3" Grid.Row="1" BorderThickness="3" CornerRadius="3" Margin="0,0,0,1">
    <Border.BorderBrush>
        <RadialGradientBrush>
            <GradientStop Color="Black" Offset="0.047"/>
            <GradientStop Color="#FF00907A" Offset="1"/>
        </RadialGradientBrush>
    </Border.BorderBrush>
    <Image x:Name="imgCapture" Stretch="None" Width="200" Height="150" />
</Border>
<StackPanel Grid.Column="3" Grid.Row="5" Orientation="Horizontal" HorizontalAlignment="Left" Width="162">
    <Button x:Name="bntCapture" Content="Capture Image" Click="bntCapture_Click" />       
</StackPanel>

在 C# 事件处理程序中

private void bntCapture_Click(object sender, RoutedEventArgs e)
{
    for (int j = 1; j <=30;j++ )
    {
        string path = Directory.GetCurrentDirectory();              
        string em_cap_original_path=System.IO.Path.Combine(path, "Database");
        imgCapture.Source = imgVideo.Source;                
        Helper.SaveImageCapture((BitmapSource)imgCapture.Source, em_cap_original_path, j);
    }
    MessageBox.Show("Images created"); 
}

这是助手class

class Helper
{
    //Block Memory Leak
    [System.Runtime.InteropServices.DllImport("gdi32.dll")]
    public static extern bool DeleteObject(IntPtr handle);
    public static BitmapSource bs;
    public static IntPtr ip;
    public static BitmapSource LoadBitmap(System.Drawing.Bitmap source)
    {

        ip = source.GetHbitmap();

        bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(ip, IntPtr.Zero, System.Windows.Int32Rect.Empty,

        System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

        DeleteObject(ip);

        return bs;

    }
    public static void SaveImageCapture(BitmapSource bitmap, string em_cap_original_path, int counter)
    {            
        PngBitmapEncoder encoder = new PngBitmapEncoder();
        encoder.Interlace = PngInterlaceOption.On;
        encoder.Frames.Add(BitmapFrame.Create(bitmap));            
        string img_path = em_cap_original_path + "/" + "frame" + counter + ".png";
        FileStream fp;
        fp = System.IO.File.Create(img_path);
        encoder.Save(fp);
        fp.Close();                       
    }
}

此代码可以生成 10 张图像,但它们是同一图像的拍摄,而不是来自图像序列。

如果有人知道我该怎么做。任何帮助将不胜感激。

如果偶数处理程序中的循环是您拍摄多张照片的方式,您应该在拍摄照片之间稍等片刻,您可以这样做:

private async void bntCapture_Click(object sender, RoutedEventArgs e)
{
    for (int j = 1; j <= 30; j++)
    {
        string path = Directory.GetCurrentDirectory();
        string em_cap_original_path = System.IO.Path.Combine(path, "Database");
        imgCapture.Source = imgVideo.Source;
        Helper.SaveImageCapture((BitmapSource)imgCapture.Source, em_cap_original_path, j);
        await Task.Delay(20);//Delay in ms
    }
    MessageBox.Show("Images created");
}

注意 async 空白和 await Task.Delay()