从内存中更新 ASP.NET 页面中的图像而不刷新页面

发布于 2024-08-27 23:19:28 字数 622 浏览 5 评论 0原文

我有一个命令行 C# 服务器和一个 ASP.NET 客户端,它们都通过 .NET Remoting 进行通信。服务器以每秒 2 帧的速度向客户端发送从网络摄像头抓取的静态图像。我可以通过远程方法调用将图像传递给客户端,并且图像最终在此客户端方法中:

    public void Update(System.Drawing.Image currentFrame)
    {
        // I need to display the currentFrame on my page.
    }
  1. 如何在页面上显示图像而不将图像保存到硬盘?如果它是一个 winForms 应用程序,我可以将 currentFrame 传递到图片框,即。 picturebox.Image = currentFrame.

  2. 上述 Update 方法每秒至少触发 2 次。如果客户端是 winForms 应用程序,我可以简单地输入picturebox.Image = currentFrame,屏幕上的当前帧将自动更新。如何使用 ASP.NET 达到同样的效果?整个页面需要重新加载等吗?我基本上想让它看起来像实时视频源!

I have a command line C# server and an ASP.NET client, they both communicate via .NET Remoting. The server is sending the client still images grabbed from a webcam at say 2 frames a second. I can pass the images down to the client via a remote method call and the image ends up in this client method:

    public void Update(System.Drawing.Image currentFrame)
    {
        // I need to display the currentFrame on my page.
    }
  1. How do i display the image on the a page without saving the image to the hard disc? If it was a winForms app i could pass currentFrame to a picturebox ie. picturebox.Image = currentFrame.

  2. The above Update method gets fired at least 2 times a second. If the client was a winForms app i could simply put picturebox.Image = currentFrame and the current frame on the screen would automatically get updated. How do i achieve the same effect with ASP.NET? Will the whole page need to be reloaded etc? I want to basically make it look like a live video feed!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

彩扇题诗 2024-09-03 23:19:29

您已将一名玩家排除在游戏之外:浏览器。您的问题实际上是:如何使互联网上的某些随机计算机上的浏览器根据服务器中发生的情况更新其显示的图像。

答案当然是:你不知道。

您可以将图像放置在 UpdatePanel 内,并根据计时器更新面板。图像的 src 属性的 URL 必须指向您自己的 HttpHandler.ashx 文件),该文件将调用服务器获取最新图像。

永远记住 Web 的工作原理:控制权是浏览器,而不是服务器端。

PS .NET Remoting 已被弃用,取而代之的是 WCF。

You have left one player out of your game: the browser. Your question really is: how do I make the browser, on some random computer in the Internet, update an image it is displaying based on what's happening in a server.

The answer, of course, is: you don't.

You can place the image inside of an UpdatePanel, and update the panel based on a timer. The URL for the src property of the image would have to lead to a HttpHandler (.ashx file) of your own, that would call the server to get the latest image.

Always remember how the Web works: the browser is in control, not the server side.

P.S. .NET Remoting has been deprecated in favor of WCF.

孤君无依 2024-09-03 23:19:29

您需要在服务器上有一个页面或处理程序来呈现图像。这就是它的工作。然后,您可以在页面上放置 javascript,显示作用于计时器 (window.setTimeout) 的图像,并不断轮询服务器以获取更新的图像。您可能希望在查询字符串中使请求有些动态,这样您就不会因重新显示缓存的图像而不是服务器提供的新图像而陷入困境。像

<script type="text/javascript" language="javascript">
    i = 0;
    window.setTimeout(updateImage, 500);
    var img = document.getElementById('img_to_update');

    function updateImage() {
        img.src = 'imageRenderer.aspx?req=' + i;
        i += 1;
        window.setTimeout(updateImage, 500);
    }
</script>

Where i 这样的东西仅用于保持请求唯一以防止缓存问题。

无论如何,这是确切的脚本吗?也许不是,我把它从我的脑海中写下来,我已经有一段时间没有做过类似的事情了。但它应该让您了解如何实现部分独立于服务器端技术的客户端。 (此脚本可以位于任何页面上,静态或动态呈现。)

You will need to have a page or handler on the server to render the image. That's its job. Then you can have javascript in place on the page that displays the image that acts on a timer (window.setTimeout) and is constantly polling the server for an updated image. You probably want to make the request somewhat dynamic in the querystring so you don't get stuck redisplaying a cached image instead of a new image delivered by the server. Something like

<script type="text/javascript" language="javascript">
    i = 0;
    window.setTimeout(updateImage, 500);
    var img = document.getElementById('img_to_update');

    function updateImage() {
        img.src = 'imageRenderer.aspx?req=' + i;
        i += 1;
        window.setTimeout(updateImage, 500);
    }
</script>

Where i only serves to keep the request unique to prevent caching issues.

Anyway, is that the exact script? Perhaps not, I wrote it off the top of my head and it has been a while since I've done anything similar. But it should give you an idea of how you can achieve part of this client-side indepenent of the server side technology. (This script could be on any page, static or dynamically rendered.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文