Help understanding why I am receiving "Object reference not set to aninstance of an object" error



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:

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 = ""
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
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
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
Return ConvertedImage
End Function
Private Sub CreateImage()

Dim BrowsePage As New WebBrowser()
BrowsePage.ScrollBarsEnabled = False
AddHandler BrowsePage.DocumentCompleted, AddressOf
While BrowsePage.ReadyState <> WebBrowserReadyState.Complete
End While
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.DrawToBitmap(ConvertedImage, BrowsePage.Bounds)

End Sub

End Class

Bill McCarthy

Hi Sarah,

The short answer is this is not going to work. Even once you iron out the
bugs in your code, the call to DrawToBitmap does not work for activeX
controls. You can probably draw from the desktop or use BitBlt etc. As to
your code, the threading stuff in there really serves no purpose as you are
immediately waiting for the thread you just spawned:


The only reason you would do that is if you want to run an STA thread and
you currently aren't on one. If this is a winforms app you'll be on a STA
thread anyway. Last time I did this kind of trick was with a VB.NET macro
because the calling thread was MTA, and I needed STA for the
Winforms.Clipboard functions to work.

Then in your code you have :

While BrowsePage.ReadyState <> WebBrowserReadyState.Complete
End While

Which means you are waiting till the page is complete, hence you don't need
to add an event handler for that, you can handle that in the code and avoid
any thread rush issues that may occur as there is no guarantee what thread
the event will be called on.

The other issue is you have
BrowsePage.DrawToBitmap(ConvertedImage, BrowsePage.Bounds)
That may work in this case as long as the position of the control relative
to it's parent is at 0, 0. It's a lot safer to explicitly create a
rectangle such as new Rectangle(0, 0, Width, Height)

Okay, so you are probably wondering where to from here ;) I would suggest
first of all removing all the threading.... in fact, I'd suggest putting a
web browser control on a form and start from there.

This code works, and may be a starting point for you

Private Declare Function BitBlt Lib "gdi32" Alias "BitBlt" (ByVal hDestDC
As IntPtr, ByVal x As Int32, ByVal y As Int32, ByVal nWidth As Int32, ByVal
nHeight As Int32, ByVal hSrcDC As IntPtr, ByVal xSrc As Int32, ByVal ySrc As
Int32, ByVal dwRop As Int32) As Int32

Private Const SRCCOPY As Int32 = &HCC0020

Private Sub SaveBrowserAsBitmap()

Dim wdth = WebBrowser1.Width
Dim hght = WebBrowser1.Height
Dim bmp As New Bitmap(wdth, hght)

Dim grBitmap As Graphics = Graphics.FromImage(bmp)
Dim grSource As Graphics = Graphics.FromHwnd(WebBrowser1.Handle)
Dim success = BitBlt(grBitmap.GetHdc, 0, 0, wdth, hght,
grSource.GetHdc, 0, 0, SRCCOPY)

End sub

Alternatively you could use Graphics.CopyFromScreen etc.

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question