Solved

VB.NET webbrowser DrawToBitmap not function - help

Posted on 2011-09-25
17
2,083 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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

Industry Leaders: 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!

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

734 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