Home All Groups Group Topic Archive Search About

Save graphics image to bitmap?

Author
8 Apr 2006 12:56 AM
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.

Author
8 Apr 2006 7:24 AM
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

Show quoteHide quote
"dgk" <NoWh***@MailsAnonymous.com> schreef in bericht
news:9e1e32hgu80me8q6q2k671rn8584k5p8u1@4ax.com...
> 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.
Author
8 Apr 2006 10:44 PM
Herfried K. Wagner [MVP]
"Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> schrieb:
> 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.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
8 Apr 2006 10:40 AM
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
-------------------
Show quoteHide quote
"dgk" <NoWh***@MailsAnonymous.com> wrote in message
news:9e1e32hgu80me8q6q2k671rn8584k5p8u1@4ax.com...
> 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.
Author
8 Apr 2006 1:31 PM
dgk
On Sat, 8 Apr 2006 06:40:07 -0400, "Ken Tucker [MVP]"
<vb***@bellsouth.net> wrote:

>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?
Author
8 Apr 2006 4:25 PM
Homer J Simpson
"dgk" <NoWh***@MailsAnonymous.com> wrote in message
news:q4ef32tedskoev545kjo24omdnb4fl829u@4ax.com...

> 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> ??
Author
8 Apr 2006 8:35 PM
dgk
On Sat, 08 Apr 2006 16:25:37 GMT, "Homer J Simpson"
<nob***@nowhere.com> wrote:

Show quoteHide quote
>
>"dgk" <NoWh***@MailsAnonymous.com> wrote in message
>news:q4ef32tedskoev545kjo24omdnb4fl829u@4ax.com...
>
>> 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> ??
>
>
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.
Author
8 Apr 2006 9:46 PM
Homer J Simpson
"dgk" <NoWh***@MailsAnonymous.com> wrote in message
news:7l6g32hn31ekj4v91n7rsgqioe657ko1h0@4ax.com...

> 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.
Author
9 Apr 2006 8:53 PM
dgk
On Sat, 08 Apr 2006 21:46:39 GMT, "Homer J Simpson"
<nob***@nowhere.com> wrote:

Show quoteHide quote
>
>"dgk" <NoWh***@MailsAnonymous.com> wrote in message
>news:7l6g32hn31ekj4v91n7rsgqioe657ko1h0@4ax.com...
>
>> 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.
>
>
I fear so. I think that I might have to dust off an Appleman book that
I thought was safely locked away.
Author
9 Apr 2006 1:25 PM
Ken Tucker [MVP]
Hi,

          Did you try saving the documentstream?

Ken
------------------

Show quoteHide quote
"dgk" wrote:

> On Sat, 08 Apr 2006 16:25:37 GMT, "Homer J Simpson"
> <nob***@nowhere.com> wrote:
>
> >
> >"dgk" <NoWh***@MailsAnonymous.com> wrote in message
> >news:q4ef32tedskoev545kjo24omdnb4fl829u@4ax.com...
> >
> >> 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> ??
> >
> >
> 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.
>
>
>
Author
9 Apr 2006 8:51 PM
dgk
On Sun, 9 Apr 2006 06:25:02 -0700, Ken Tucker [MVP]
<KenTucker***@discussions.microsoft.com> wrote:

>Hi,
>
>          Did you try saving the documentstream?
>
>Ken
>------------------
>
>"dgk" wrote:
>

Not until you mentioned it, but it works the same as DocumentText; no
images are saved.
Author
10 Apr 2006 5:56 PM
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
Author
11 Apr 2006 3:11 AM
Homer J Simpson
"BK" <bkunn***@hotmail.com> wrote in message
news:1144691803.143731.305520@j33g2000cwa.googlegroups.com...

> 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
Author
9 Apr 2006 1:21 PM
Ken Tucker [MVP]
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
----------------

Show quoteHide quote
"dgk" wrote:

> On Sat, 8 Apr 2006 06:40:07 -0400, "Ken Tucker [MVP]"
> <vb***@bellsouth.net> wrote:
>
> >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?
>
>
>
Author
9 Apr 2006 6:42 PM
Homer J Simpson
"Ken Tucker [MVP]" <KenTucker***@discussions.microsoft.com> wrote in message
news:6B851C22-749D-4783-A4F0-3E0B762C6DD1@microsoft.com...

> 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.
Author
10 Apr 2006 4:51 PM
dgk
On Sun, 9 Apr 2006 06:21:01 -0700, Ken Tucker [MVP]
<KenTucker***@discussions.microsoft.com> wrote:

>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.