Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Outputting image to response output stream giving GDI+ error

Tags:

c#

asp.net

gdi+

When outputting an image to the output stream, does it require temporary storage? I get the "generic GDI+" error that is usually associated with folder permission error when saving an image to file.

The only thing I'm doing to the image is adding some text. I still get the error even when I output the image straight without modifications. For example, doing this will give me the error:

using (Bitmap image = new Bitmap(context.Server.MapPath("images/stars_5.png"))) {     image.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png); } 

Everything works fine on my local machine running Windows 7 with IIS 7.5 and ASP.NET 2.0. The problem is occurring on the QA server which is running Windows Server 2003 with IIS 6 and ASP.NET 2.0.

The line that's giving the error is:

image.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Png); 

Here's the stack trace:

[ExternalException (0x80004005): A generic error occurred in GDI+.]    System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams) +378002    System.Drawing.Image.Save(Stream stream, ImageFormat format) +36    GetRating.ProcessRequest(HttpContext context) in d:\inetpub\wwwroot\SymInfoQA\Apps\tools\Rating\GetRating.ashx:54    System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +181    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +75 
like image 803
Daniel Avatar asked Apr 12 '11 01:04

Daniel


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language?

C is an imperative procedural language supporting structured programming, lexical variable scope, and recursion, with a static type system. It was designed to be compiled to provide low-level access to memory and language constructs that map efficiently to machine instructions, all with minimal runtime support.


2 Answers

PNGs (and other formats) need to be saved to a seekable stream. Using an intermediate MemoryStream will do the trick:

using (Bitmap image = new Bitmap(context.Server.MapPath("images/stars_5.png"))) {    using(MemoryStream ms = new MemoryStream())    {       image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);       ms.WriteTo(context.Response.OutputStream);    } } 
like image 124
Mark Cidade Avatar answered Sep 17 '22 15:09

Mark Cidade


I just would add:

Response.ContentType = "image/png"; 

So it can be viewed directly in the browser when it isn't within an img tag.

like image 35
oamilkar Avatar answered Sep 19 '22 15:09

oamilkar