Urgent! GDI+ Memory consumption

Discussion in 'ASP .Net' started by Ervin, Sep 15, 2003.

  1. Ervin

    Ervin Guest

    Hi All,

    I've got a small program that allows you to write text to the front of a
    postcard, and send it to someone. It works fine for about 10-15 requests,
    then it just hangs. Specifically, it hangs at the creation of a StringFormat
    object.

    The deadline for the projects has already past, and I'm smashing my head
    against the wall trying to figure this out.

    I've tried everything to get this code to work properly, but it just ends up
    consuming about 300k of memory (aspnet_wp.exe process) on each request, and
    doesn't release them even though I call all the dispose methods & set
    referece objects to null.

    The code below is a .aspx file, which is called by an image tag in the
    calling page.

    If any one could help, I'd really appreciate it.

    (Note: "p" is a simple class which holds all the properties for the
    postcard)

    // Create image
    System.Drawing.Image image = null;
    Graphics g = null;

    if(p.CardFront.FileName != string.Empty)
    {
    image = new Bitmap(p.CardFront.FileName );
    }
    else
    {
    image = new Bitmap((int)mWidth, (int)mHeight,
    PixelFormat.Format32bppArgb);
    }

    // Set font settings
    FontStyle fontStyle = new FontStyle();
    FontFamily fontFamily = null;
    Font font = null;
    SolidBrush solidBrush = null;
    StringFormat stringFormat =
    (StringFormat)StringFormat.GenericTypographic.Clone(); //// <--- hangs
    here!
    RectangleF rectangleF;
    SizeF sizeF;

    try
    {
    // Set graphics values
    g = Graphics.FromImage(image);
    g.CompositingQuality = CompositingQuality.HighQuality;
    g.InterpolationMode = InterpolationMode.High;
    g.SmoothingMode = SmoothingMode.HighQuality;
    g.TextRenderingHint = TextRenderingHint.AntiAlias;

    // Check for values;
    if(p.CardFront.Line1.Text.Length > 0)
    {
    stringFormat.Alignment = p.CardFront.Line1.Alignment;

    if(p.CardFront.Line1.Bold)
    fontStyle |= FontStyle.Bold;

    if(p.CardFront.Line1.Italic)
    fontStyle |= FontStyle.Italic;

    if(p.CardFront.Line1.Underline)
    fontStyle |= FontStyle.Underline;

    fontFamily = GetFamily(p.CardFront.Line1.FontFace);
    font = new Font(fontFamily,(float)p.CardFront.Line1.FontSize,
    fontStyle);
    solidBrush = new SolidBrush(p.CardFront.Line1.FontColor);

    sizeF = g.MeasureString(p.CardFront.Line1.Text, font, (int)p.Width,
    stringFormat);
    p.CardFront.Line1.Width = sizeF.Width;
    p.CardFront.Line1.Height = sizeF.Height;

    rectangleF = new RectangleF(p.CardFront.Line1.X, p.CardFront.Line1.Y,
    p.CardFront.Line1.Width, p.CardFront.Line1.Height);

    g.DrawString(p.CardFront.Line1.Text, font, solidBrush, rectangleF,
    stringFormat);
    p.CardFront.Line2.Y = rectangleF.Bottom;
    }

    if(p.CardFront.Line2.Text.Length > 0)
    {
    stringFormat.Alignment = p.CardFront.Line2.Alignment;

    if(p.CardFront.Line2.Bold)
    fontStyle |= FontStyle.Bold;

    if(p.CardFront.Line2.Italic)
    fontStyle |= FontStyle.Italic;

    if(p.CardFront.Line2.Underline)
    fontStyle |= FontStyle.Underline;

    fontFamily = GetFamily(p.CardFront.Line2.FontFace);
    font = new Font(fontFamily,(float)p.CardFront.Line2.FontSize,
    fontStyle);
    solidBrush = new SolidBrush(p.CardFront.Line2.FontColor);

    sizeF = g.MeasureString(p.CardFront.Line2.Text, font, (int)p.Width,
    stringFormat);
    p.CardFront.Line2.Width = sizeF.Width;
    p.CardFront.Line2.Height = sizeF.Height;

    rectangleF = new RectangleF(p.CardFront.Line2.X, p.CardFront.Line2.Y,
    p.CardFront.Line2.Width, p.CardFront.Line2.Height);

    g.DrawString(p.CardFront.Line2.Text, font, solidBrush, rectangleF,
    stringFormat);
    p.CardFront.Line3.Y = rectangleF.Bottom;
    }

    if(p.CardFront.Line3.Text.Length > 0)
    {
    stringFormat.Alignment = p.CardFront.Line3.Alignment;

    if(p.CardFront.Line3.Bold)
    fontStyle |= FontStyle.Bold;

    if(p.CardFront.Line3.Italic)
    fontStyle |= FontStyle.Italic;

    if(p.CardFront.Line3.Underline)
    fontStyle |= FontStyle.Underline;

    fontFamily = GetFamily(p.CardFront.Line3.FontFace);
    font = new Font(fontFamily,(float)p.CardFront.Line3.FontSize,
    fontStyle);
    solidBrush = new SolidBrush(p.CardFront.Line3.FontColor);

    sizeF = g.MeasureString(p.CardFront.Line3.Text, font, (int)p.Width,
    stringFormat);
    p.CardFront.Line3.Width = sizeF.Width;
    p.CardFront.Line3.Height = sizeF.Height;

    rectangleF = new RectangleF(p.CardFront.Line3.X, p.CardFront.Line3.Y,
    p.CardFront.Line3.Width, p.CardFront.Line3.Height);

    g.DrawString(p.CardFront.Line3.Text, font, solidBrush, rectangleF,
    stringFormat);
    p.CardFront.Line4.Y = rectangleF.Bottom;
    }

    if(p.CardFront.Line4.Text.Length > 0)
    {
    stringFormat.Alignment = p.CardFront.Line4.Alignment;

    if(p.CardFront.Line4.Bold)
    fontStyle |= FontStyle.Bold;

    if(p.CardFront.Line4.Italic)
    fontStyle |= FontStyle.Italic;

    if(p.CardFront.Line4.Underline)
    fontStyle |= FontStyle.Underline;

    fontFamily = GetFamily(p.CardFront.Line4.FontFace);
    font = new Font(fontFamily,(float)p.CardFront.Line4.FontSize,
    fontStyle);
    solidBrush = new SolidBrush(p.CardFront.Line4.FontColor);

    sizeF = g.MeasureString(p.CardFront.Line4.Text, font, (int)p.Width,
    stringFormat);
    p.CardFront.Line4.Width = sizeF.Width;
    p.CardFront.Line4.Height = sizeF.Height;

    rectangleF = new RectangleF(p.CardFront.Line4.X, p.CardFront.Line4.Y,
    p.CardFront.Line4.Width, p.CardFront.Line4.Height);

    g.DrawString(p.CardFront.Line4.Text, font, solidBrush, rectangleF,
    stringFormat);
    p.CardFront.Line4.Y = rectangleF.Bottom;
    }

    // Save to stream
    Response.ContentType = "image/jpeg";
    image.Save(Response.OutputStream,ImageFormat.Jpeg);
    }
    catch(Exception exp)
    {
    HttpContext.Current.Trace.Warn("An error occured", exp.Message + " " +
    exp.StackTrace);
    }
    finally
    {

    // Clean up
    if(fontFamily != null)
    {
    fontFamily.Dispose();
    fontFamily = null;
    }

    if(stringFormat != null)
    {
    stringFormat.Dispose();
    stringFormat = null;
    }

    if(font != null)
    {
    font.Dispose();
    font = null;
    }

    if(solidBrush != null)
    {
    solidBrush.Dispose();
    solidBrush = null;
    }

    if(image != null)
    {
    image.Dispose();
    image = null;
    }

    if(g != null)
    {
    g.Dispose();
    g = null;
    }
    }
    }
     
    Ervin, Sep 15, 2003
    #1
    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. Duccio
    Replies:
    0
    Views:
    535
    Duccio
    Feb 25, 2006
  2. Kiran Kumar

    aspnet_wp.exe memory consumption

    Kiran Kumar, Jul 15, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    462
    Natty Gur
    Jul 15, 2003
  3. tony_wang
    Replies:
    1
    Views:
    4,092
    Saravana [MVP]
    Nov 21, 2003
  4. Jim Campbell
    Replies:
    1
    Views:
    956
    Ashish
    Feb 12, 2004
  5. Robin van de Water [synthesiSFactory]

    GDI+ is not properly initialized (internal GDI+ error).

    Robin van de Water [synthesiSFactory], Jan 10, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    1,349
    Robin van de Water [synthesiSFactory]
    Jan 10, 2007
Loading...

Share This Page