Hi -
Please be gentle. I am quite new to visual basic, but I have been
going through tutorials and reading up. I found a code snippet on the
internet that I wanted to see if I could re-purpose for a project, but
I keep getting the error: "Object reference not set to an instance of
an object" for the 7th line of the code below which is:
obj.ConvertPage (URL).Save("C:\ screencaptest2. bmp",
System.Drawing. Imaging.ImageFo rmat.Bmp)
I think this happens when you have only declared an object and not
instantiated, but in my code below I thought I that I did both. Maybe
I am totally doing the wrong thing. I am just trying to pass an URL
to the public function ConvertPage and then save the returned bitmap.
Thank you in advance for any assistance you can provide.
Public Class Form1
Private Sub Button1_Click(B yVal sender As System.Object, ByVal e
As System.EventArg s) Handles Button1.Click
Dim obj As New ImageFromHtml
Dim URL As String
URL = "http://www.yahoo.com"
obj.ConvertPage (URL).Save("C:\ screencaptest2. bmp",
System.Drawing. Imaging.ImageFo rmat.Bmp)
End Sub
End Class
Imports Microsoft.Visua lBasic
Imports System.Threadin g
Imports System.Drawing
Imports System.Windows. Forms
Public Class ImageFromHtml
Private PageUrl As String
Private ConvertedImage As Bitmap
Private m_intHeight As Integer
Public Property Height() As Integer
Get
Return m_intHeight
End Get
Set(ByVal value As Integer)
m_intHeight = value
End Set
End Property
Private m_intWidth As Integer
Public Property Width() As Integer
Get
Return m_intWidth
End Get
Set(ByVal value As Integer)
m_intWidth = value
End Set
End Property
Public Function ConvertPage(ByV al PageUrl As String) As Bitmap
Me.PageUrl = PageUrl
Dim thrCurrent As New Thread(New ThreadStart(Add ressOf
CreateImage))
thrCurrent.SetA partmentState(A partmentState.S TA)
thrCurrent.Star t()
thrCurrent.Join ()
Return ConvertedImage
End Function
Private Sub CreateImage()
Dim BrowsePage As New WebBrowser()
BrowsePage.Scro llBarsEnabled = False
BrowsePage.Navi gate(PageUrl)
AddHandler BrowsePage.Docu mentCompleted, AddressOf
WebBrowser_Docu mentCompleted
While BrowsePage.Read yState <WebBrowserRead yState.Complete
Application.DoE vents()
End While
BrowsePage.Disp ose()
End Sub
Private Sub WebBrowser_Docu mentCompleted(B yVal sender As Object,
ByVal e As WebBrowserDocum entCompletedEve ntArgs)
Dim BrowsePage As WebBrowser = DirectCast(send er, WebBrowser)
BrowsePage.Clie ntSize = New Size(Width, Height)
BrowsePage.Scro llBarsEnabled = False
ConvertedImage = New Bitmap(Width, Height)
BrowsePage.Brin gToFront()
BrowsePage.Draw ToBitmap(Conver tedImage, BrowsePage.Boun ds)
End Sub
End Class