Dynamic PNG images with ASP.NET and GDI+
page 1 of 1
Published: 05 Dec 2003
Unedited - Community Contributed
Abstract
Why do I get "A generic error occurred in GDI+. " when I switch my working GIF to use PNG format instead? Answer: You can't use the Bitmap Save() method with a "non-seekable" stream. Some image formats require that the stream can seek, as this article demonstrates.
by Chris Garrett
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 54333/ 36

Dynamic PNG images with ASP.NET and GDI+
Dynamic PNG images with ASP.NET and GDI+
by Chris Garrett

FAQ

Q: Why do I get "A generic error occurred in GDI+. " when I switch my working GIF to use PNG format instead?

A: You can''t use the Bitmap Save() method with a "non-seekable" stream. Some image formats require that the stream can seek.

Instead of streaming to the Response.OutputStream Save to a memory Stream, then copy from the memory Stream to the output Stream. See the example below.

Seeking? Seekable? What the - ?

"Seeking" is where the stream can be queried and the current position within a stream can be moved backwards and forwards.

Some streams, for example a network stream, have no concept of a current position so can not support seeking.

Creating and streaming a PNG with ASP.NET

Check out the source below which illustrates how to do this piece of stream gymnastics! It is a lot easier to do than it is to explain, honest ;O)

View Source
/chrisg/tools/view-png.aspx.aspx

/chrisg/tools/view-png.vb.aspx

Download
/chrisg/tools/download.asp?file=png.aspx

/chrisg/tools/download.asp?file=png.vb



User Comments

Title: System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+   
Name: Behrooz
Date: 2011-11-16 1:23:58 AM
Comment:
I could not open Source link or Download link .
If its possible please send me email.
behrooz.bit@gmail.com
Thanks a lot
Title: System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+   
Name: Quan
Date: 2011-11-03 12:46:15 AM
Comment:
I could not open the link source for download. Can you send to my email hongquanthuhuong@gmail.com. Thanks a lot
Title: System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+   
Name: Jason
Date: 2011-03-21 12:34:59 PM
Comment:
I could not open the link source for download. Can you send to my email pauleta_phoon@hotmail.com. Thanks a lot
Title: System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+   
Name: Mark
Date: 2010-06-29 4:22:50 PM
Comment:
This totally fixed my problem. Thanks!!
Title: I want see source but it don't show   
Name: samzuza
Date: 2010-05-29 5:46:08 PM
Comment:
I want see source but it don't show. i have a problem in view source link and download link .

please. help sent file to me.

sam_narak7@hotmail.com
Title: Got the same error when using Png files   
Name: Pedro
Date: 2010-03-11 6:29:06 PM
Comment:
This totally fixed my problem. Thank you!

GDI+ should throw a better error message...
Title: Thanks   
Name: Juan Santander
Date: 2010-01-15 9:59:35 AM
Comment:
This helped a lot, thank you very much!!!
Title: Thanks   
Name: Paul
Date: 2009-12-31 9:23:14 PM
Comment:
Also got the dreaded GDI+ generic error message!

Writing to a MemoryStream fixed the issue on the production server (I'm guessing permissions related because worked fine on local machine). But I am having trouble (now) with my PNG files, where before they were fine.
Title: Most unhelpful error message ever   
Name: Geo
Date: 2009-11-09 7:07:58 AM
Comment:
I hit this error too. In my case it was a typo, passing a target filename the same as the source file I had just opened in another stream (ie locked file). Silly mistake but terrible error message
Title: almost   
Name: jlm
Date: 2009-09-16 4:16:20 PM
Comment:
It would help to include source in the article just in case download links stopped working, like these have
Title: Thanks   
Name: Ebi
Date: 2009-08-06 9:00:33 AM
Comment:
Really thank u very much! it helps a lot.
Title: Many thanks!   
Name: enlightened reader
Date: 2009-07-01 8:36:57 PM
Comment:
Excellent solution to a pesky problem. Thank you for sharing! I could not open the links for source or download - got HTTP 503 errors on both.
Title: thanks   
Name: infused
Date: 2009-06-07 9:33:16 AM
Comment:
Wow, thanks. Had a lot of trouble with this error. Now I know why. Thanks
Title: Thank you   
Name: Evan
Date: 2009-06-03 11:04:05 AM
Comment:
I saw a lot of discussion pertaining to the generic GDI+ error but none of the suggestions seemed pertinent to my situation. Your fix was what I needed- many thanks.
Title: Great PNG resizing example   
Name: Corey
Date: 2009-05-22 12:10:44 PM
Comment:
Hey, thanks for this code. I had been trying to resize a png and maintain it's transparency for some time. This was just what I needed.
Title: Amazing!   
Name: Aaron Blake
Date: 2009-05-07 5:09:28 AM
Comment:
This was exactly what I needed, cheers.
Title: Great Work!   
Name: caleb harrison
Date: 2008-11-06 2:09:38 PM
Comment:
Thanks for this article. It helped me out a lot!
Title: Help   
Name: Paulo Santos
Date: 2008-09-18 10:10:36 AM
Comment:
i have a error when i save my pic on memory stream. can anyone hlep me?

code:
Public Shared Function ConvertBitmapToTiff(ByVal inputBmp As Bitmap, ByVal compression As TiffCompressionEnum) As Bitmap
'If the dest format matches the source format and quality/bpp not changing, just clone
If inputBmp.RawFormat.Equals(ImageFormat.Tiff) AndAlso compression = TiffCompressionEnum.Unspecified Then
Return DirectCast(inputBmp.Clone(), Bitmap)
End If

If compression = TiffCompressionEnum.Unspecified Then
'None of the params are chaning; use the general purpose converter
Return ConvertBitmap(inputBmp, ImageFormat.Tiff)
End If

'Create an in-memory stream which will be used to save
'the converted image

Dim imgStream As System.IO.Stream = New System.IO.MemoryStream

'Get the ImageCodecInfo for the desired target format
Dim destCodec As ImageCodecInfo = FindCodecForType(MimeTypeFromImageFormat(ImageFormat.Tiff))

If destCodec Is Nothing Then
'No codec available for that format
Throw New ArgumentException("The requested format " + MimeTypeFromImageFormat(ImageFormat.Tiff) + " does not have an available codec installed", "destFormat")
End If


'Create an EncoderParameters collection to contain the
'parameters that control the dest format's encoder
Dim destEncParams As New EncoderParameters(1)

'set the compression parameter
Dim compressionValue As EncoderValue

Select Case compression
Case TiffCompressionEnum.CCITT3
compressionValue = EncoderValue.CompressionCCITT3
Exit Select
Case TiffCompressionEnum.CCITT4

compressionValue = EncoderValue.Compression
Title: Great stuff   
Name: Simon
Date: 2008-08-18 6:26:10 AM
Comment:
Sorted my problem in no time!
Title: Excellent   
Name: Don
Date: 2007-09-26 7:27:36 PM
Comment:
Beautiful, clear explanation of exactly what to do and why.
Title: Auto Width/Height   
Name: Brian
Date: 2007-07-16 4:57:06 PM
Comment:
Is this possible to do without having to set a specific width/height of the image. Maybe somehow automatically knowing the width/height based on the text going into the PNG?
Title: You saved me!   
Name: Maciek
Date: 2006-11-07 4:48:14 AM
Comment:
Thanks... that was it!
Title: Very useful   
Name: tekHedd
Date: 2005-06-29 11:20:35 PM
Comment:
I agree that this is very helpful. It deals with the problem of generating PNG's on the fly and is a great example of basic transparent drawing (setting up the bitmap and so on). At the same time!
Title: Tnx   
Name: Rad Danesh
Date: 2005-05-28 7:25:17 AM
Comment:
Thanks :)
Title: Great help   
Name: Lee
Date: 2005-02-23 12:31:15 PM
Comment:
Thanks for this article, it was very useful. Pitty all articles on the web aren't this good, life would be so easy.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2017 ASPAlliance.com  |  Page Processed at 2017-12-16 9:56:11 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search