By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,276 Members | 1,406 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,276 IT Pros & Developers. It's quick & easy.

Error creating thumbnail

P: n/a
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
Feb 8 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
tshad wrote:
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 \.

--
Göran Andersson
_____
http://www.guffa.com
Feb 8 '07 #2

P: n/a
"Göran Andersson" <gu***@guffa.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
tshad wrote:
> oThumbnail.Save is giving me an error:

System.Runtime.InteropServices.ExternalExceptio n: 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.
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
>
--
Göran Andersson
_____
http://www.guffa.com

Feb 8 '07 #3

P: n/a
tshad wrote:
"Göran Andersson" <gu***@guffa.comwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>tshad wrote:
>> 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.

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.
>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.
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.

--
Göran Andersson
_____
http://www.guffa.com
Feb 9 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.