XAML 部分
<Window x:Class="CameraCapture.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Camera Capture" Height="450" Width="800">
<Grid>
<Button Content="Capture" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Click="CaptureButton_Click"/>
<Button Content="Print" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="80,0,0,0" Click="PrintButton_Click"/>
<ListBox Name="PhotosListBox" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,40,0,0" Width="780" Height="360">
<ListBox.ItemTemplate>
<DataTemplate>
<Image Source="{Binding}" Width="100" Height="100" Margin="5"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Window>
后端代码
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using AForge.Video;
using AForge.Video.DirectShow;
namespace CameraCapture
{
public partial class MainWindow : Window
{
private FilterInfoCollection videoDevices;
private VideoCaptureDevice videoSource;
private Bitmap currentFrame;
private List<BitmapImage> capturedImages;
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
capturedImages = new List<BitmapImage>();
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
if (videoDevices.Count == 0)
{
MessageBox.Show("No video devices found");
return;
}
videoSource = new VideoCaptureDevice(videoDevices[0].MonikerString);
videoSource.NewFrame += new NewFrameEventHandler(videoSource_NewFrame);
videoSource.Start();
}
private void videoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
if (currentFrame != null)
{
currentFrame.Dispose();
}
currentFrame = (Bitmap)eventArgs.Frame.Clone();
}
private void CaptureButton_Click(object sender, RoutedEventArgs e)
{
if (currentFrame != null)
{
BitmapImage bitmapImage = BitmapToBitmapImage(currentFrame);
capturedImages.Add(bitmapImage);
PhotosListBox.Items.Add(bitmapImage);
}
}
private BitmapImage BitmapToBitmapImage(Bitmap bitmap)
{
using (MemoryStream memory = new MemoryStream())
{
bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);
memory.Position = 0;
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memory;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
bitmapImage.Freeze();
return bitmapImage;
}
}
private void PrintButton_Click(object sender, RoutedEventArgs e)
{
BitmapImage selectedImage = PhotosListBox.SelectedItem as BitmapImage;
if (selectedImage != null)
{
PrintImage(selectedImage);
}
else
{
MessageBox.Show("Please select an image to print.");
}
}
private void PrintImage(BitmapImage image)
{
PrintDocument printDoc = new PrintDocument();
printDoc.PrintPage += (sender, e) =>
{
Bitmap bitmap = BitmapImageToBitmap(image);
e.Graphics.DrawImage(bitmap, e.MarginBounds);
bitmap.Dispose();
};
try
{
PrintDialog printDialog = new PrintDialog();
if (printDialog.ShowDialog() == true)
{
printDoc.PrinterSettings.PrinterName = printDialog.PrintQueue.FullName;
printDoc.Print();
}
}
catch (Exception ex)
{
MessageBox.Show("An error occurred while trying to print: " + ex.Message);
}
}
private Bitmap BitmapImageToBitmap(BitmapImage bitmapImage)
{
using (MemoryStream outStream = new MemoryStream())
{
BitmapEncoder enc = new BmpBitmapEncoder();
enc.Frames.Add(BitmapFrame.Create(bitmapImage));
enc.Save(outStream);
return new Bitmap(outStream);
}
}
protected override void OnClosed(EventArgs e)
{
if (videoSource != null && videoSource.IsRunning)
{
videoSource.SignalToStop();
videoSource.WaitForStop();
}
base.OnClosed(e);
}
}
}