使用wpf调用摄像头拍照并打印

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);
        }
    }
}

  • 4
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值