C#:需要在“想象的”对象上渲染表单/控件桌面

发布于 2024-08-19 20:22:09 字数 428 浏览 3 评论 0原文

好吧,这就是我想做的。首先,我将解释我想要实现的最终结果,以防有其他关于如何做到这一点的想法。

我正在制作一个屏幕捕获实用程序,它只拍摄一个窗口的屏幕截图......我的窗口(我对其有完全的编程控制)。但是,该窗口可能比运行该实用程序的计算机桌面大得多。特别是在分辨率为 1024x768 的计算机上,高度可能达到数千像素。

所以我试图捕获整个窗口,即使它比屏幕大得多。这就是我想要达到的最终结果。

对此的一个假设解决方案是在某种图形或屏幕对象上呈现表单/控件,并从该对象上截取屏幕截图,而不是拍摄物理桌面的屏幕截图。

本质上,我需要在一个只存在于代码和内存中的假想屏幕上绘制控件,我什至不知道要搜索什么,所以即使是关于将什么放入 Google(真正的搜索引擎)中的想法也会有所帮助。

有什么想法吗?提前致谢!

编辑:我正在使用WinForms。

Okay, here's what I'm trying to do. First I'll explain the end result I'm trying to achieve in case there are other ideas on how to do this.

I'm making a screen capture utility that takes a screen shot of only one window... my window (which I have total programmatic control over). However, this window may be much larger than the desktop of the computer on which the utility will run. The height, in particular, may reach several thousand pixels on a computer with 1024x768 resolution.

So I'm trying to capture the full window even though it's much larger than the screen. That's the end result I'm trying to achieve.

One hypothetical solution to this is to render the form/control on a graphics or screen object of some sort, and take the screen shot off of that object, instead of taking a screen shot of the physical desktop.

Essentially I need to draw controls on an imaginary screen that exists only in code and memory and I don't even know what to search for, so even ideas on what to put into Google (the TRUE search engine) would be helpful.

Any ideas? Thanks in advance!

EDIT: I'm using WinForms.

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

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

发布评论

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

评论(2

皇甫轩 2024-08-26 20:22:09

您没有提及您的 C# 应用程序使用哪种技术,我假设它是 WinForms 或 WPF。

如果您的实现使用 WPF,您可以简单地将其渲染为 DrawingImage具有正确的尺寸 - 甚至使用 WPF 的打印功能将窗口的内容“打印”到内存中的图像。 这是一个在 WPF 中打印的不错示例,您可以对其进行调整(如果您使用的是 WPF)。

对于 WinForms 应用程序,这有点棘手,因为 WinForm 控件在更高分辨率下并不总是能够很好地缩放,并且可能会出现对齐问题。 这是一个描述将 WinForm 屏幕打印到图像的链接。它演示了打印 UserControl,但您应该能够根据您的目的调整实现。

You didn't mention which technology your C# application uses, I'm assuming it's either WinForms or WPF.

If your implementation uses WPF, you could simply render it to a DrawingImage with the right dimensions - or even use the printing capabilities of WPF to "print" the contents of the window to an image in memory. Here's a decent example of printing in WPF that you may be able to adapt (if you're using WPF).

With a WinForms application, it is a bit trickier, because WinForm controls don't always scale well under higher resolutions, and can exhibit alignment problems. Here's a link that describes printing a WinForm screen to an image. It demonstrates printing a UserControl, but you should be able to adapt the implementation for your purposes.

梦屿孤独相伴 2024-08-26 20:22:09

嗯,这很奇怪。你真的写过这个表格吗? Form 类极其坚持它的 Size 永远不能大于屏幕。我从未找到解决此问题的方法,也从未在 WF 相关论坛中看到过该解决方法。

无论如何,您无法制作屏幕截图,因为您没有足够的屏幕。唯一的其他选项是 Control.DrawToBitmap()。

当您尝试创建位图时,“几千个像素”可能会让您在 32 位操作系统上遇到 OutOfMemory 异常的麻烦。不是因为您没有足够的内存,而是因为虚拟内存地址空间中没有足够大的空洞来容纳位图。唯一好的解决方法是 64 位操作系统。

Hmm, that's very odd. Have you actually written this form yet? The Form class is extremely insistent that its Size can never be larger than the screen. I've never found a workaround for this and have never seen one posted in a WF related forum.

Anyhoo, you can't make a screen shot because you don't have enough screen. The only other option is Control.DrawToBitmap().

"Several thousands of pixels" is liable to get you into trouble with OutOfMemory exceptions on 32-bit operating systems when you try to create the bitmap. Not because you don't have enough memory but because there isn't an empty hole left in the virtual memory address space that is large enough to fit the bitmap The only good workaround for that is a 64-bit operating system.

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