S
Sarah
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.ImageFormat.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(ByVal sender As System.Object, ByVal e
As System.EventArgs) 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.ImageFormat.Bmp)
End Sub
End Class
Imports Microsoft.VisualBasic
Imports System.Threading
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(ByVal PageUrl As String) As Bitmap
Me.PageUrl = PageUrl
Dim thrCurrent As New Thread(New ThreadStart(AddressOf
CreateImage))
thrCurrent.SetApartmentState(ApartmentState.STA)
thrCurrent.Start()
thrCurrent.Join()
Return ConvertedImage
End Function
Private Sub CreateImage()
Dim BrowsePage As New WebBrowser()
BrowsePage.ScrollBarsEnabled = False
BrowsePage.Navigate(PageUrl)
AddHandler BrowsePage.DocumentCompleted, AddressOf
WebBrowser_DocumentCompleted
While BrowsePage.ReadyState <> WebBrowserReadyState.Complete
Application.DoEvents()
End While
BrowsePage.Dispose()
End Sub
Private Sub WebBrowser_DocumentCompleted(ByVal sender As Object,
ByVal e As WebBrowserDocumentCompletedEventArgs)
Dim BrowsePage As WebBrowser = DirectCast(sender, WebBrowser)
BrowsePage.ClientSize = New Size(Width, Height)
BrowsePage.ScrollBarsEnabled = False
ConvertedImage = New Bitmap(Width, Height)
BrowsePage.BringToFront()
BrowsePage.DrawToBitmap(ConvertedImage, BrowsePage.Bounds)
End Sub
End Class
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.ImageFormat.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(ByVal sender As System.Object, ByVal e
As System.EventArgs) 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.ImageFormat.Bmp)
End Sub
End Class
Imports Microsoft.VisualBasic
Imports System.Threading
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(ByVal PageUrl As String) As Bitmap
Me.PageUrl = PageUrl
Dim thrCurrent As New Thread(New ThreadStart(AddressOf
CreateImage))
thrCurrent.SetApartmentState(ApartmentState.STA)
thrCurrent.Start()
thrCurrent.Join()
Return ConvertedImage
End Function
Private Sub CreateImage()
Dim BrowsePage As New WebBrowser()
BrowsePage.ScrollBarsEnabled = False
BrowsePage.Navigate(PageUrl)
AddHandler BrowsePage.DocumentCompleted, AddressOf
WebBrowser_DocumentCompleted
While BrowsePage.ReadyState <> WebBrowserReadyState.Complete
Application.DoEvents()
End While
BrowsePage.Dispose()
End Sub
Private Sub WebBrowser_DocumentCompleted(ByVal sender As Object,
ByVal e As WebBrowserDocumentCompletedEventArgs)
Dim BrowsePage As WebBrowser = DirectCast(sender, WebBrowser)
BrowsePage.ClientSize = New Size(Width, Height)
BrowsePage.ScrollBarsEnabled = False
ConvertedImage = New Bitmap(Width, Height)
BrowsePage.BringToFront()
BrowsePage.DrawToBitmap(ConvertedImage, BrowsePage.Bounds)
End Sub
End Class