The ASP.NET code below worked fine under v1.1 of the framework.
Basically, it takes an image file that is uploaded (via a web form)
and saves a thumbnail of that file to a temporary directory.
The web server has write permissions to the directory, so that
is NOT the issue.
The code below was compiled under v2.0 of the Framework and
now whenever the code runs we receive the following:
System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+. at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
According to the run-time error, the error occurs on the following line:
Try to save the image without encoder etc parameters and see what happens. If saving doesn't fail in this case you should check out the information in encoder parameters.
Don't forget to mark solution providing post as "Answered".
System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+. at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams) at System.Drawing.Image.Save(String filename, ImageFormat format)
Permissions are not a problem -- I have two web sites running the EXACT same code the v1.1 page can upload and resize any image file, the v2.0 crashes with the error shown in my original post.
Does ANYONE have a WORKING VB code example that makes use of the BitMap.Save( string, codec, encoder_params ) method using V2.0 of the Framework???
Your not going to believe this -- the site running v1.1 had a virtual directory set-up which was mapped to the directory in which the image was saved to -- things worked fine.
The v2.0 site also had the same virtual directory name, but the physical path was different -- I changed the path to point to the same directory as the v1.0 site and now the code works.
So in short -- you were right about the "path must exist".
Sorry again. Case closed.
Marked as answer by bdaviduck on May 10, 2008 01:53 PM
bdaviduck
Member
547 Points
138 Posts
A generic error occurred in GDI+. at System.Drawing.Image.Save
May 10, 2008 05:41 AM|LINK
The ASP.NET code below worked fine under v1.1 of the framework.
Basically, it takes an image file that is uploaded (via a web form)
and saves a thumbnail of that file to a temporary directory.
The web server has write permissions to the directory, so that
is NOT the issue.
The code below was compiled under v2.0 of the Framework and
now whenever the code runs we receive the following:
System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+. at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
According to the run-time error, the error occurs on the following line:
fin_img.Save( img_file_spec, img_codec, encoder_params )
Can someone please point out why code that ran under v1.1 is no longer running under v2.0??
And more importantly, if you see an obvious fix to the code, please reply!!
Thanks!
==========================================================
dim posted_fileobj as HttpPostedFile
dim img_file_stream as FileStream
dim src_img as System.Drawing.Bitmap
dim fin_img as System.Drawing.Bitmap
dim src_rect as Rectangle
dim fin_rect as Rectangle
dim gobj as Graphics
dim img_codec_array as ImageCodecInfo()
dim img_codec as ImageCodecInfo
dim encoder_params as System.Drawing.Imaging.EncoderParameters
dim image_height as Integer
dim image_width as Integer
dim adjust_ratio as Double
dim img_file_spec as String
' Yes - the server can write to this directory
img_file_spec = "D:\temp\thumbnail.jpg"
image_width = 200
image_height = 200
src_img = Nothing
fin_img = Nothing
try
' Get the file that was uploaded to us ...
posted_fileobj = request.Files( 0 )
' Create source image ...
src_img = new Bitmap( posted_fileobj.InputStream )
' Set source and final rectangle sizes ...
src_rect = New Rectangle( 0, 0, src_img.Width, src_img.Height )
fin_rect = New Rectangle( 0, 0, image_width, image_height )
' Create final image ...
fin_img = New Bitmap( image_width, image_height )
gobj = Graphics.FromImage( fin_img )
' Set graphics properties ...
gobj.InterpolationMode = InterpolationMode.HighQualityBicubic
gobj.SmoothingMode = SmoothingMode.HighQuality
gobj.PixelOffsetMode = PixelOffsetMode.HighQuality
gobj.CompositingQuality = CompositingQuality.HighQuality
' Copy source image to final ...
gobj.DrawImage( src_img, fin_rect, src_rect, System.Drawing.GraphicsUnit.Pixel )
' Set image quality to reduce image and file size ...
encoder_params = New System.Drawing.Imaging.EncoderParameters( 1 )
encoder_params.Param( 0 ) = New System.Drawing.Imaging.EncoderParameter( System.Drawing.Imaging.Encoder.Quality, 70L )
' Save optimized image to a JPEG file ...
fin_img.Save( img_file_spec, img_codec, encoder_params )
catch ex as Exception
err_msg = ex.ToString()
ok = false
finally
if ( not fin_img is Nothing ) then
fin_img.Dispose()
fin_img = Nothing
end if
' Must dispose, otherwise file remains locked ...
if ( not src_img is Nothing ) then
src_img.Dispose()
src_img = Nothing
end if
posted_fileobj = Nothing
end try
gdi+ memory bitmap sclaing
DigiMortal
Contributor
5658 Points
939 Posts
MVP
Re: A generic error occurred in GDI+. at System.Drawing.Image.Save
May 10, 2008 10:47 AM|LINK
Also visit my ASP.NET blog or follow me @ Twitter:twitter.com/gpeipman
bdaviduck
Member
547 Points
138 Posts
Re: A generic error occurred in GDI+. at System.Drawing.Image.Save
May 10, 2008 12:37 PM|LINK
No go, I changed the line:
fin_img.Save( img_file_spec, img_codec, encoder_params )
to
fin_img.Save( img_file_spec, System.Drawing.Imaging.ImageFormat.Jpeg )
the result (same exception):
System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+. at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams) at System.Drawing.Image.Save(String filename, ImageFormat format)
DigiMortal
Contributor
5658 Points
939 Posts
MVP
Re: A generic error occurred in GDI+. at System.Drawing.Image.Save
May 10, 2008 12:48 PM|LINK
Also visit my ASP.NET blog or follow me @ Twitter:twitter.com/gpeipman
bdaviduck
Member
547 Points
138 Posts
Re: A generic error occurred in GDI+. at System.Drawing.Image.Save
May 10, 2008 01:40 PM|LINK
Nope - as I stated in my original post,
Permissions are not a problem -- I have two web sites running the EXACT same code the v1.1 page can upload and resize any image file, the v2.0 crashes with the error shown in my original post.
Does ANYONE have a WORKING VB code example that makes use of the BitMap.Save( string, codec, encoder_params ) method using V2.0 of the Framework???
DigiMortal
Contributor
5658 Points
939 Posts
MVP
Re: A generic error occurred in GDI+. at System.Drawing.Image.Save
May 10, 2008 01:42 PM|LINK
Also visit my ASP.NET blog or follow me @ Twitter:twitter.com/gpeipman
bdaviduck
Member
547 Points
138 Posts
Re: A generic error occurred in GDI+. at System.Drawing.Image.Save
May 10, 2008 01:53 PM|LINK
Ugh,
Your not going to believe this -- the site running v1.1 had a virtual directory set-up which was mapped to the directory in which the image was saved to -- things worked fine.
The v2.0 site also had the same virtual directory name, but the physical path was different -- I changed the path to point to the same directory as the v1.0 site and now the code works.
So in short -- you were right about the "path must exist".
Sorry again. Case closed.
Subhash Shar...
Member
2 Points
1 Post
Re: A generic error occurred in GDI+. at System.Drawing.Image.Save
Apr 12, 2010 07:36 AM|LINK
Picture which you are going to save is allready in use by some process, thats why you are getting the exception...
Good Luck