Problem Saving .Png Format

Last post 06-24-2009 3:21 PM by IranianCuriousBoy. 2 replies.

Sort Posts:

  • Problem Saving .Png Format

    06-24-2009, 2:10 AM

     Hi

    I create a Bitmap and when try to save it in png format there is an error. It says:

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

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

    There is no problem saving in gif or jpeg formats.

    and my code:

            // Create a new Bitmap object, 50 x 50 pixels in size
            Image canvas = new Bitmap(50, 50);
            // create an object that will do the drawing operations
            Graphics artist = Graphics.FromImage(canvas);
            // draw a few shapes on the canvas picture
            artist.Clear(Color.Lime);
            artist.FillEllipse(Brushes.Red, 3, 30, 30, 30);
            artist.DrawBezier(new Pen(Color.Blue, 3), 0, 0, 40, 15, 10, 35, 50, 50);
            // return the picture
            canvas.Save(Response.OutputStream, ImageFormat.Png);
            // now the drawing is done, we can discard the artist object
            artist.Dispose();
            // also the Bitmap
            canvas.Dispose();
     
    Help please
    Thanks


     

     

  • Re: Problem Saving .Png Format

    06-24-2009, 3:44 AM
    Answer
    • Participant
      1,197 point Participant
    • Ambran
    • Member since 03-18-2009, 2:23 PM
    • Denmark
    • Posts 234

    Hi Iranianboy

    The solution is to use a Response.BinaryWrite like this:

    MemoryStream io = new MemoryStream();
    bmp.Save(io, ImageFormat.Png);
    Response.BinaryWrite( io.GetBuffer() );

    Found it here: http://www.velocityreviews.com/forums/t78540-generation-of-png-in-aspnet-failed.html

    Good luck

    Amit

    If this helped you in any way, mark it as an Answer, Thanks.
  • Re: Problem Saving .Png Format

    06-24-2009, 3:21 PM

     Hi

    Reason:The PNG implementation in System.Drawing requires the use of a searchable stream, which Response.OutputStream isn’t.

    Thanks

Page 1 of 1 (3 items)