Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2254
  • Last Modified:

VB.NET webbrowser DrawToBitmap not function - help

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
kidd12
Asked:
kidd12
  • 8
  • 5
  • 2
  • +1
1 Solution
 
raysonleeCommented:
Perhaps WebBrowser1 does not have focus when you save the screenshot. Try set focus to it by:
WebBrowser1.focus()  
bm=CreateScreenshot(WebBrowser1)
0
 
kidd12Author Commented:
not function, i set createscreenshot to webbrowser1 and set focus and always the image is blank in www.google.com
0
 
raysonleeCommented:
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Chris Raisin(Retired Analyst/Programmer)Commented:
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
 
Chris Raisin(Retired Analyst/Programmer)Commented:
0
 
Chris Raisin(Retired Analyst/Programmer)Commented:
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
 
kidd12Author Commented:
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
 
kidd12Author Commented:
my friend i am need for vb.net
0
 
Chris Raisin(Retired Analyst/Programmer)Commented:
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
 
Chris Raisin(Retired Analyst/Programmer)Commented:
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
 
kidd12Author Commented:
my friend i am trying use this and the google page appear blank on image, save the image blank.
0
 
Chris Raisin(Retired Analyst/Programmer)Commented:
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
 
kidd12Author Commented:
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
 
Chris Raisin(Retired Analyst/Programmer)Commented:
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
 
Chris Raisin(Retired Analyst/Programmer)Commented:
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 8
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now