如何获得Windows窗体面板与C#中的位图? [英] How to get a Windows Forms panel as a bitmap with C#?

查看:238
本文介绍了如何获得Windows窗体面板与C#中的位图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有与它的几个控制面板Windows窗体。该小组不占用所有形式的空间,但只有一小部分。如果有一些方法来检索面板的显示器(包括所有子控件)作为位图我想知道的是。就像一个截图,但只有裁剪到面板上。

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.

我没有寻找到屏幕捕获,因为面板的滚动控制(DevX控制)内,从而有时它可能不会完全可见的,我需要它的视觉重新presentation是否可见或不可见。

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.

这是可能的呢?

编辑:

好了,现在看来,这是为我担心。与DrawToBitmap()建议的解决方案只绘制可见控件的一部分。我已经使用DisplayRectangle中检索的完全控制权的大小。矩形是确定的,而现在的位图是完全控制的大小,但是这是不可见的控制部分是透​​明的位图,不显示是在控制的看不见的部分控制。

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?

推荐答案

当然,只要使用面板的DrawToBitmap()方法。它并不是100%可靠,可能有不支持它的面板上的控制。 RichTextBox的一样,web浏览器或某种第三方ActiveX控件。

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;
    }

这篇关于如何获得Windows窗体面板与C#中的位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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