Save graphics image to bitmap?

D

dgk

I'm trying to save the image of a webbrowser control. Not a particular
image being shown on the page, but rather the visible display of the
webbrowser. Using VS2005 I figured that maybe DrawToBitmap would work,
but it appears that the webbrowser doesn't implement it.

So I looked around and found that this code:

Dim g As Graphics
g.CopyFromScreen(Me.wb1.PointToScreen(New Point(0, 0)), New
Point(0, 0), wb1.Size)

should get the image into a graphics object. What I can't figure out
is how to get that into a bitmap that I can hang on to. This:

dim b as Bitmap = g.???

doesn't appear to be the correct approach; at least nothing works.

Is there some way to do this or am I going about it wrong? I can use
SnagIt or even PrintScreen to dump it into the clipboard so there must
be some way to get at it. Any help greatly appreciated.
 
C

Cor Ligthert [MVP]

dgk,

A image in a webpage (therefore as well in the webbrowser) has to be forever
an image (or Java, Flash program or whatever related for web) as an url.

Therefore working with bitmaps in a webbrowser is withouth any sense.

You have to set the picture (url) too an absolute place in the browser or
create first something as a table to fix it on its place.

I hope this helps,

Cor
 
K

Ken Tucker [MVP]

Hi,

Dock the webbrowser control in a panel. Use the panels drawtobitmap
method.

Dim bmWb As New Bitmap(Panel1.Width, Panel1.Height)
Panel1.DrawToBitmap(bmWb, Panel1.ClientRectangle)

PictureBox2.Image = bmWb


Ken
 
D

dgk

Hi,

Dock the webbrowser control in a panel. Use the panels drawtobitmap
method.

Dim bmWb As New Bitmap(Panel1.Width, Panel1.Height)
Panel1.DrawToBitmap(bmWb, Panel1.ClientRectangle)

PictureBox2.Image = bmWb


Ken

That was clever but it doesn't work; all I get is a white screen. I
then tried getting rid of the panel and using the form, but I get the
whole form with a big white space where the webbrowser is. I guess
that's why I was having so much trouble doing it straight; the
webbrowser image is sort of outside the scope of the project.

It appears that this is not going to be trivial. Any other ideas?
 
H

Homer J Simpson

That was clever but it doesn't work; all I get is a white screen. I
then tried getting rid of the panel and using the form, but I get the
whole form with a big white space where the webbrowser is. I guess
that's why I was having so much trouble doing it straight; the
webbrowser image is sort of outside the scope of the project.

It appears that this is not going to be trivial. Any other ideas?

You are trying to simulate <CTRL>-<PRINT SCREEN> or <PRINT SCREEN> ??
 
D

dgk

You are trying to simulate <CTRL>-<PRINT SCREEN> or <PRINT SCREEN> ??
Basically, except that I only want the browser window. My app has the
user go online to a site, and they should be able to capture images of
what they've done as part of the documentation of the process. These
are sites that I have no control over.

I'm thinking that I might need to figure out Bitblt and I've been
poking around at pinvoke.net
(http://www.pinvoke.net/default.aspx/gdi32.BitBlt) but haven't managed
to make it work yet. It doesn't blow up, it just doesn't work.

I also tried grabbing the webbrowser documenttext and just setting it
when I want to recreate the image but that doesn't include any images
in the page. Then I tried just saving the whole document into an
htmldocument object - I had high hopes for this, but
WebBrowser.Document is readonly.

I know it can be done. Snagit does it, printscreen does it without any
granularity, so I can do it. I just need to figure out how to write a
function that takes a webbrowser as a parameter and returns a bitmap.
That sounds easy. I figured that it would take ten minutes and I've
been working on it on and off for three weeks.
 
H

Homer J Simpson

I know it can be done. Snagit does it, printscreen does it without any
granularity, so I can do it. I just need to figure out how to write a
function that takes a webbrowser as a parameter and returns a bitmap.
That sounds easy. I figured that it would take ten minutes and I've
been working on it on and off for three weeks.

Have you looked at something like "Programming Windows with C#" by Charles
Petzold or one of his similar books? Maybe even one for Win95/98 etc? I
suspect you MAY be in that territory.
 
H

Herfried K. Wagner [MVP]

Cor Ligthert said:
A image in a webpage (therefore as well in the webbrowser) has to be
forever an image (or Java, Flash program or whatever related for web) as
an url.

The OP wants to create a screenshot of the page being shown in the
webbrowser control and save it to a bitmap, IMO.
 
Joined
Apr 6, 2006
Messages
3
Reaction score
0
hi dkg and MVPs,

Flame me to cinders if I'm wrong, but aren't people missing the point of your query?

You asked why this doesn't work:
Dim g As Graphics
g.CopyFromScreen(Me.wb1.PointToScreen(New Point(0, 0)), New
Point(0, 0), wb1.Size)
....
dim b as Bitmap = g.???

What you have to do is this:

Dim b as New Bitmap(w,h)
Dim g as Graphics = Graphics.FromImage(b)
g.CopyFromScreen... and so on

This had me puzzled for quite a while because it looks as though you are defining a graphics object as a copy of the bitmap. What Graphics.FromImage actually does is define the graphics object as a kind of pointer to the bitmap. Then anything you do to the Graphics object (DrawLine, CopyFromScreen... whatever) automatically updates the bitmap itself.

Since this is rather counterintuitive, I'm surprised nobody ever sees fit to mention it.

all the best. Vic (aka Boops_Boops)
 
G

Guest

Hi,

I tested it with the msdn home page and it worked. I did not try
this with a flash site so I do not know how this will work with flash. Are
you sure the document completed loading before you got the bitmap. The
webbrowser has a documentcompleted event you should get the image then.

Ken
----------------
 
H

Homer J Simpson

Hi,

I tested it with the msdn home page and it worked. I did not
try
this with a flash site so I do not know how this will work with flash.
Are
you sure the document completed loading before you got the bitmap. The
webbrowser has a documentcompleted event you should get the image then.

The only thing I've been able to do with flash is, if it supports printing,
print to PrimoPDF.
 
D

dgk

Have you looked at something like "Programming Windows with C#" by Charles
Petzold or one of his similar books? Maybe even one for Win95/98 etc? I
suspect you MAY be in that territory.
I fear so. I think that I might have to dust off an Appleman book that
I thought was safely locked away.
 
D

dgk

Hi,

I tested it with the msdn home page and it worked. I did not try
this with a flash site so I do not know how this will work with flash. Are
you sure the document completed loading before you got the bitmap. The
webbrowser has a documentcompleted event you should get the image then.

Ken

Odd, I tried it with Google and it didn't work. I'm pretty sure the
site finished loading - I'm not keying off any WebBrowser event, I'm
doing it when the user presses a button on the form containing the
WebBrowser. I'll give it another chance when I get home.
 
B

BK

This is something I found on the web a while back, I use it to print
screen shots from within my VB apps. It takes a window and converts it
to a bitmap image using BitBlt (as you mentioned above). It works
great for me and it seems like you could modify it for your purposes.
Hope this helps.

At the top of the form:

Private Declare Auto Function BitBlt Lib "gdi32.dll" (ByVal hdcDest
As IntPtr, ByVal nXDest As Integer, ByVal nYDest As Integer, ByVal
nWidth As Integer, ByVal nHeight As Integer, ByVal hdcSrc As IntPtr,
ByVal nXSrc As Integer, ByVal nYSrc As Integer, ByVal dwRop As
System.Int32) As Boolean
Private Const SRCCOPY As Integer = &HCC0020

Then, in the method where I want to print (modify this to save easily
enough), I have the following snippet:

' Copy the form's image into a bitmap.
m_PrintBitmap = GetFormImage()

' Make a PrintDocument and print.
m_PrintDocument = New PrintDocument
m_PrintDocument.Print()

And finally, the GetFormImage function (that does the heavy lifting) :

Private Function GetFormImage() As Bitmap
' Get this form's Graphics object.
Dim me_gr As Graphics = Me.CreateGraphics

' Make a Bitmap to hold the image.
Dim bm As New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height,
me_gr)
Dim bm_gr As Graphics = me_gr.FromImage(bm)
Dim bm_hdc As IntPtr = bm_gr.GetHdc

' Get the form's hDC. We must do this after
' creating the new Bitmap, which uses me_gr.
Dim me_hdc As IntPtr = me_gr.GetHdc

' BitBlt the form's image onto the Bitmap.
BitBlt(bm_hdc, 0, 0, Me.ClientSize.Width, Me.ClientSize.Height,
_
me_hdc, 0, 0, SRCCOPY)
me_gr.ReleaseHdc(me_hdc)
bm_gr.ReleaseHdc(bm_hdc)

' Return the result.
Return bm
End Function

Bill
 
H

Homer J Simpson

This is something I found on the web a while back, I use it to print
screen shots from within my VB apps. It takes a window and converts it
to a bitmap image using BitBlt (as you mentioned above). It works
great for me and it seems like you could modify it for your purposes.
Hope this helps.

I note "How to: Copy Pixels for Reducing Flicker in Windows Forms" and
"CopyPixelOperation Enumeration" which relate to BitBlt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top