简体   繁体   English

在C#中快速复制GUI?

[英]Fast copying of GUI in C#?

Right now I'm copying window graphics from one window to another via BitBlt from WinApi. 现在我正在通过WinApi的BitBlt将窗口图形从一个窗口复制到另一个窗口。 I wonder if there is any other fast / faster way to do the same in C#. 我想知道在C#中是否还有其他快速/更快的方法来做同样的事情。

Keyword is performance here. 关键字是这里的表现 If I should stay with WinApi I would hold HDC in memory for quick drawing and if .NET Framework has other possibilities I would probably hold Graphics objects. 如果我应该继续使用WinApi,我会在内存中保存HDC以便快速绘图,如果.NET Framework有其他可能性,我可能会持有Graphics对象。 Right now I'm something to slow when I have to copy ~ 1920x1080 windows. 现在,当我必须复制~1920x1080的窗口时,我有点慢。

So how can I boost performance of gui copying in C# ? 那么如何在C#中提升gui复制的性能呢?
I just want to know if I can get better than this. 我只是想知道我是否能比这更好。 Explicit hardware acceleration (OpenGL, DirectX) is out of my interest here. 显式硬件加速(OpenGL,DirectX)在我看来不是我的兴趣。 I decided to stay pure .NET + WinApi. 我决定保持纯.NET + WinApi。

// example to copy desktop to window
Graphics g = Graphics.FromHwnd(Handle);
IntPtr dc = g.GetHdc();
IntPtr dc0 = Windows.GetWindowDC(Windows.GetDesktopWindow());
Windows.BitBlt(dc, 0, 0, Width, Height, dc0, 0, 0, Windows.SRCCOPY);
// clean up of DCs and Graphics left out

Hans's questions: 汉斯的问题:

  • How slow is it? 它有多慢?
    • Too slow. 太慢了。 Feels (very) stiff. 感觉(非常)僵硬。
  • How fast does it need to be? 它需要多快?
    • The software mustn't feel slow. 软件一定不要感觉慢。
  • Why is it important? 它为什么如此重要?
    • User friendliness of software. 用户友好的软件。
  • What does the hardware look like? 硬件是什么样的?
    • Any random PC. 任何随机PC。
  • Why can't you solve it with better hardware? 为什么你不能用更好的硬件解决它?
    • It's just software that runs on Windows machines. 它只是在Windows机器上运行的软件。 You won't goy and buy a new PC for a random software that runs slow on your old ? 您不会为购买旧PC上的随机软件购买新PC吗?

Get a better video card! 获得更好的视频卡!

The whole point of the GDI, whether you access it from native code or via .Net, is that it abstracts the details of the graphics subsystem. 无论您是从本机代码还是通过.Net访问它,GDI的重点在于它抽象出图形子系统的细节。 The downside being that the low level operations such as blitting are in the hands of the graphic driver writers. 缺点是blitting等低级操作掌握在图形驱动程序编写者手中。 You can safely assume that these are as optimised as it's possible to get (after all, a video card manufacturer wants to make their card look the best). 你可以放心地假设这些都是尽可能优化的(毕竟,视频卡制造商希望让他们的卡看起来最好)。

The overhead of using the .Net wrappers instead of using the native calls directly pale into insignificance compared to the time spent doing the operation itself, so you're not going to gain much really. 与操作本身所花费的时间相比,使用.Net包装器而不是使用本机调用的开销直接变得微不足道,因此您不会获得太多的收益。

Your code is doing a non-scaling, no blending copy which is possibly the fastest way to copy images. 您的代码正在进行非缩放,没有混合副本,这可能是复制图像的最快方法。

Of course, you should be profiling the code to see what effect any changes you do to the code is having. 当然,您应该对代码进行概要分析,以了解您对代码所做的任何更改会产生什么影响。

The question then is why are you copying such large images from one window to another? 那么问题是你为什么要将这么大的图像从一个窗口复制到另一个窗口? Do you control the contents of both windows? 你控制两个窗口的内容吗?

Update 更新

If you control both windows, why not draw to a single surface and then blit that to both windows? 如果您控制两个窗口,为什么不绘制到单个表面,然后将其blit到两个窗口? This should replace the often costly operation of reading data from the video card (ie blitting from one window to another) with two write operations. 这应该取代通过两次写入操作从视频卡读取数据(即从一个窗口到另一个窗口的blitting)的通常昂贵的操作。 It's just a thought and might not work but you'd need timing data to see if it makes any difference. 这只是一个想法,可能不起作用,但你需要时间数据,看看它是否有任何区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM