473,378 Members | 1,394 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Urgent! GDI+ Memory consumption

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.Clon e(); //// <--- 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;
}
}
}
Nov 17 '05 #1
0 1317

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Rob | last post by:
I have a form - when you click the submit button, it appends a variable to the URL (e.g. xyz.cgi?inputID=some_dynamic_variable) It also opens a new page. Now, that some_dynamic_variable is...
9
by: Stefan Bauer | last post by:
Hi NG, we've got a very urgent problem... :( We are importing data with the LOAD utility. The input DATE field data is in the format DDMMYYYY (for days) and MMYYYY (for months). The target...
28
by: Tamir Khason | last post by:
Follwing the struct: public struct TpSomeMsgRep { public uint SomeId;
16
by: | last post by:
Hi all, I have a website running on beta 2.0 on server 2003 web sp1 and I keep getting the following error:- Error In:...
7
by: zeyais | last post by:
Here is my HTML: <style> ..leftcolumn{float:left;width:300px;border: 1px solid #ccc} ..rtcolumn{float:left;width:600px;border: 1px solid #ccc} </style> <body> <div class="leftcolumn"...
33
by: dembla | last post by:
Hey Frnds can anyone help me in this i need a program in 'c' PROGRAM to print NxN Matrix 9 1 8 1 2 3 2 7 3 as 4 5 6 6 4 5 7 8 9 in sorted form
8
by: ginnisharma1 | last post by:
Hi All, I am very new to C language and I got really big assignment in my work.I am wondering if anyone can help me.........I need to port compiler from unix to windows and compiler is written...
1
by: alok sengar | last post by:
hi, I have already tried this URL's code "http://www.java2s.com/Code/CSharp/Network/SimpleSNMP.htm" but I am getting error when i am creating a UDP type Socket and recieving packet from this...
3
by: N. Spiker | last post by:
I am attempting to receive a single TCP packet with some text ending with carriage return and line feed characters. When the text is send and the packet has the urgent flag set, the text read from...
7
by: Cirene | last post by:
I used to use the Web Deployment Project with my VS2005 projects. Now I've fully upgraded to VS2008. Do I have to download a new version of the Web Deployment Project? If so where can I find...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.