Aforge的一点简单应用

    今天正好需要将一些无标识的图片按照甲方给的编号将图片命名,我了个去,2340张,2340张啊!!!还是无偿的啊。。。。。心中无数只草尼草呼啸而过。。真是上面运动嘴,下面跑断腿啊。。啥也别说了,整吧。。要不不能怎么着。说实话,以前的项目也没有图片处理的经验,百度上神游一圈,别说,还真有点眉目了。四下对此下来,一个叫AForge的控件还挺对胃口的。再一圈参考下来,打开VS2008,洗个澡,let's go !

首先添加一些引用:

using AForge;
using AForge.Controls;
using AForge.Video;
using AForge.Video.DirectShow;
using AForge.Imaging;
using AForge.Imaging.Filters;

然后新建一个选项卡(千万别告诉我不会 - -!),然后把Aforge.Controls.dll拖放在上面,会出现几个工具,如下图所示:

将VideoSourcePlayer控件拖放在form上,出会现控件区域。

然后该coding了,在form_load中整代码如下:

        private void Form1_Load(object sender, EventArgs e)
        {
            count = 1;
            //枚举所有视频输入设备
            videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);

            if (videoDevices.Count == 0)
            {
                MessageBox.Show("未发现视频设备!");
                return;
            }
           
            VideoCaptureDevice videoSource = new VideoCaptureDevice(videoDevices[0].MonikerString);
            videoSource.DesiredFrameSize = new Size(800, 600);
            videoSource.DesiredFrameRate = 1;
            videoSourcePlayer1.VideoSource = videoSource;

        }

先枚举视频设备,并定义大小和帧数。

然后打开摄像头:

 videoSourcePlayer1.Start();

然后在videoSourcePlayer事件中找到NewFrame事件并双击:

在里面编辑代码如下:

                Bitmap bitMap = image;
                bitMap.Save(@"D:\" + textBox1.Text.Trim() + "_" + count.ToString() + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);

//如果没有下面这行代码,将连续拍照,此行代码关键!!!!

                videoSourcePlayer1.NewFrame -= new VideoSourcePlayer.NewFrameHandler(videoSourcePlayer1_NewFrame);

然后在拍照按钮中代码如下:

注册这个事件,

  videoSourcePlayer1.NewFrame +=new VideoSourcePlayer.NewFrameHandler(videoSourcePlayer1_NewFrame);

基本代码就完成了,但是有个坑一直没有解决,望哪位大量指点,这个坑是:

一打开视频自动就会照一张图片是为什么呢??求解决啊求解决!

海纳百川,有容乃大;壁立千仞,无欲则钢!

测试代码链接:http://download.csdn.net/my/uploads

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值