Page 1 of 1

Get an image of a webpage Usefull for a speed-dial feature in a webbrowser... Rate Topic: -----

#1 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 183
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Posted 18 April 2010 - 05:09 AM

Note: I wrote this tutorial for those inexperienced in VB. More advanced users may find all the instructions annoying. Sorry :turned:

Hi, in this tutorial I'm going to show you how to retreive an image of a webpage from the internet, and then display it in a picturebox:

First start Visual Basic (or visual studio if you have it) and go to File -> New Project -> Windows Forms Application
Name you project whatever you want. I called it 'WebCapture'.

You should now see a blank windows form infront of you. Resize it to whatever size you want. To the left of your form you should see the toolbox. On the toolbox, locate the 'PictureBox' Control, and drag it onto your form. You should now have a form with a picturebox on it.

Next, click on the picturebox, and to the right you should see the properties of the picturebox. Change the name of the picturebox to wbImage. Now find the 'SizeMode' property of the picturebox, and set it to 'Stretch'

Next, add a webbrowser control to your form and set the visible property to false. Change the webbrowser's name to 'WbMain'.

Now add a textbox and a button to your form. Name the textbox 'txtURL', and name the button 'btnNavigate'.

Now double click btnNavigate, and put this code into the button click event:
'Tell the webbrowser to navigate to whatever URL is in the textbox
WbMain.Navigate(TxtUrl.text)
'Disable the navigate button for a second...
btnNavigate.Enabled = False



Now double click your 'WbMain', and go to the DocumentCompleted event. Add this code:

            'Declare two integers for the width and height of the webbrowser document    
            Dim scrollWidth As Integer
            Dim scrollHeight As Integer
            'Ser the integers to the size & height of the webbrowser document
            scrollHeight = wbMain.document.Body.ScrollRectangle.Height
            scrollWidth = wbMain.document.Body.ScrollRectangle.Width
            'Resize the webbrowser to the size of the page
            wbMain.Size = New Size(scrollWidth, scrollHeight)
            'Create a new bitmap the size of the webbrowser document
            Dim bm As New Bitmap(scrollWidth, scrollHeight)
            'Capture the snapshot
            WbMain.DrawToBitmap(bm, New Rectangle(0, 0, bm.Width, bm.Height))
            wbImage.Image = bm
            bm.Dispose()
            btnNavigate.Enabled = True



Now press F5 to begin debugging. Your form should show. Type a url into the textbox and click the button. A few seconds later (Depending on the speed of your internet connection) a snapshot of the page should appear in the picturebox.


So what did we actually do?

First we made the webbrowser navigate to the URL in the textbox, and then we made it take a snapshot of the document whenever it had finsished loadinf the page.

Is This A Good Question/Topic? 1
  • +

Replies To: Get an image of a webpage

#2 qtex  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 142
  • Joined: 24-October 09

Posted 21 April 2010 - 08:00 AM

Its easyer to do like that :

PictureBox1.ImageLocation = "http://files.uploadffs.com/d/1/0d661ebb/Lawn_Mower.png"


But all depends on situation

This post has been edited by qtex: 21 April 2010 - 08:01 AM

Was This Post Helpful? -1
  • +
  • -

#3 Jack Eagles1  Icon User is offline

  • Pugnacious Penguin (inspired by no2pencil)
  • member icon

Reputation: 183
  • View blog
  • Posts: 1,152
  • Joined: 10-December 08

Posted 22 April 2010 - 06:08 PM

Yes, but that's only going to get you an image off the internet. This way lets you capture an image of a whole webpage... They're different things.
Was This Post Helpful? 0
  • +
  • -

#4 NemY  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 27-January 10

Posted 20 November 2010 - 06:14 AM

I think Qtex read it as "Get an image off a webpage", thinking you were making a tutorial on how to show a certain image found on a webpage.


Thanks for the tutorial though, very helpful

This post has been edited by NemY: 20 November 2010 - 06:15 AM

Was This Post Helpful? 0
  • +
  • -

#5 chris_24  Icon User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 37
  • Joined: 29-November 10

Posted 15 December 2010 - 01:20 AM

if just wanting to get the image on the webpage, press printScreen.. :sweatdrop:
Was This Post Helpful? 0
  • +
  • -

#6 Merko  Icon User is offline

  • New D.I.C Head

Reputation: 10
  • View blog
  • Posts: 42
  • Joined: 23-November 10

Posted 22 December 2010 - 02:44 PM

View Postchris_24, on 15 December 2010 - 12:20 AM, said:

if just wanting to get the image on the webpage, press printScreen.. :sweatdrop:

If the webpage is large, you will only get image of what you are seeing.

If you use the other method, you will get the entire site in an image. with out all the toolbars etc.
Was This Post Helpful? 0
  • +
  • -

#7 SerokSimko  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-January 11

Posted 19 January 2011 - 08:50 AM

Hello and Thank you for this wonderful tutorial. I always wondered how some people take huge pictures of web pages.

Unfortunately there seems to be a problem with my code.
When I completed the coding according to your instructions, and started the debugging, I typed www.yahoo.com in textbox, pressed the button and an exception with message: 'Parameter is not valid' was shown as error.

I tried to put the code inside try catch block but still it gives me the same error. Actually it is not an error / warning / message since it shows 0 error, 0 warning, and 0 messages. Once I click the button during run-time, program hangs, goes back to Visual Studio, and then a pop-up shows, telling me parameter is not valid.

Kindly look into it, I just began to start programming for a while, please bear with me.

Public Class Form1

    Private Sub btnNavigate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNavigate.Click
        WbMain.Navigate(txtURL.Text)
        btnNavigate.Enabled = False
    End Sub

    Private Sub WbMain_DocumentCompleted(ByVal sender As Object, ByVal e As  _
                                         System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) _
                                         Handles WbMain.DocumentCompleted
        Try
            Dim scrollWidth As Integer
            Dim scrollHeight As Integer
            scrollWidth = WbMain.document.Body.ScrollRectangle.Width
            scrollHeight = WbMain.document.Body.ScrollRectangle.Height
            WbMain.Size = New Size(scrollWidth, scrollHeight)
            Dim bm As New Bitmap(scrollWidth, scrollHeight)
            WbMain.DrawToBitmap(bm, New Rectangle(0, 0, bm.Width, bm.Height))
            wbImage.Image = bm
            bm.Dispose()
            btnNavigate.Enabled = True
        Catch ex As Exception

        End Try

    End Sub
End Class




Here is the exception:
System.ArgumentException was unhandled
Message="Parameter is not valid."
Source="System.Drawing"
StackTrace:
at System.Drawing.Image.get_RawFormat()
at System.Drawing.Graphics.IgnoreMetafileErrors(Image image, Int32& errorStatus)
at System.Drawing.Graphics.DrawImage(Image image, Int32 x, Int32 y, Int32 width, Int32 height)
at System.Drawing.Graphics.DrawImage(Image image, Rectangle rect)
at System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
at System.Windows.Forms.Control.WmPaint(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativewindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativewindow.WndProc(Message& m)
at System.Windows.Forms.Nativewindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at WebImage.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
Was This Post Helpful? 0
  • +
  • -

#8 Jakeyy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-October 11

Posted 01 October 2011 - 06:01 AM

This is a pretty cool tutorial, thanks :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1