locked
A generic error occurred in GDI+. at System.Drawing.Image.Save RRS feed

  • Question

  • User-954049702 posted


    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

    Saturday, May 10, 2008 1:41 AM

Answers

  • User-954049702 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 10, 2008 9:53 AM

All replies

  • User-525215917 posted
    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.
    Saturday, May 10, 2008 6:47 AM
  • User-954049702 posted

    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)

     

    Saturday, May 10, 2008 8:37 AM
  • User-525215917 posted
    Two things to check:
    1. path of image file - folder must exist,
    2. permissions of folder - you must have write permissions set to this folder.
    Saturday, May 10, 2008 8:48 AM
  • User-954049702 posted

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

    Saturday, May 10, 2008 9:40 AM
  • User-525215917 posted
    Can you show a full exception with trace?
    Saturday, May 10, 2008 9:42 AM
  • User-954049702 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, May 10, 2008 9:53 AM
  • User-1399921933 posted

    Picture which you are going to save is allready in use by some process, thats why you are getting the exception...


    Good Luck

    Monday, April 12, 2010 3:36 AM