Png Image display Problem

Discussion in 'ASP .Net' started by =?Utf-8?B?RmFoYWQgQWlqYXo=?=, Dec 7, 2004.

  1. 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
    =?Utf-8?B?RmFoYWQgQWlqYXo=?=, Dec 7, 2004
    #1
    1. Advertising

  2. =?Utf-8?B?RmFoYWQgQWlqYXo=?=

    Hans Kesting Guest

    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
    Hans Kesting, Dec 7, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sela

    aspx to display PNG

    sela, Oct 27, 2006, in forum: .NET
    Replies:
    1
    Views:
    503
  2. Just Me
    Replies:
    1
    Views:
    343
    Just Me
    Jan 14, 2007
  3. Replies:
    0
    Views:
    452
  4. Erwin
    Replies:
    2
    Views:
    113
    Erwin
    May 7, 2008
  5. Mark Szlazak
    Replies:
    3
    Views:
    160
    Fletch
    Apr 25, 2006
Loading...

Share This Page