Save handle's image to file

06-30-2006, 11:54 PM
I'm writing a program that will be needing to save a number of different controls' current content to disk. Whatever that the object is (or, would be if it were visible) showing needs to be saved as the image. I'm new to .NET, but well versed in VB6.

The first control I'm doing is a standard VB .NET 2005 webbrowser, though it shouldn't matter what control.

'''cut: load page into webbrowser, wait for it to finish. lWidth = 800, lHeight 600
Dim oGraphics As System.Drawing.Graphics = System.Drawing.Graphics.FromHwnd(browserCtrl.Handle)
Dim hBitmap As New Bitmap(lWidth, lHeight)

oGraphics.DrawImageUnscaled(hBitmap, 0, 0)


It saves an 800x600 blank image currently. Any ideas in the correct direction would be helpful. Thanks!

07-03-2006, 05:03 AM
Hmm, most controls have a DrawToBitmap method, not the webbrowser though. So use Graphics.CopyFromScreen - which is a bitblt wrapper:

Dim bm As New Bitmap(browser.Width, browser.Height)
Dim g As Graphics = Graphics.FromImage(bm)
g.CopyFromScreen(Me.PointToScreen(browser.Location), New Point(0, 0), browser.Size)

07-06-2006, 08:11 PM
Thanks for the advice - that code works fine if the control is in view. I'm trying to not use any real controls to produce the image. If I can't get it to work, I'll just do it in VB6 - I know I can do it in about 10 minutes there.

Here's my (yet still broken, just makes a blank output) code:

Public Sub MakeImage( _
ByVal sFileName As String, ByVal sUrl As String, _
ByVal lWidth As Integer, Optional ByVal lHeight As Integer = 0)

Dim browserCtrl As New WebBrowser

If lHeight = 0 Then lHeight = 600

browserCtrl.Width = lWidth
browserCtrl.Height = lHeight

While (browserCtrl.IsBusy = True)
End While

Dim oGraphics As System.Drawing.Graphics = System.Drawing.Graphics.FromHwnd(browserCtrl.Handle)
Dim hBitmap As New Bitmap(lWidth, lHeight)

oGraphics.DrawImageUnscaled(hBitmap, 0, 0)

End Sub

Just wanted to note that I did try some other stuff, with normal BitBlt calls, but came up empty handed. My main obstacle seems to have been getting the contents of the graphics object into a bitmap so I can save it

08-14-2006, 05:09 AM
This is an oldish post I came across whilst searching the forum for something else. I just thought I'd clarify for anyone else that comes across it why the saved Image is blank.
'this line creates a graphics object which points at the control
Dim oGraphics As System.Drawing.Graphics = System.Drawing.Graphics.FromHwnd(browserCtrl.Handle)
' this line creates a blank image
Dim hBitmap As New Bitmap(lWidth, lHeight)
' this line draws the blank image to the control (not the other way around)
oGraphics.DrawImageUnscaled(hBitmap, 0, 0)
' saves the still blank image
hBitmap.Save("C:\test.png")To the best of my knowldge getting the image that a Graphics object is drawing to is not possible. It is a mechanism for writing to an image not reading from an image. Much as you would not expect a StreamWriter object to read a stream.

08-14-2006, 10:22 AM
I recently did another project (legacy VB) where I used GDI+ and now realize that.

The code I currently have uses some win32 api calls. Currently, instead of copying out of the browser's DC, it copies from the screen. So, I get basically a screenshot saved. Any help would be appreciated, I still want this to work.

Dim BrowserDC As IntPtr = GetDC(browserCtrl.Handle)

Dim hBitmap As IntPtr = CreateCompatibleBitmap(BrowserDC, lWidth, lHeight)
Dim hTempDC As IntPtr = CreateCompatibleDC(BrowserDC)
SelectObject(hTempDC, hBitmap)
'copy browser hDC into temp dc that has the hBitmap assosiated with it
BitBlt(hTempDC, 0, 0, lWidth, lHeight, BrowserDC, 0, 0, 13369376)

Dim oBitmap As Bitmap = Bitmap.FromHbitmap(hBitmap)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum