Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VB.NET webbrowser DrawToBitmap not function - help

Posted on 2011-09-25
17
Medium Priority
?
2,191 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 

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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

721 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