472,356 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,356 software developers and data experts.

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:
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
Aug 13 '08 #1
3 2190
Sarah wrote:
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)
It is almost certain that you are not getting an object back from ConvertPage.
It would much easier to debug if you had written
Dim X As BitMap = obj.ConvertPage
X.Save("C:\scr....")

You don't really need the extra thread in ConvertPage. Since the call to
WebBrowser.Navigate is already asynchronous, there is no need to put it all on a
separate thread as well.

By the look of it, the specified height and width of the bitmap to be returned
is 0, 0; nothing in your code ever changes that. Giving it a size to shoot for
might help some. Try putting obj.Width = 640 : obj.Height = 480 before the call
to ConvertPage.

Aug 14 '08 #2
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:

thrCurrent.Start()
thrCurrent.Join()

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
Application.DoEvents()
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)
grSource.ReleaseHdc()
grBitmap.ReleaseHdc()
bmp.Save("C:\afilename.bmp")

End sub
Alternatively you could use Graphics.CopyFromScreen etc.



"Sarah" <He********@aol.comwrote in message
news:ba**********************************@z66g2000 hsc.googlegroups.com...
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
Aug 14 '08 #3
Sarah,

I assume that there is enough in this tip to solve your problem

http://www.vb-tips.com/ServerClock.aspx

Cor

"Sarah" <He********@aol.comschreef in bericht
news:ba**********************************@z66g2000 hsc.googlegroups.com...
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

Aug 14 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Lauchlan M | last post by:
Hi. Usin ASP.NET, getting an "Object reference not set to an instance of an object" error. In my login.aspx page I have: string arrUserRoles = new string {"UserRole"};...
1
by: Kamal | last post by:
I am trying to send mail through smtp. smtp service is running on my machine. But every time during the smtpmail.send(msg) call gives "Could not access 'CDO.Message' object." error. Could some...
1
by: Lauchlan M | last post by:
Hi. I'm using ASP.NET, getting an "Object reference not set to an instance of an object" error. In my login.aspx page I have: string arrUserRoles = new string {"UserRole"};...
1
by: Kurt Van Campenhout | last post by:
Hi, I am trying to get/set Terminal server information in the active directory on a windows 2000 domain. Since the ADSI calls for TS don't work until W2K3, I need to do it myself. I'm fairly...
2
by: Jeff | last post by:
I'm getting an Object Reference error before I even run my app, and I'm not sure where to look to find the cause. I'd appreciate your help. When I open my Windows Application project, the...
7
by: dhnriverside | last post by:
Hi peeps I'm just following this HOW-TO from MSDN.. http://support.microsoft.com/default.aspx?scid=kb;en-us;306355 But I've got a problem. I've adding the #using System.Diagnostics; line to...
2
by: louie.hutzel | last post by:
This JUST started happening, I don't remember changing any code: When I click the submit button on my form, stuff is supposed to happen (which it does correctly) and a result message is posted back...
9
by: bill | last post by:
I keep getting Object reference not set to an instance of an object error when trying to run my application on an installed client machine. I installed it on several others and it runs fine. I...
2
by: dotnetnoob | last post by:
i got this program that will fetch the data in the excel spreadsheet, it was working before then i make some adjustment and it now give me an error of "Object reference not set to an instance of an...
5
by: piyumi80 | last post by:
hi, i write the following code to get a specific data row from the data set.but it generates the "Object reference not set to an instance of an object.".....error private void...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
1
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.