system.drawing error. Need help debugging.

D

darrel

I'm modyfying an image upload and resizing script that we've had laying
around for a long while.

I'm getting a NullReferenceException error (see full error at bottom) from
this line:


Dim g As System.Drawing.Image =
System.Drawing.Image.FromFile(strTempFullFileName)

I'm not sure what I should be checking for based on that error. There is a
file located where 'strTempFullFileName' points to.

Any suggestions on what else to check for would be appreciated.

-Darrel

FULL ERROR:

[NullReferenceException: Object reference not set to an instance of an
object.]
imageUpload.ImageUpload.btnUpload_Click(Object sender, EventArgs e) in
C:\Inetpub\wwwroot\CourtsCMS\imageUpload\ImageUpload.aspx.vb:552
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +108
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument) +57
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument) +18
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain() +1292
 
E

Eliyahu Goldin

Darrel,

Did you set a breakpoint at this line to make sure that strTempFullFileName
is not null?

Eliyahu
 
D

Darren Kopp

Without seeing your code, you will need to Upload the file, save it
someplace, then load from where you saved the image. Also try this:

Dim g as New System.Drawing.Image
g = System.Drawing.Image.FromFile(strTempFullFileName)

Could you post the code on where you are getting strTempFullFileName
from? Feel free to check out
http://www.easerve.com/developer/tutorials/asp-net-tutorials-upload-image-file.aspx.
That article has a decent tutorial of how to upload an image with
vb.net

HTH,
Darren Kopp
http://blog.secudocs.com/
 
D

darrel

Did you set a breakpoint at this line to make sure that
strTempFullFileName
is not null?

Yep. The error comes from this specific line, and strTempFullFileName does
resolve to a full file path that does point to the specific file.

-Darrel
 
D

darrel

Without seeing your code, you will need to Upload the file, save it
someplace, then load from where you saved the image.

Right. That's what this line is doing, grabbing the file that I've already
uploaded. The STR does resolve to a full path that does point at the file:

D:\Web\Applications\assets\images\_temp\imageUploadTest.jpg

But maybe that path is malformed? Does it look correct?
Dim g as New System.Drawing.Image
g = System.Drawing.Image.FromFile(strTempFullFileName)

I tried that, but get an error in the IDE:

Dim g As New System.Drawing.Image

('new' cannot be used on a class that is declared 'MustInherit')

-Darrel
 
D

Darren Kopp

Ah, here we go. Image is an abstract class. try this

Dim g as New System.Drawing.Bitmap(strTempFullFileName)

Don't worry that it's a "Bitmap", that just means it deals with pixels.
You can call something like g.save("bla.jpg", ImageFormat.Jpeg) to
save jpg's and such.

HTH,
Darren Kopp
http://blog.secudocs.com/
 
D

darrel

Dim g as New System.Drawing.Bitmap(strTempFullFileName)

I was optimitic! The IDE does let me enter that, but, alas, I get the exact
same error when I try to execute it.

-Darrel
 
D

Darren Kopp

could you post the code for how you upload the image and where you
assign a value to strTempFullFileName? The more code to look at the
easier it will be to find out what's happening.

-Darren Kopp
http://blog.secudocs.com/
 
D

darrel

could you post the code for how you upload the image and where you
assign a value to strTempFullFileName? The more code to look at the
easier it will be to find out what's happening.

Sure, though, that part of the code does execute just fine. The image is
uploaded and is sitting in that particular folder:

----------------------------------------------------------------------
Dim postedFile = uploadedFile.PostedFile
Dim contentType As String = postedFile.ContentType
Dim contentLength As Integer = postedFile.ContentLength

strUploadFileName = Mid(postedFile.FileName, InStrRev(postedFile.FileName,
"\") + 1)

'clean up the filename
strUploadFileName = fixFileName(strUploadFileName)

'Temp file name/location
strTempFullFileName = savePath & "_temp\" & strUploadFileName

'save the temp file
postedFile.SaveAs(strTempFullFileName)

resizeImage(categoryFolder, customImageSize, "jpg", strUploadFileName)

----------------------------------------------------------------------

The file I am trying to retrieve does have the correct path and file name as
the string is resolving to.

-Darrel
 
D

Darren Kopp

Where do you declare strTempFullFileName? I don't see Dim
strTempFullFileName as String. That could be the cause of the
exception, but even with that i wouldn't think it would compile. Also
shouldn't it be...

Dim postedFile as HttpPostedFile = uploadedFile.PostedFile?

-Darren Kopp
 
D

darrel

Where do you declare strTempFullFileName? I don't see Dim
strTempFullFileName as String. That could be the cause of the
exception, but even with that i wouldn't think it would compile. Also
shouldn't it be...

Darren:

Thanks for all the help. In the process of trying to send over the code, I
gave it yet another once over.

I think I discovered a user error on my part. One function was erroring out,
cascading back to the parent function, which then tried to execute a CAPTCH
statement, which, itself, had an error.

So, all this time, the error I was receiving wasn't referring to the actual
problem.

ARGH!

;o)

-Darrel
 

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

Top