Urgent! GDI+ Memory consumption

E

Ervin

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;
}
}
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top