Solved

VB.NET webbrowser DrawToBitmap not function - help

Posted on 2011-09-25
17
2,039 Views
Last Modified: 2012-05-12
hello friend i am need save webbrowser image to c:\, i am trying this
 Private Function CreateScreenshot(ByVal Control As Control) As Bitmap
        Dim Screenshot As New Bitmap(WebBrowser1.Width, WebBrowser1.Height)
        Control.DrawToBitmap(Screenshot, New Rectangle(0, 0, WebBrowser1.Width, WebBrowser1.Height))
        Return Screenshot
    End Function
Dim bm As Bitmap
        bm = CreateScreenshot(Me)
        bm.Save("c:\imagem.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
in some websites work and in oter websites like google.com the image is blank, what is this, please help me for resolve this, the imge always blank.
0
Comment
Question by:kidd12
  • 8
  • 5
  • 2
  • +1
17 Comments
 
LVL 9

Expert Comment

by:raysonlee
ID: 36597209
Perhaps WebBrowser1 does not have focus when you save the screenshot. Try set focus to it by:
WebBrowser1.focus()  
bm=CreateScreenshot(WebBrowser1)
0
 

Author Comment

by:kidd12
ID: 36597333
not function, i set createscreenshot to webbrowser1 and set focus and always the image is blank in www.google.com
0
 
LVL 9

Expert Comment

by:raysonlee
ID: 36597947
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 13

Expert Comment

by:Chris Raisin
ID: 36600840
Have a look at my solution I gave for this problem for a VB6 user.



Although the coding is VB6, the methodology is the same.

You basically have to copy the webbrowser image to the clipboard, then copy the clipboard handle to a picturebox's image property, then load the picture into the picturebox from its image value, then save the picturebox image to a file (Whew!)

It took me ages to get it to work....I kept getting blank images too using any other method.

I hope this puts you on the right track.
The answer I have given is here:

http://www.experts-exchange.com/expertsZone.jsp?filterCreated=true&listType=1&listid=44988

0
 
LVL 13

Expert Comment

by:Chris Raisin
ID: 36600858
0
 
LVL 13

Expert Comment

by:Chris Raisin
ID: 36600874
Only look at the code from comment 36600486 onwards, since earlier code turned out not to what we were after.

There is probably another way to do it in VB.Net, but at least the methodolgy might lead you to succeeding using your VB.Net coding.

I could not find anywhere on the Internet where this has been solved before.

Cheers
Chris
(craisin)
0
 

Author Comment

by:kidd12
ID: 36601970
please my friend, pass this to vb.net for me, i am tryed in my application in vb.net and got a lot of errors, i am using visual basic express 2008. i am need code in visual express 2008.
thanksss
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36602102
0
 

Author Comment

by:kidd12
ID: 36602127
my friend i am need for vb.net
0
 
LVL 13

Expert Comment

by:Chris Raisin
ID: 36707760
Does the following code improve things?
Option Explicit On

Public Class Form1
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim bm As System.Drawing.Bitmap
        Me.WebBrowser1.Size = ClientSize
        Me.WebBrowser1.Navigate("www.google.com", False)
        Do While Me.WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
            Application.DoEvents()
        Loop
        bm = CreateScreenshot(Me.WebBrowser1)
        bm.Save("c:\imagem.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
        End
    End Sub
    Private Function CreateScreenshot(WebBrowser1 As WebBrowser) As Bitmap
        Dim Screenshot As New Bitmap(WebBrowser1.Width, WebBrowser1.Height)
        WebBrowser1.DrawToBitmap(Screenshot, New Rectangle(0, 0, WebBrowser1.Width, WebBrowser1.Height))
        Return Screenshot
    End Function

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    End Sub

  


End Class

Open in new window

0
 
LVL 13

Expert Comment

by:Chris Raisin
ID: 36707794
Success!

Please ignore previous code, since I needed to change two lines in it
(the width and height of the browser).

Fully successful methodology shows in following code.

Hope you are pleased with the result.

Cheers
Chris
(craisin)
Public Class Form1

    Private Property AccessibleNavigation As Point

    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Dim bm As System.Drawing.Bitmap
        Me.WebBrowser1.Height = Screen.GetWorkingArea(AccessibleNavigation).Height
        Me.WebBrowser1.Width = Screen.GetWorkingArea(AccessibleNavigation).Width
        Me.WebBrowser1.Navigate("www.google.com", False)
        Do While Me.WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
            Application.DoEvents()
        Loop
        bm = CreateScreenshot(Me.WebBrowser1)
        bm.Save("c:\imagem.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
        End
    End Sub
    Private Function CreateScreenshot(WebBrowser1 As WebBrowser) As Bitmap
        Dim Screenshot As New Bitmap(WebBrowser1.Width, WebBrowser1.Height)
        WebBrowser1.DrawToBitmap(Screenshot, New Rectangle(0, 0, WebBrowser1.Width, WebBrowser1.Height))
        Return Screenshot
    End Function

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

    End Sub

  


End Class

Open in new window

0
 

Author Comment

by:kidd12
ID: 36708115
my friend i am trying use this and the google page appear blank on image, save the image blank.
0
 
LVL 13

Expert Comment

by:Chris Raisin
ID: 36709844
Well it appears on my machine just fine.....Your problem is in the allowance of time for the browser to refresh fully before your code moves on.

Can you please post your amended code below and I will see how it differs from mine.


Cheers
Chris
(craisin)
0
 

Author Comment

by:kidd12
ID: 36713229
my code is
Private Function CreateScreenshot(ByVal WebBrowser1 As WebBrowser) As Bitmap
        Dim Screenshot As New Bitmap(WebBrowser1.Width, WebBrowser1.Height)
        WebBrowser1.DrawToBitmap(Screenshot, New Rectangle(0, 0, WebBrowser1.Width, WebBrowser1.Height))
        Return Screenshot
    End Function
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Dim bm As System.Drawing.Bitmap
        bm = CreateScreenshot(Me.WebBrowser1)
        bm.Save("c:\imagem.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
    End Sub
my friend i am need screenshot of page after fully loaded. thanks
0
 
LVL 13

Accepted Solution

by:
Chris Raisin earned 500 total points
ID: 36714302
You need to change the code in the subroutine "Button6_Click" to the follwoing.

It works flawlessly for me.

You currently will always get a blank screen because you are not allowing the browser to fully refresh itself.

Also, the browser cannot be


Private Sub Button6_Click(sender As System.Object, e As System.EventArgs) Handles Button6.Click
        Dim bm As System.Drawing.Bitmap
        Me.WebBrowser1.Height = Screen.GetWorkingArea(AccessibleNavigation).Height
        Me.WebBrowser1.Width = Screen.GetWorkingArea(AccessibleNavigation).Width
        'you may comment out the following line
        'provided your browser has already has a url loaded
        'otherwise you will need to amend the URL as needed
        Me.WebBrowser1.Navigate("www.google.com", False)
        Me.Webbrowser1.refresh() 
        Do While Me.WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
            Application.DoEvents()
        Loop
        bm = CreateScreenshot(Me.WebBrowser1)
        'you can change the filename here as well when needed
        bm.Save("c:\imagem.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
     'The following line may be commented out
        MessageBox.Show("Please now look at file C:\Imagem.jpg")     
    End Sub

Open in new window

0
 
LVL 13

Expert Comment

by:Chris Raisin
ID: 36714349
OOps, sorry about the broken sentence abve. I was going to say that the WebBrowser cannot be "hidden", but I was wrong!

I just ran it successfully with the Webbrowser's "Visible" property set to "False" and it worked! (much easier than the VB6 version!)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question