473,545 Members | 1,995 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.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
Aug 13 '08 #1
3 2318
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.ImageFo rmat.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.Navi gate 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.Star t()
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.Clipbo ard functions to work.

Then in your code you have :

While BrowsePage.Read yState <WebBrowserRead yState.Complete
Application.DoE vents()
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.Draw ToBitmap(Conver tedImage, BrowsePage.Boun ds)
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 SaveBrowserAsBi tmap()

Dim wdth = WebBrowser1.Wid th
Dim hght = WebBrowser1.Hei ght
Dim bmp As New Bitmap(wdth, hght)

Dim grBitmap As Graphics = Graphics.FromIm age(bmp)
Dim grSource As Graphics = Graphics.FromHw nd(WebBrowser1. Handle)
Dim success = BitBlt(grBitmap .GetHdc, 0, 0, wdth, hght,
grSource.GetHdc , 0, 0, SRCCOPY)
grSource.Releas eHdc()
grBitmap.Releas eHdc()
bmp.Save("C:\af ilename.bmp")

End sub
Alternatively you could use Graphics.CopyFr omScreen etc.



"Sarah" <He********@aol .comwrote in message
news:ba******** *************** ***********@z66 g2000hsc.google groups.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.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
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******** *************** ***********@z66 g2000hsc.google groups.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.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

Aug 14 '08 #4

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

Similar topics

6
19932
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"}; Context.Items.Add("UserRoles", arrUserRoles); Context.User = new
1
5444
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 one help me out to resolve this issue. Thanks in advance, Kamal
1
2345
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"}; Context.Items.Add("UserRoles", arrUserRoles); Context.User = new
1
941
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 new to VB.NET, so I need some help. Here is a code snippit :
2
2202
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 following Microsoft Development Environment error message displays: "Object reference not set to an instance of an object." Then when I access the...
7
3782
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 my cs file, but when I try and type the following line... Exception objErr = Server.GetLastError().GetBaseException();
2
2967
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 to the same page at the top of my form alerting the user that their request has been approved or denied. However, the end result is an error...
9
4544
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 tried installing .Net framework 1.1 on the clients machine that is getting the error but the error remains. Can anyone provide help? Thanks.
2
2701
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 object error" i can't seem to see what's wrong with my code so i'm hoping some fresh eyes can spot what's wrong or what i'm missing. the following...
5
2306
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 showClientListForUser() { string fkeys = new string; fkeys = txtUserName.Text; fkeys = txtUserCompanyNumber.Text;
0
7675
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7440
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7775
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5344
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4963
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3470
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3451
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1902
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
726
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.