如何循环检索流图像?

时间:2012-10-01 09:23:01

标签: c# aforge

我如何才能获得所有流媒体jpeg来创建avi?

Bitmap img = eventArgs.Frame;
for (int i = 0; i < 1000; i++)
{
    //img.SetPixel(i, i, Color.Red);
    writer.AddFrame(img);
}
writer.Close();

我从AForge那里得到了这个例子,http://www.aforgenet.com/framework/docs/html/5fedbbbe-6d28-5f69-35a2-9d7119544a2d.htm 但是当我测试它时,它会创建一个视频文件,从视频的开头到结尾都有1个相同的帧。

我如何执行writer.AddFrame(img);每次都用新框架创建视频文件?


编辑:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;

using System.Drawing;
using System.IO;

using AForge.Video;
using AForge.Video.VFW;
using AForge.Video.DirectShow;

namespace VidRec
{
    public partial class LiveRecording : System.Web.UI.Page
    {
        AVIWriter writer = new AVIWriter("MSVC");   //video with compression

    protected void Page_Load(object sender, EventArgs e)
    {
            //camera source
            string streamingSource = "http://e888.example.com:81/snapshot.cgi";

            // create new AVI file and open it
            writer.Open("c:\\video.avi", 640, 480);

            //IPcam streaming source to be recorded
            JPEGStream JPEGSource = new JPEGStream(streamingSource);
            //MJPEGStream JPEGSource = new MJPEGStream(streamingSource);
            //login name and password
            JPEGSource.Login = "login";
            JPEGSource.Password = "password";
            //set NewFrame event handler
            JPEGSource.NewFrame += new NewFrameEventHandler(video_NewFrame);

            //start streaming the image files
            JPEGSource.Start();
    }

    private void video_NewFrame(object sender, NewFrameEventArgs eventArgs) //event handler for NewFrame
    {
        //get frame
        Bitmap image = eventArgs.Frame;
        //add the image as a new frame of video file
        writer.AddFrame(image);

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        writer.Close();
    }
}

}

3 个答案:

答案 0 :(得分:0)

如果查看您的示例,您将看到他们在每个循环中修改图像。你不这样做,所以你从头到尾都有相同的框架。

答案 1 :(得分:0)

对于循环的每次迭代,您需要将下一个图像放入img中。

你将它们冷却加载到一个数组中,然后在你的for循环内循环。


评论后编辑

说你有10张图片名称1.jpg到10.jpg

你冷得像这样的数组。

        Image[] imageAry = new Image[10];

        for (int i = 0; i < 10; i++)
        {
            imageAry[i] = Image.FromFile(string.Format("{0}.jpg", i + 1))
        }

然后你将在你的for循环中使用你的imageAry数组并迭代每张图片。

答案 2 :(得分:0)

要测试一个示例,只需进行少许更改并取消注释第4行,如:

Bitmap img = eventArgs.Frame;
for (int i = 0; i < 1000; i++)
{
    JPEGStream JPEGSource = new JPEGStream(streamingSource);
    img = (bitmap)JPEGSource;
    // if JPEGSource is stream not image then use below like instead of above line
    // img = Image.FromStream(JPEGSource);
    writer.AddFrame(img);
}
writer.Close();

如果以上操作不起作用并且您将此流保存到名称为timestamp.jpeg的任何文件夹,则可以使用以下代码:

using System.IO;

现在:

Bitmap img = eventArgs.Frame;
for (int i = 0; i < 1000; i++)
{
    // JPEGStream JPEGSource = new JPEGStream(streamingSource);
    // save it to folder like d:\images\
    // if you are saving images to folder using any other program 
    // then this might not be needed
    string[] filePaths = Directory.GetFiles(@"d:\images\");
    foreach(string f in filePaths)
    {
        img = Image.FromFile(f);
        writer.AddFrame(img);
    }
}
writer.Close();