views:

53

answers:

1

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?

+2  A: 

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;
    }
Hans Passant
Oh, dear, how I never noticed this method before :( I guess there was no need to... Thank you very much. I'm gonna hope it will work for all used controls (mostly edit boxes, checkboxes, groups, etc.) Thanks again.
kornelijepetak
That will work fine.
Hans Passant
for nonscrolling forms it works perfectly. When using Scrollable Control (DevX) it's as described in my edit.
kornelijepetak
I have no mental image of how you expect to stuff a 100 pound pig in a 20 pound poke.
Hans Passant
Shouldn't the control be able to draw its items even though they are currently out of view? They are not hidden (with its Visible set to false), they are simply not visible on screen because the scrollbar is set to some position. Or have I missed the point of your comment?
kornelijepetak
@kornelijepetak: try to use instead of `pnl.Width/Height` **`pnl.DisplayRectangle.Width/Height`** when you create a new bitmap.
serhio