gpt4 book ai didi

c# - Aforge.net 相机捕获并将图像保存到目录

转载 作者:太空狗 更新时间:2023-10-29 21:44:34 24 4
gpt4 key购买 nike

大家。几天来我一直被困在这里处理这个错误,但我仍然无法弄清楚。我的猜测:我认为我的代码有一些问题,因为我在使用对象后没有正确处理对象,(我对释放资源、线程这些概念不是很熟悉)。我通过引用人们在 youtube 上所做的事情获得了这些代码,但是尽管我做了完全相同的事情,但我的代码并没有很好地运行。

情况:我有两个图片框,左边一个可以拍我的视频,右边一个拍快照,如果你按下 button1 ,你会开始视频, clone_button 会复制一张图像即拍快照, save_image 应该保存到路径引用,但是,当我试图保存它时,我一次又一次地在 GDI+ 中收到一个一般性错误。此外,我的调试器似乎变得疯狂(即无法终止 vshost.exe ),一旦我运行了这个程序,我必须重新启动计算机才能让我的代码再次运行,这是令人沮丧和沮丧的。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;
//AForge.Video dll
using AForge.Video;
using AForge.Video.DirectShow;
using AForge.Imaging;
using AForge.Imaging.Filters;
using AForge;


namespace WebCameraCapture
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private FilterInfoCollection CaptureDevice; // list of webcam
private VideoCaptureDevice FinalFrame;

private void Form1_Load(object sender, EventArgs e)
{
CaptureDevice = new FilterInfoCollection(FilterCategory.VideoInputDevice);//constructor
foreach (FilterInfo Device in CaptureDevice)
{
comboBox1.Items.Add(Device.Name);
}

comboBox1.SelectedIndex = 0; // default
FinalFrame = new VideoCaptureDevice();
}

private void button1_Click(object sender, EventArgs e)
{
FinalFrame = new VideoCaptureDevice(CaptureDevice[comboBox1.SelectedIndex].MonikerString);// specified web cam and its filter moniker string
FinalFrame.NewFrame += new NewFrameEventHandler(FinalFrame_NewFrame);// click button event is fired,
FinalFrame.Start();
}

void FinalFrame_NewFrame(object sender, NewFrameEventArgs eventArgs) // must be void so that it can be accessed everywhere.
// New Frame Event Args is an constructor of a class
{
pictureBox1.Image = (Bitmap)eventArgs.Frame.Clone();// clone the bitmap
}

private void From1_CLosing(object sender, EventArgs e)
{
if (FinalFrame.IsRunning==true) FinalFrame.Stop();
}

private void save_Click(object sender, EventArgs e)
{
if (pictureBox2.Image != null)
{
Bitmap varBmp = new Bitmap(pictureBox2.Image);
Bitmap newBitmap = new Bitmap(varBmp);
varBmp.Dispose();
varBmp = null;
varBmp.Save(@"C:\a.png", ImageFormat.Png);
}
else
{ MessageBox.Show("null exception"); }
}

private void clone_Click(object sender, EventArgs e)
{
pictureBox2.Image = (Bitmap)pictureBox1.Image.Clone();
}
}
}

任何 AForge.net 用户都可以按下面的链接进行试用。谢谢!

SAMPLE

最佳答案

在查看您的代码后,在我看来,您似乎在保存图像之前处理了图像。这意味着您的程序无法保存图像,因为它不再存在。实际上,它显示您实际上已经删除了两次捕获的图像,一次是在处置时,第二次是在您将其设置为 null 时。

因此,如果您在保存后移动这两个代码段,它应该可以正常工作。在不使用对话框更改文件名的情况下,您肯定会收到错误消息,除非您在每次创建该文件后将其删除。

private void save_Click(object sender, EventArgs e)
{
if (pictureBox2.Image != null)
{
//Save First
Bitmap varBmp = new Bitmap(pictureBox2.Image);
Bitmap newBitmap = new Bitmap(varBmp);
varBmp.Save(@"C:\a.png", ImageFormat.Png);
//Now Dispose to free the memory
varBmp.Dispose();
varBmp = null;
}
else
{ MessageBox.Show("null exception"); }
}

如果你打开任务管理器,你可以看到你的程序占用了多少内存。使用完毕后处理内存,将其还给系统。您的 FinalFrame_NewFrame 线程中没有处置,因此当相机正在读取图像时,您应该会看到内存使用量继续攀升,直到您停止该程序。

我已将 dispose 添加到我的线程中,使内存使用得到控制,但现在我正在调试我的图像保存。因为我正在处理,所以我无法保存图像大声笑。我的程序最终尝试保存一个空图像文件并抛出相应的错误。

我和你一样使用第二个picurebox,但是使用例如pbox2.image = pbox1.image,不复制数据,它复制图像数据的内存位置,所以当我处理pbox1时释放内存,图像数据随内存位置一起消失。

关于c# - Aforge.net 相机捕获并将图像保存到目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24770522/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com