1
Answer

Grabbing Bitmap Image of a Control

Roger Garrett

Roger Garrett

13y
2.4k
1
I have a control. It happens to be a webBrowser control, but the issue seems to apply to ANY control. I want to copy the image of that control (the "source") and transfer it into a PictureBox control (the "target"). I can do that using a bitblt, with code like the following:

[CODE]
          Bitmap controlBmp;            using (Graphics g1 = control.CreateGraphics())            {                controlBmp = new Bitmap(control.Width, control.Height, g1);                using (Graphics g2 = Graphics.FromImage(controlBmp))                {                    IntPtr dc1 = g1.GetHdc();                    IntPtr dc2 = g2.GetHdc();                    BitBlt(dc2, 0, 0, control.Width, control.Height, dc1, 0, 0, 13369376);                    g1.ReleaseHdc(dc1);                    g2.ReleaseHdc(dc2);                }            }            pictureBox1.Image = controlBmp;
[/CODE]
But the bitblt only operates on that portion of the source control that happens to be fully visible on the display screen. If any portion of the source control is offscreen then that offscreen portion appears blank when bitblt-ed and dislayed in the target control.

I tried Invalidating the source control and doing an Update on it, with the hopes that it would force a build of the entire image of the control, but I still get the offscreen portion appearing as blank in the resultant bitmap.

So, my question: How can I acquire the complete bitmap image of a control, even if some (or even ALL) of the control is not currently on the screen? 
An interesting side note: The webBrowser has an "unsupported" function called DrawToBitmap which actually works (sometimes) and can even get the entire image of the webBrowser EVEN WHEN the entire webBrowser control is not visible anywhere on the screen and is not even a child of the current form. So, it appears that at least under some circumstances it is indeed possible to get an image of an "offscreen" control.
So, how is it done?



Answers (1)