By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,417 Members | 1,845 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,417 IT Pros & Developers. It's quick & easy.

Png Image display Problem

P: n/a
Hi,

I am trying to display a Png image via ASP .NET. I get the image from the
stream, creates the Bitmap, set the content type to Image/Png, but it give an
error. If I set the content type to Image/Jpeg it works really fine. Below is
the code I wrote and the error msg:

************************************************** ********
ERROR
************************************************** ********
A generic error occurred in GDI+.
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+.

Source Error:
Line 27: // makeTransparent((float)Double.Parse(trans));
Line 28:
Line 29: b.Save(Response.OutputStream, ImageFormat.Png);
Line 30: }
Line 31:
Source File: c:\inetpub\wwwroot\ImageGenerator\ImgGen.aspx Line: 29

Stack Trace:
[ExternalException (0x80004005): A generic error occurred in GDI+.]
System.Drawing.Image.Save(Stream stream, ImageCodecInfo encoder,
EncoderParameters encoderParams) +631
System.Drawing.Image.Save(Stream stream, ImageFormat format) +34
ASP.ImgGen_aspx.Page_Load(Object sender, EventArgs e) in
c:\inetpub\wwwroot\ImageGenerator\ImgGen.aspx:29
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +750

************************************************** ********

I have given full permission to "Everyone" as I heard it is a permission
problem, but no success. Following is the piece of code that I wrote:

************************************************** ********
CODE
************************************************** ********

<%@ Page language="c#" Debug="true" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.IO" %>
<script language="C#" runat="server">
Bitmap b = null;
Graphics g = null;

void Page_Load(Object sender, EventArgs e)
{
Response.ContentType = "image/jpeg";
string url = Request.QueryString["url"];
string trans = Request.QueryString["trans"];

WebClient client = new WebClient();
Stream stream = client.OpenRead(url);
b = new Bitmap(stream);
// stream.Close(); // I TRIED AN UN-COMMENTED VERSION TOO

b.Save(Response.OutputStream, ImageFormat.Png);
}
</script>

************************************************** ********

Please note that if I change the content type to Image/Jpeg, it works
perfect. If any one can help, that would be appreciated.

Thanx,
Fahad
Nov 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Fahad Aijaz wrote:
Hi,

I am trying to display a Png image via ASP .NET. I get the image from
the stream, creates the Bitmap, set the content type to Image/Png,
but it give an error. If I set the content type to Image/Jpeg it
works really fine. Below is the code I wrote and the error msg:

************************************************** ********
ERROR
************************************************** ********
A generic error occurred in GDI+.
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+.

Source Error:
Line 27: // makeTransparent((float)Double.Parse(trans));
Line 28:
Line 29: b.Save(Response.OutputStream, ImageFormat.Png);
Line 30: }
Line 31:


try changing the code a bit, so that you are not writing directly to the OutputStream:

MemoryStream io = new MemoryStream();
myBitmap.Save(io, ImageFormat.Png);
byte[] ba = io.GetBuffer();
Response.BinaryWrite(ba);

There was something (I forgot the exact details) about needing read/write access to that
stream, and MemoryStream provides that.

Hans Kesting
Nov 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.