如何使用 C# 将 Windows 窗体面板作为位图获取?

发布于 2024-09-08 17:34:57 字数 396 浏览 9 评论 0原文

我有一个 Windows 窗体,其中一个面板上有多个控件。面板并不占据所有的表单空间,而只占据一小部分。我想知道是否有某种方法可以将面板(带有所有子控件)的显示检索为位图。就像屏幕截图一样,但仅裁剪到面板。

我没有查看屏幕截图,因为该面板位于可滚动控件(DevX 控件)内,因此有时它可能不完全可见,我需要其视觉表示(无论是否可见)。

这有可能吗?

编辑:

好吧,现在看来正如我所担心的那样。建议使用 DrawToBitmap() 的解决方案仅绘制控件的可见部分。我使用 DisplayRectangle 来检索完整控件的大小。矩形就可以了,现在位图是完整控件的大小,但是控件不可见的部分在位图上是透明的,不显示控件不可见部分上的控件。

有没有机会完全渲染出来?

I have a Windows form with a panel with several controls on it. The panel does not take up all of the form space, but only a small part. What I want to know is if there is some way to retrieve the display of the panel (with all child controls) as a bitmap. Like a screenshot but only cropped to the panel.

I am not looking into the screen capture because the panel is within the scrollable control (DevX controls) so at times it may not be fully visible and I need its visual representation whether visible or not.

Is this possible at all?

Edit:

Well, now it seems that it was as I feared. The suggested solution with DrawToBitmap() only draws the part of the control that is VISIBLE. I have used DisplayRectangle to retreive the size of the complete control. The rectangle is ok, and now the bitmap is the size of the complete control, but the part of the control that is NOT VISIBLE is TRANSPARENT on the bitmap, not displaying the controls that are on the invisible part of the control.

Is there any chance for this to be rendered completely?

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

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

发布评论

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

评论(1

如梦 2024-09-15 17:34:57

当然,只需使用面板的 DrawToBitmap() 方法即可。它不是 100% 可靠,面板上可能有不支持它的控件。例如 RichTextBox、WebBrowser 或某种第 3 方 ActiveX 控件。

    private static Image PanelToBitmap(Control pnl) {
        var bmp = new Bitmap(pnl.Width, pnl.Height);
        pnl.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
        return bmp;
    }

Sure, just use the panel's DrawToBitmap() method. It isn't 100% reliable, there might be controls on the panel that don't support it. Like RichTextBox, WebBrowser or some kind of 3rd party ActiveX control.

    private static Image PanelToBitmap(Control pnl) {
        var bmp = new Bitmap(pnl.Width, pnl.Height);
        pnl.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
        return bmp;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文