使用C#进行Webform流式直播视频

时间:2014-07-29 20:09:32

标签: c# asp.net visual-studio-2010 webforms video-streaming

所以我有一个Windows应用程序可以保存IP摄像机的每一帧。好吧它保存到同一个文件,每个新图像都会覆盖最后一个。现在我有一个显示此图像的webform,但我必须按一个按钮才能显示新图像。这是代码:

namespace PlayVideo

public partial class Video : System.Web.UI.Page
{

    FileStream fs = File.Open(@"location of filestream");



    protected void Page_Load(object sender, EventArgs e)
    {

        string saveTo = @"place to save";
        FileStream writeStream = new FileStream(saveTo, FileMode.Create, FileAccess.ReadWrite);

        ReadWriteStream(fs, writeStream);

        Image1.ImageUrl = "~/images/OrangeLogo.jpg";
        Image2.ImageUrl = "~/images/test.jpg";


    }

    // readStream is the stream you need to read
    // writeStream is the stream you want to write to
    private void ReadWriteStream(Stream readStream, Stream writeStream) 
    {
        int Length = 256;
        Byte [] buffer = new Byte[Length];
        int bytesRead = readStream.Read(buffer,0,Length);
        // write the required bytes
        while( bytesRead > 0 ) 
        {
            writeStream.Write(buffer,0,bytesRead);
            bytesRead = readStream.Read(buffer,0,Length);
        }
        readStream.Close();
        writeStream.Close();
    }

    protected void StartBut_Click(object sender, EventArgs e)
    {

        FileStream writeStream = File.OpenWrite(@"location of writestream");
        FileStream readStream = File.Open(@"location of readstream");

        ReadWriteStream(readStream, writeStream);
        Image1.ImageUrl = "~/images/test.jpg";

    }



}

ReadWriteStream()的代码来自 http://www.developerfusion.com/code/4669/save-a-stream-to-a-file/

所以我的问题是,如何在不按下按钮的情况下显示新图像?这可能吗?哦,我必须使用VS2010,因为它是为了工作,他们只有VS2010。

1 个答案:

答案 0 :(得分:0)

您需要创建一个查看器页面来显示图像并放置此标记:

< img src =“/ video.aspx”id =“the-image”/>

然后在同一页面上,您可以添加一个javascript来刷新图像:

function refreshImage(){
    $("#the-image").attr('src', 'video.aspx');
    setTimeout("refreshImage();", 5000);
}

$(document).ready(function(){
    setTimeout("refreshImage();", 5000);
})

然后你需要改变你的video.aspx以吐出图像:

protected void Page_Load(object sender, EventArgs e)
{
    Response.Clear();
    Response.TransmitFile(imagePath);
}

您可能需要优化此代码才能使其正常工作,但您明白了这一点......