Save handle's image to file

Toveling
07-01-2006, 12:54 AM
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)

hBitmap.Save("C:\test.bmp")


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

jo0ls
07-03-2006, 06: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)
bm.Save("c:\woop.bmp")
g.Dispose()
bm.Dispose()

Toveling
07-06-2006, 09: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
browserCtrl.Navigate(sUrl)

While (browserCtrl.IsBusy = True)
Application.DoEvents()
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)

hBitmap.Save("C:\test.png")
oGraphics.Dispose()
hBitmap.Dispose()
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

Cags
08-14-2006, 06: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.

Toveling
08-14-2006, 11: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)
oBitmap.Save("C:\test.png")
oBitmap.Dispose()

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum