Solved

VB.NET webbrowser DrawToBitmap not function - help

Posted on 2011-09-25
17
1,971 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
Comment Utility
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
Comment Utility
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
Comment Utility
0
 
LVL 13

Expert Comment

by:Chris Raisin
Comment Utility
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
Comment Utility
0
 
LVL 13

Expert Comment

by:Chris Raisin
Comment Utility
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
Comment Utility
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
Comment Utility
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:kidd12
Comment Utility
my friend i am need for vb.net
0
 
LVL 13

Expert Comment

by:Chris Raisin
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now