C# Control 的屏幕截图是白色的

发布于 2024-12-07 02:42:48 字数 937 浏览 0 评论 0原文

我的问题与此相关:如何以编程方式获取 .Net WinForms 控件的屏幕截图?

我想获取 C# 中的 System.Windows.Forms.Control 的屏幕截图。我正在使用上面链接的问题中建议的 DrawToBitmap 方法,并且该方法在大多数情况下都有效。然而,也存在一些问题。

问题 1
我有两个标签页,我们称它们为A和B。我想要截屏的控件位于标签页B中。我想在按下标签页A中的按钮时截取屏幕截图。这在大多数情况下都有效,除非标签页 B 尚未被访问:那么屏幕截图只是白色的。如果我首先访问标签页 B,然后返回标签页 A 并单击按钮进行屏幕截图,那么它就可以正常工作。我猜这是因为选项卡中的某些控件加载或构建尚未完成,但我不确定到底是什么(或者可能完全是其他东西)。我一直在尝试使用 ResumeLayout、PerformLayout、Show、Update、Invalidate 强制加载或构建,但这不起作用。
编辑:通过在包含的选项卡页控件上使用DrawToBitmap而不是控件本身并在该选项卡页上执行显示来解决此问题。

问题2:
当我截取某个自定义控件(UserControl 的子类)的屏幕截图时,屏幕截图中会出现一个小的矩形白色区域(显然不应该有该区域)。矩形区域并不位于控件的某个特定部分(例如按钮或文本框),因此我不确定导致此问题的原因。在其他自定义控件(也是 UserControl 的子类)上,它工作正常,因此这本身不是问题。
编辑:解决了,有一个空控件被绘制在它上面。将该控件的 Visible 设置为 false 解决了这个问题。

My question is related to this one: How to get a screen capture of a .Net WinForms control programmatically?

I want to take a screenshot of a System.Windows.Forms.Control in C#. I'm using the DrawToBitmap method suggested in the question linked above and that works most of the time. However there are a few problems.

Problem 1:
I have two tabpages, let's call them A and B. The Control I want to take a screenshot of is in tabpage B. I want to take the screenshot when a button in tabpage A is pressed. This works most of the time, except when tabpage B hasn't been accessed yet: then the screenshot is just white. If I first access tabpage B, then go back to tabpage A and click the button to take the screenshot then it works fine. I'm guessing this is because of some loading or building of the control in the tab that hasn't been done yet, but I'm not sure what exactly (or it could be something else entirely). I've been trying to force that loading or building using ResumeLayout, PerformLayout, Show, Update, Invalidate but that doesn't work.
EDIT: Managed to solve this by using DrawToBitmap on the containing tabpage control instead of the Control itself and doing a Show on that tabpage.

Problem 2:
When I take a screenshot of a certain custom Control (a subclass of UserControl) there is a small rectangular white area in the screenshot (where there shouldn't be one obviously). The rectangular area isn't on one particular part of the control like a button or textbox so I'm not sure what's causing this. On other custom Controls (also subclasses of UserControl) it works fine, so that couldn't be the problem itself.
EDIT: Solved it, there was an empty control there that was being drawn on top of it. Setting Visible to false for that control solved it.

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

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

发布评论

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

评论(2

江南月 2024-12-14 02:42:48

你可以用这段代码来做,剩下的工作和找到正确的坐标是读者要做的功课。

int screenWidth = Screen.GetBounds(new Point(0, 0)).Width;
int screenHeight = Screen.GetBounds(new Point(0, 0)).Height;
Bitmap bmpScreenShot = new Bitmap(screenWidth, screenHeight);
Graphics gfx = Graphics.FromImage((Image)bmpScreenShot);
gfx.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, screenHeight));
bmpScreenShot.Save("test.jpg", ImageFormat.Jpeg);

You can do with this code, the rest of the work and finding the correct coordinates is for the reader to do the homework.

int screenWidth = Screen.GetBounds(new Point(0, 0)).Width;
int screenHeight = Screen.GetBounds(new Point(0, 0)).Height;
Bitmap bmpScreenShot = new Bitmap(screenWidth, screenHeight);
Graphics gfx = Graphics.FromImage((Image)bmpScreenShot);
gfx.CopyFromScreen(0, 0, 0, 0, new Size(screenWidth, screenHeight));
bmpScreenShot.Save("test.jpg", ImageFormat.Jpeg);
很酷又爱笑 2024-12-14 02:42:48

对于问题 1:如果“如果我首先访问选项卡页 B,然后返回选项卡页 A 并单击按钮进行屏幕截图,那么它就可以正常工作”,然后在 formloadevent 中添加模仿该操作的操作:

tabControl.SelectedIndex = IndexOfTabB;
tabControl.SelectedIndex = IndexOfTabA;

这是一个技巧,但它会起作用。

对于问题 2:您能否比较您的控件和它的屏幕截图的大小并向我们提供结果?如果 Control.Width 不等于位图屏幕的宽度,那么这就是一个真正的问题。

For problem 1: If "If I first access tabpage B, then go back to tabpage A and click the button to take the screenshot then it works fine" then add in formloadevent imitating of that actions:

tabControl.SelectedIndex = IndexOfTabB;
tabControl.SelectedIndex = IndexOfTabA;

That is trick but it will work.

For problem 2: Can you compare sizes of your control and screenshot of It and give results to us? If Control.Width is not equal wirdth of bitmap screen then It is a real problem.

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