【发布时间】:2014-08-13 16:46:44
【问题描述】:
我有一个将图像从 IPcamera 保存到文件流的项目。我有另一个项目,它从文件流中获取图像并将其保存为 jpg,然后在网络表单中显示图像。我添加了一个javascript脚本来每x秒刷新一次图像,但问题是新图像只有在页面加载时才会保存。这样做是因为我将 c# 代码放在“Page_Load()”函数中。我想知道是否有办法在 C# 中创建一个具有保存图像的代码部分的函数,然后在刷新图像时每 x 秒调用一次该函数?因为现在因为没有保存新图像,所以它只是刷新到相同的图像。我编写了一个代码,每 x 秒刷新一次整个页面,但我觉得这种方式会更好。我正在使用 Visual Studio 2010。
这里是提到的第二个项目的 C# 代码:
namespace PlayVideo
public partial class Video : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string saveTo = @"place to save";
FileStream writeStream = new FileStream(saveTo, FileMode.Create, FileAccess.ReadWrite);
using (FileStream fs = File.Open(@"Place where file is saved", FileMode.Open, FileAccess.ReadWrite, FileShare.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();
}
}
viewer.aspx页面的代码是:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="viewer.aspx.cs" Inherits="PlayVideo.viewer" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
</head>
<body>
<form id="form1" runat="server">
<div style="height: 60px" id="div1">
<img src="/video.aspx" id="the_image" alt="" /><br />
<script type="text/javascript" language="javascript">
function refreshImage() {
$("#the_image").attr('src', 'video.aspx');
setTimeout(refreshImage, x ms);
}
$(document).ready(function () {
setInterval(refreshImage, x ms);
})
</script>
</div>
</form>
</body>
</html>
有没有办法做到这一点?我已经阅读了有关客户端与服务器端的内容,但是由于我已经每 x 秒刷新一次图像,这有关系吗?
【问题讨论】:
-
在您的 javascript 中单击 UpdatePanel 内的按钮,该按钮调用服务器端事件,其中包含您的文件保存代码。
-
但我希望它自动保存。
-
" ...但问题是新图像仅在页面加载时才被保存..." 你为什么这样做?您为什么不将该代码放在一个单独的函数中,并使用 javascript 来尽可能频繁地调用该函数?
-
这正是我现在想要做的,也是我在这篇文章中要问的。
标签: c# javascript jquery asp.net visual-studio-2010