通过 WCF 发送图像的有效方法? [英] Efficient way to send images via WCF?

查看:30
本文介绍了通过 WCF 发送图像的有效方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过从头开始编写自定义远程控制应用程序(如 VNC)来学习 WCF、LINQ 和其他一些技术.我在创建它时考虑了三个主要目标:

I am learning WCF, LINQ and a few other technologies by writing, from scratch, a custom remote control application like VNC. I am creating it with three main goals in mind:

  1. 服务器将在应用程序级别(即无缝窗口)提供远程控制",而不是完全桌面访问.
  2. 客户端可以选择在服务器上运行的任意数量的应用程序,并接收每个应用程序的图像流.
  3. 一个客户端可以同时连接到多个服务器.

现在我正在使用 WCF 发送一个代表正在发送的窗口的字节数组:

Right now I am using WCF to send an array of Bytes that represents the window being sent:

using (var ms = new MemoryStream()) {
    window.GetBitmap().Save(ms, ImageFormat.Jpeg);
    frame.Snapshot = ms.ToArray();
}

GetBitmap 实现:

GetBitmap implementation:

var wRectangle = GetRectangle();
var image = new Bitmap(wRectangle.Width, wRectangle.Height);
var gfx = Graphics.FromImage(image);

gfx.CopyFromScreen(wRectangle.Left, wRectangle.Top, 0, 0, wRectangle.Size, CopyPixelOperation.SourceCopy);

return image;

然后通过 WCF(TCPBinding,它将始终通过 LAN)发送到客户端,并以没有边框的空白窗口形式重建,如下所示:

It is then sent via WCF (TCPBinding and it will always be over LAN) to the client and reconstructed in a blank windows form with no border like this:

using (var ms = new MemoryStream(_currentFrame.Snapshot))
{
    BackgroundImage = Image.FromStream(ms);
}

我想让这个过程在 CPU 和内存使用方面尽可能高效,带宽排在第三位.我的目标是让客户端连接到 5 个以上的服务器,每个服务器有 10 个以上的应用程序.

I would like to make this process as efficient as possible in both CPU and memory usage with bandwidth coming in third place. I am aiming to have the client connect to 5+ servers with 10+ applications per server.

我现有的方法是否是最好的方法(同时继续使用这些技术),我可以做些什么来改进它?

Is my existing method the best approach (while continuing to use these technologies) and is there anything I can do to improve it?

我正在研究的想法(但我没有经验):

Ideas that I am looking into (but I have no experience with):

  • 使用开源图形库而不是 .Net 解决方案来捕获和保存图像.
  • 另存为 PNG 或其他图像类型,而不是 JPG.
  • 每次发送图像增量而不是完整图像.
  • 尝试录制"窗口并创建压缩视频流而不是图片快照(mpeg?).

推荐答案

您应该注意以下几点:

  • 传输:TCP/二进制消息编码将是传输图像数据的最快方式
  • 图像捕获:您可以依靠 P/Invoke 来访问您的屏幕数据,因为这会更快且更消耗内存.一些示例:在 C# 中捕获屏幕图像 [P/Invoke]如何使用 .NET [Managed] 和 截取屏幕截图href="http://www.geekpedia.com/tutorial181_Capturing-screenshots-using-Csharp.html" rel="noreferrer">使用 C# 捕获屏幕截图(托管)
  • 您应该在发送之前减少您的图像数据;
    • 明智地选择您的图像格式,因为某些格式具有本机压缩(如 JPG)
    • 一个例子应该是 查找图像之间的差异 C#
    • 只发送差异图像,您可以裁剪它并只发送非空白区域

    只需完成所有这些步骤并对最终代码感到满意后,您就可以下载VncSharp 源代码.它实现了RFB 协议 (维基百科条目), "一种用于远程访问图形用户界面的简单协议.因为它在帧缓冲区级别工作,所以适用于所有窗口系统和应用程序,包括 X11、Windows 和 Macintosh.RFB 是 VNC(虚拟网络计算)中使用的协议."

    Just after passing through all this steps and being satisfied with your final code, you can download VncSharp source code. It implements the RFB Protocol (Wikipedia entry), "a simple protocol for remote access to graphical user interfaces. Because it works at the framebuffer level it is applicable to all windowing systems and applications, including X11, Windows and Macintosh. RFB is the protocol used in VNC (Virtual Network Computing)."

    这篇关于通过 WCF 发送图像的有效方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆