Error creating thumbnail

T

tshad

oThumbnail.Save is giving me an error:

System.Runtime.InteropServices.ExternalException: A generic error occurred
in GDI+.

theFileName = New FileInfo(strFileName)
fileOut = fileIn.Substring(0,fileIn.LastIndexOf(".")) &
session("User").CompanyID & fileIn.Substring(fileIn.LastIndexOf("."))
pathFileOut = Server.MapPath("\Uploads") & "\" &
fileIn.Substring(0,fileIn.LastIndexOf(".")) & "Temp" &
fileIn.Substring(fileIn.LastIndexOf("."))
MyFile2.PostedFile.SaveAs(PathFileOut)

' Write out a temporary file that will be read back in for thumbnail
Dim oImage As System.Drawing.Image
oImage = oImage.FromFile(pathFileOut)

' Read back in File, create thumbnail and save it
Dim oThumbnail as System.Drawing.Image
oThumbnail = oImage.GetThumbnailImage(100,200,Nothing,Nothing)
thumbnailFileOut = Server.MapPath("\Uploads") & "\" &
fileIn.Substring(0,fileIn.LastIndexOf(".")) &
fileIn.Substring(fileIn.LastIndexOf("."))
oThumbnail.Save(Server.MapPath("uploads") & "\" & fileOut,
ImageFormat.Jpeg)

I am getting the error on the last line.

The path and filename are valid - what causes the problem here?

Thanks,

Tom
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

tshad said:
oThumbnail.Save is giving me an error:

System.Runtime.InteropServices.ExternalException: A generic error occurred
in GDI+.

theFileName = New FileInfo(strFileName)
fileOut = fileIn.Substring(0,fileIn.LastIndexOf(".")) &
session("User").CompanyID & fileIn.Substring(fileIn.LastIndexOf("."))
pathFileOut = Server.MapPath("\Uploads") & "\" &
fileIn.Substring(0,fileIn.LastIndexOf(".")) & "Temp" &
fileIn.Substring(fileIn.LastIndexOf("."))
MyFile2.PostedFile.SaveAs(PathFileOut)

' Write out a temporary file that will be read back in for thumbnail
Dim oImage As System.Drawing.Image
oImage = oImage.FromFile(pathFileOut)

' Read back in File, create thumbnail and save it
Dim oThumbnail as System.Drawing.Image
oThumbnail = oImage.GetThumbnailImage(100,200,Nothing,Nothing)
thumbnailFileOut = Server.MapPath("\Uploads") & "\" &
fileIn.Substring(0,fileIn.LastIndexOf(".")) &
fileIn.Substring(fileIn.LastIndexOf("."))
oThumbnail.Save(Server.MapPath("uploads") & "\" & fileOut,
ImageFormat.Jpeg)

I am getting the error on the last line.

The path and filename are valid - what causes the problem here?

Thanks,

Tom

You create the string thumbnailFileOut on the line before, but then you
use the string fileOut instead.

Another thing; Server.MapPath("\Uploads") should be
Server.MapPath("/Uploads"). A virtual path uses /, never \.
 
T

tshad

Göran Andersson said:
You create the string thumbnailFileOut on the line before, but then you
use the string fileOut instead.

Your right. I missed that. That was a carryover from my testing.

I also tried to make some changes such as not creating 2 image files and
working only with one (oImage).

I found that my ...Temp4... file is being locked and I can't get rid of it.
I thought that the oImage=nothing would release it. It seems to if I am
using 2 Image objects as I was doing before.

theFileName = New FileInfo(strFileName)
fileOut = fileIn.Substring(0,fileIn.LastIndexOf(".")) &
session("User").CompanyID & fileIn.Substring(fileIn.LastIndexOf("."))
pathFileOut = Server.MapPath("\Uploads") & "\" &
fileIn.Substring(0,fileIn.LastIndexOf(".")) & "Temp4" &
fileIn.Substring(fileIn.LastIndexOf("."))
MyFile2.PostedFile.SaveAs(PathFileOut)
CompanyPicture.Text = fileout
Dim oImage As System.Drawing.Image
oImage = oImage.FromFile(pathFileOut)
oImage = oImage.GetThumbnailImage(100,200,Nothing,Nothing)
FileOut = Server.MapPath("/Uploads") & "/" &
fileIn.Substring(0,fileIn.LastIndexOf(".")) &
fileIn.Substring(fileIn.LastIndexOf("."))
oImage.Save(FileOut, ImageFormat.Jpeg)
oImage = nothing

Also, is there a better way to handle this so I don't have to create the
temporary file that I am uploading (MyFile2.PostedFile.SaveAs(PathFileOut)).
I am doing this so I can use the FromFile method to get the image into the
image object. If I could get it directly into the image object I wouldn't
have the problem with the temporary file locking. Once it locks I can't
seem to get it unlocked (which is why I am now at Temp4 - I now have
Temp-Temp4 locked). I can't delete it even if I stop the web server.
Another thing; Server.MapPath("\Uploads") should be
Server.MapPath("/Uploads"). A virtual path uses /, never \.

I did try it with both "/Uploads" and "\Uploads" and it seems to work in
both cases.

Thanks,

Tom
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

tshad said:
Your right. I missed that. That was a carryover from my testing.

I also tried to make some changes such as not creating 2 image files and
working only with one (oImage).

I found that my ...Temp4... file is being locked and I can't get rid of it.
I thought that the oImage=nothing would release it. It seems to if I am
using 2 Image objects as I was doing before.

You have to call the Dispose method on the Image object to make it
release the resources. Dereferencing it will not immediately remove it,
it just makes it possible for the garbage collector to remove it, but
you have no control over when this will happen.
theFileName = New FileInfo(strFileName)
fileOut = fileIn.Substring(0,fileIn.LastIndexOf(".")) &
session("User").CompanyID & fileIn.Substring(fileIn.LastIndexOf("."))
pathFileOut = Server.MapPath("\Uploads") & "\" &
fileIn.Substring(0,fileIn.LastIndexOf(".")) & "Temp4" &
fileIn.Substring(fileIn.LastIndexOf("."))
MyFile2.PostedFile.SaveAs(PathFileOut)
CompanyPicture.Text = fileout
Dim oImage As System.Drawing.Image
oImage = oImage.FromFile(pathFileOut)
oImage = oImage.GetThumbnailImage(100,200,Nothing,Nothing)
FileOut = Server.MapPath("/Uploads") & "/" &
fileIn.Substring(0,fileIn.LastIndexOf(".")) &
fileIn.Substring(fileIn.LastIndexOf("."))
oImage.Save(FileOut, ImageFormat.Jpeg)
oImage = nothing

Also, is there a better way to handle this so I don't have to create the
temporary file that I am uploading (MyFile2.PostedFile.SaveAs(PathFileOut)).
I am doing this so I can use the FromFile method to get the image into the
image object. If I could get it directly into the image object I wouldn't
have the problem with the temporary file locking.

The InputStream property of PostedFile will give you a stream that you
can use to read the data. You can load the image directly from that stream.
Once it locks I can't
seem to get it unlocked (which is why I am now at Temp4 - I now have
Temp-Temp4 locked). I can't delete it even if I stop the web server.

That is because you are not disposing the Image objects.
I did try it with both "/Uploads" and "\Uploads" and it seems to work in
both cases.

The difference is that "/Uploads" means the "Uploads" folder at the "/"
path, while "\Uploads" means the "\Uploads" folder at the "." path. As
long as you are in the root folder, "/" and "." will have the same
meaning, and the file system will handle "\\" as "\", so you are saved
by a coinsidence and the by tolerance of the file system.

I see also that you are mixing / and \ in the absolute paths. You should
use the separator that is correct for the file system. To write code
that isn't depending on a specific file system, you should use the
Path.PathSeparator property to get the correct character, or use
Path.Combine to concatenate the path. You can also make MapPath do that,
if you first create a complete virtual path to the file, then use
MapPath on the final string.
 

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

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top