- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
大家。几天来我一直被困在这里处理这个错误,但我仍然无法弄清楚。我的猜测:我认为我的代码有一些问题,因为我在使用对象后没有正确处理对象,(我对释放资源、线程这些概念不是很熟悉)。我通过引用人们在 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 用户都可以按下面的链接进行试用。谢谢!
最佳答案
在查看您的代码后,在我看来,您似乎在保存图像之前处理了图像。这意味着您的程序无法保存图像,因为它不再存在。实际上,它显示您实际上已经删除了两次捕获的图像,一次是在处置时,第二次是在您将其设置为 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/
我目前正在做一个“自动车牌检测和识别”,我正在使用 AForge.Net;我使用了一个简单的算法; 虽然它不是很有效,但它会满足我的要求。我已经计算出高度、宽度和高度与宽度(高度/宽度)的比率超过40
我的 WinForms .NET 4 C# 应用程序在用户与其交互时记录桌面。 它根据系统的速度使用 AForge FFMPEG 或 VFW 包装器。当然,捕获是在后台线程中完成的。 无论哪种情况,包
我已经编写了一些需要处理大约 3000 张图像的 C# 代码,但是到第 500 张图像时,任务管理器显示该程序使用了 1.5GB 的内存。下面的功能似乎是罪魁祸首之一。我可以在这里做得更好吗?任何帮助
我正在使用 Aforge .NET 框架在我的应用程序中获取网络摄像头,但我总是得到一个全灰色的图像框架。 this.videoSource = new VideoCaptur
我正在玩 AForge。我从 AForge 网站复制粘贴了示例。 using System; using System.Collections.Generic; using System.Linq;
我正在尝试在 Unity 中使用 AForge,但在转换输入数据时遇到了问题。我有一个二维数组,用于存储需要转换为 UnmanagedImage 的像素值。我想出了以下代码,但我不确定它是否是最有效的
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this qu
我正在尝试学习 Aforge.net。我写了 o 代码,它工作正常,但我只想在我的图像上检测到紫色,但我找不到它是怎么回事。所以任何人都可以帮助我吗?这是我的代码 Bitmap resim = new
有人可以给我一个完整的例子,使用这个用 VB.NET 或 C# 编写的库在 picturebox 中显示流的图像 最佳答案 这是一个非常非常简单的表单,它使用了 AForge.Video.MJPEGS
如何使用此方法: public static void DFT (Complex[] data, Direction direction) 如果我的输入数组是short[] samples?我可以轻松
我正在尝试使用 Aforge.net 开发一个 LPR 系统,我想在我的图像上应用一个过滤器,正如您在此处看到的: Bitmap a = new Bitmap(@"C:\Use
我需要一些帮助将视频从笔记本电脑相机保存到文件。现在我使用代码来搜索图片框上的视频设备和以前的视频。 我试过 AVIFileWrite 但我失败了。 如何使用 VideoFileWriter 来实现我
我正在尝试从视频文件中提取特定帧。当我使用 aforge 库播放视频文件时,我有一个想要的帧。我调用了一个新帧事件,如果新帧与我的特定帧匹配,那么它会显示一条消息:“帧匹配”。此特定帧随机出现在视频文
我尝试使用 AForge 在 C# 中使用 IP 摄像机做一个录制功能,但我无法真正理解编码,因为我是一个新学习者。录制的视频将保存在我的电脑中。 据我所知,此功能是编写/创建视频并将其保存在我的存储
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我正在尝试检测这张图片上的矩形: 使用此代码: static void Main(string[] args) { // Open your image string path = "
我正在使用 AForge 进行运动检测,并且我知道可以设置运动区域。是否可以让它仅在所有定义区域中有运动时触发?如果上面的功能不是现成的,我正在考虑编写它。 目前,我的理解是区域设置为视觉库中 Mot
我需要在图像上查找对象,因此我尝试使用 AForge 库来完成。我从非常简单的图案和图片开始,但识别准确度已经很糟糕了。我正在使用 ExhaustiveTemplateMatching: Exhaus
如何检测视频中的非移动 Blob ? 假设我有一个视频和一个初始背景帧位图。是否有可能检测到不移动的 Blob /对象?并围绕该对象绘制一个矩形? 最佳答案 这让我想起了一种检测地铁上遗忘元素的算法。
我正在尝试在 AForge 中检测矩形内的圆。我已成功确定 Rectangles,但无法在 Rectangle 中找到 circles。如何在 AForge 中找到另一个形状内的形状。 string
我是一名优秀的程序员,十分优秀!