Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic Image creation becoming extremely slow.

Status
Not open for further replies.

solepixel

Programmer
May 30, 2007
111
US
I'm creating a website with dynamically generated header graphics and I'm running into a problem with the server. I don't have all the specs on the server, but is there a more optimized way of creating images that doesn't take up so much CPU resources. The problem I'm having tends to be when there are more than 2 images on the page the script must create. I was reading about octree, but not quite sure how to implement. I'm kind of new to ASP.NET, so any help is much appreciated.

Here's the image creation script:
Code:
'setup my variables
Dim strText As String = Request("text")
Dim intSize As Integer = 30

' set up memory stream
Dim MemStream As New MemoryStream()

'setup font
Dim privateFontCollection As New PrivateFontCollection()
' to load in the font add the font file to the private collection.
privateFontCollection.AddFontFile(Server.MapPath("\fonts\tradeg_ltc18.ttf"))
Dim thisFont As FontFamily = privateFontCollection.Families(0)
Dim regFont As New Font(thisFont, intSize, FontStyle.Regular, GraphicsUnit.Pixel)

'setup width height
Dim txtpic As New System.Drawing.Bitmap(5000, 5000, PixelFormat.Format32bppArgb)
Dim txtg As Graphics = Graphics.FromImage(txtpic)
Dim dimensions As SizeF = txtg.MeasureString(strText, regFont)
Dim imgWidth As Integer = dimensions.Width
Dim imgHeight As Integer = dimensions.Height
txtpic.Dispose()
txtg.Dispose()
'Dim imgWidth As Integer = 300
'Dim imgHeight As Integer = 50

' set up image and graphic
Dim pic As New System.Drawing.Bitmap(imgWidth, imgHeight, PixelFormat.Format32bppArgb)
Dim g As Graphics = Graphics.FromImage(pic)

' set up pen and brush
Dim TextBrush As SolidBrush = New SolidBrush(Color.FromArgb(255, 0, 0, 0))

' blank the image and set smoothing
'g.Clear(Color.Transparent)
g.Clear(Color.FromArgb(255, 255, 255, 255))
g.SmoothingMode = SmoothingMode.AntiAlias ' antialias objects
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias
'g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit

' draw image
g.DrawString(strText, regFont, TextBrush, New PointF(0, 0))

' set the content type
'Response.ContentType = "image/png"
Response.ContentType = "image/gif"
'Response.ContentType = "image/jpg"

' send the image to the memory stream then output
'pic.Save(MemStream, ImageFormat.Png)
pic.Save(MemStream, ImageFormat.Gif)
'pic.Save(MemStream, ImageFormat.Jpeg)
MemStream.WriteTo(Response.OutputStream)

' tidy up
pic.Dispose()
 
Ok, i have done that, but it doesn't seem to help out terribly much. I understand it's much more difficult for ASP.NET to dynamically create images than PHP, but surely there are better methods.
 
Try adding Trace="True" to your web page. Then, add Trace.Warn statements into various sections of the image creation sections and when you view the page, you should be able to see where the process is slowing down at which will at least give you something to work with.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
I did as you said, ca8msm, but I'm not quite sure what's supposed to happen. Once I added it all, the page stopped working at all for a few minutes. Now it's working again, but I don't really know how trace works to know how well my script is working. Is there a log file somewhere that has my warn messages in it?
 
No, what should happen is if you add Trace="True" e.g.
Code:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default1.aspx.vb" Inherits="Default1" Trace="true" %>
Then, when you add Trace.Warn statements in your code e.g.
Code:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Trace.Warn("I did something here")
    End Sub
you should automatically get some information written out to your page e.g.
Code:
Trace Information
Category	Message	From First(s)	From Last(s)
aspx.page	Begin PreInit		
aspx.page	End PreInit	0.00103588584582677	0.001036
aspx.page	Begin Init	0.00166278116352777	0.000627
aspx.page	End Init	0.00171837482138093	0.000056
aspx.page	Begin InitComplete	0.00174603196775009	0.000028
aspx.page	End InitComplete	0.00176754308159277	0.000022
aspx.page	Begin PreLoad	0.00178737800474641	0.000020
aspx.page	End PreLoad	0.00180777165812973	0.000020
aspx.page	Begin Load	0.00182788594639822	0.000020
	[!]I did something here	0.00233269870891412	0.000505[/!]
aspx.page	End Load	0.00236175268085748	0.000029
aspx.page	Begin LoadComplete	0.00238326379470016	0.000022
aspx.page	End LoadComplete	0.00240393681319833	0.000021
aspx.page	Begin PreRender	0.00242433046658165	0.000020
aspx.page	End PreRender	0.00245422253386953	0.000030
aspx.page	Begin PreRenderComplete	0.00247601301282705	0.000022
aspx.page	End PreRenderComplete	0.00249640666621037	0.000020
aspx.page	Begin SaveState	0.0330500105460331	0.030554
aspx.page	End SaveState	0.050866520744955	0.017817
aspx.page	Begin SaveStateComplete	0.0509087048772959	0.000042
aspx.page	End SaveStateComplete	0.0509313334515979	0.000023
aspx.page	Begin Render	0.0509517271049812	0.000020
aspx.page	End Render	0.0532609591442488	0.002309
The line I highlighted should show you when your statement started. You can add additional statements throughout your code which will enable you to fins out which sections are taking the longest time.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
That will probably be because you are outputting an image. Don't set the content type and don't output the image to the output stream whilst you are testing and you should be presented with a HTML page with the tracing information on it.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
would this line on my page be hurting anything?

<%@ OutputCache Duration="360" VaryByParam="text" %>
 
Doesn't the trace only display if you are running the web page from the server ? ie through localhost ?



 
I got it to display now. Not quite sure what all this means, but here's the section you showed me:
Code:
aspx.page	Begin PreInit		
aspx.page	End PreInit	2.76783195305999E-05	0.000028
aspx.page	Begin Init	5.33617601788463E-05	0.000026
aspx.page	End Init	7.45904712928144E-05	0.000021
aspx.page	Begin InitComplete	9.52342179224833E-05	0.000021
aspx.page	End InitComplete	0.0001152979997643	0.000020
aspx.page	Begin PreLoad	0.000136596706628526	0.000021
aspx.page	End PreLoad	0.000155525557376874	0.000019
aspx.page	Begin Load	0.000174344414803387	0.000019
	line 19	0.000197123031815925	0.000023
	line 27	0.000683483502787331	0.000486
	line 39	0.104260919872722	0.103577
	line 44	0.104496615562627	0.000236
	line 55	0.104576940685744	0.000080
	line 59	0.105689483138524	0.001113
aspx.page	End Load	0.105723526071631	0.000034
aspx.page	Begin LoadComplete	0.105745434741462	0.000022
aspx.page	End LoadComplete	0.105772898074046	0.000027
aspx.page	Begin PreRender	0.105792861861958	0.000020
aspx.page	End PreRender	0.10581536049597	0.000022
aspx.page	Begin PreRenderComplete	0.105835109296936	0.000020
aspx.page	End PreRenderComplete	0.105855588053582	0.000020
aspx.page	Begin SaveState	0.105949382358928	0.000094
aspx.page	End SaveState	0.105972705942853	0.000023
aspx.page	Begin SaveStateComplete	0.105991999771442	0.000019
aspx.page	End SaveStateComplete	0.106011498587586	0.000019
aspx.page	Begin Render	0.106030372441673	0.000019
aspx.page	End Render	0.106093523607495	0.000063
 
Doesn't the trace only display if you are running the web page from the server ? ie through localhost ?
No, I don't think so. I've used it from a staging server before.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
I'm just going to take a guess, but is my delay at or around line 39?
 
What is shows is that the time taken between "line 27" and "line 29" is the slowest as it takes "0.103756" seconds.

What you need to do is check what is taking the longest time, and then see if there are any improvements you can make to the piece of code in question.


____________________________________________________________
Mark,
[URL unfurl="true"]http://aspnetlibrary.com[/url]

Need help finding an answer? Try the Search Facility or read FAQ222-2244.
 
It appears this code is slowing it down the most:
Code:
txtpic.Dispose()
txtg.Dispose()

Would it help to do this at the end when I dispose of the other image and graphics? Would that help the speed?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top