Get image of a control

  • Thread starter Thread starter MaxH
  • Start date Start date
M

MaxH

How can I get (and save) the image of a control as a bitmap
(or other graphics file format)?

Thank you.

Max
 
Hi Max,

The code below will capture the given Control's image and return a
BitMap.

This example captures the current Form (which is a Control itself)
Dim bmp As Bitmap
bmp = CaptureControl (Me)
bmp.Save ("FormX.bmp", Imaging.ImageFormat.Bmp)

Regards,
Fergus

<code>
Public Declare Function BitBlt Lib "gdi32" ( _
ByVal hDestDC As IntPtr, _
ByVal x As Integer, _
ByVal y As Integer, _
ByVal nWidth As Integer, _
ByVal nHeight As Integer, _
ByVal hSrcDC As IntPtr, _
ByVal xSrc As Integer, _
ByVal ySrc As Integer, _
ByVal dwRop As Integer _
) As Integer

'===================================================================
Public Function CaptureControl(ByVal c As Control) As Bitmap

Const SRCCOPY As Integer = &HCC0020

Dim bmp As Bitmap
Dim gDest, gSource As Graphics
Dim hdcSource, hdcDest As IntPtr

bmp = New Bitmap(c.Width, c.Height)

gSource = c.CreateGraphics
Try
gDest = Graphics.FromImage(bmp)
Try
hdcSource = gSource.GetHdc
Try
hdcDest = gDest.GetHdc
Try
BitBlt( _
hdcDest, 0, 0, _
c.Width, c.Height, _
hdcSource, 0, 0, SRCCOPY _
)
Finally
gDest.ReleaseHdc(hdcDest)
End Try
Finally
gSource.ReleaseHdc(hdcSource)
End Try
Finally
gDest.Dispose()
End Try
Finally
gSource.Dispose()
End Try

Return bmp
End Function
</code>
 
Thank you very much.

Is there a way to capture the image of the control also if
the control is not visible (or is partially covered by
another control)?


Thanks again.


Max
 
Hi Max,

We have a similar issue in the languages.vb group - when the app in
question doesn't have focus and is behind others. So far, with many things
tried, there's been no success. It seems that the Control must be on top and
visible in order to be captured.

If we get success there I'll post back here. If you get lucky first, it
would be great to hear from you! ;-)

Regards,
Fergus
 
We have a similar issue in the languages.vb group - when the app in
question doesn't have focus and is behind others. So far, with many things
tried, there's been no success. It seems that the Control must be on top and
visible in order to be captured.

If we get success there I'll post back here. If you get lucky first, it
would be great to hear from you! ;-)

Btw. how to capture whole screen or at least some (visible) window from
..NET?
 
Back
Top