我有一个项目将图像从IPcamera保存到文件流。我有另一个项目从文件流中获取图像并将其保存为jpg,然后在webform中显示图像。以下是第二个项目的C#代码:
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);
Response.Clear();
Response.TransmitFile("~/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();
}
然后我添加了一个查看器页面来显示图像并使用javascript刷新图像,这里是代码:
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Image ID="Image1" runat="server" />
<img src="/video.aspx" id="the-image" alt="" />
<script language="JavaScript" type="text/javascript">
function refreshImage() {
$("#the-image").attr('src', 'video.aspx');
setTimeout("refreshImage();", 1000);
}
$(document).ready(function () {
setTimeout("refreshImage();", 1000);
})
</script>
</div>
</form>
我的问题是图像永远不会刷新。我已经尝试将javascript放在html的不同部分,甚至在网上找到了很多不同的javascript方法。我现在认为我的C#代码部分出了问题,而不是javascript,但我不知道。有人能帮助我吗?
答案 0 :(得分:0)
以下是一些代码:http://jsfiddle.net/39yN8/1/
// Declaring a function that may be called later.
function refreshImage()
{
// Get the img DOM element. Only your img tag with 'myIMG' id is caught.
objIMG = document.getElementById('myIMG');
// Refresh the image src attribute.
// There is some trick to change the original URL string to hack the browser cache.
objIMG.src = objIMG.src.substr(0, objIMG.src.indexOf('&nocache=')); + '&nocache=' + Math.random();
}
$(document).ready(function () // When page is loaded...
{
// Call refreshImage function every 1000 milliseconds.
setInterval(refreshImage, 1000);
})
基本上,你在浏览器缓存方面遇到了一些问题,所以我在URL中添加了一个带有随机数的“no-cache”参数,以欺骗你的浏览器。