Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 20th, 2005, 07:34 AM
Registered User
 
Join Date: Jun 2005
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problems with GDI Image Generation

I am facing problems with Image generation at runtime using .Net GDI Library. The code was taken from net and works fine at source location. While it was working absolutely fine with my local server till two days back, it has started having issues now.

On the production server, the code runs only once or twice and puts the site on a hang. Thereafter the image generation pages dont work until the IIS is restarted, although the rest of the site still works.

Below I am pasting the code that I am using for image generation. The concept is that we take a set of values from user along with the font and size and then create an image on runtime.

    Sub NewWordingPreview()
        Dim i, j As Int16
        Dim conn As SqlConnection
        Dim cmd As SqlCommand
        Dim reader As SqlDataReader
        Dim Query As String
        conn = clsmyHelper.getConnection


        Dim ImgTemplatePath As String = "testimage.jpg"
'you can include any of your image here, mines comes from db. I have just removed that code to make it simple.

        Dim iHeight As Int16 = 100

        Dim bmp As New Bitmap(Server.MapPath(ImgTemplatePath))
        Dim g As Graphics
        Dim sAlign As New StringFormat
        Dim bmpNew As Bitmap
        Dim imageNew As Image

        Dim InkColor As Color
        InkColor = Color.FromName("Red")

        Dim pvtFntColl As New PrivateFontCollection
        pvtFntColl.AddFontFile(Server.MapPath("Fonts/Arial.ttf"))
        pvtFntColl.AddFontFile(Server.MapPath("Fonts/verdana.ttf"))
        pvtFntColl.AddFontFile(Server.MapPath("Fonts/times.ttf"))
'The ttf files are present in the correct location

        sAlign.Alignment = StringAlignment.Center
        Try
            g = Graphics.FromImage(bmp)
        Catch ex As Exception
            bmpNew = New Bitmap(bmp.Width, bmp.Height)
            imageNew = bmpNew
            g = Graphics.FromImage(imageNew)
            ' Draw the contents of old bitmap to new bitmap
            g.DrawImage(bmp, New Rectangle(0, 0, _
                                  imageNew.Width, imageNew.Height), _
                                  0, 0, bmp.Width, bmp.Height, _
                                  GraphicsUnit.Pixel)
            bmp = imageNew
            bmpNew.Dispose()
            imageNew.Dispose()
        End Try
        g.SmoothingMode = SmoothingMode.AntiAlias
        '*************Getting values of lines from left control***************

        For i = 1 To 8
            Try
                Dim fnt As Font
                Dim str As String = "This is a sample string"
                fnt = New Font(pvtFntColl.Families(0), 1)
                g.DrawString(str, fnt, New SolidBrush(InkColor), New PointF((bmp.Width / 2), iHeight), sAlign)
                iHeight += g.MeasureString(str, fnt).Height
            Catch ex As Exception
                Response.Write(ex)
            End Try
        Next
        'saving and showing image
        Try
            'Response.Write(Server.MapPath("Images/Orders/" & Session.SessionID & "-" & Session("PID") & "-" & Session("OrderID") & ".jpg"))
            bmp.Save(Server.MapPath("Images/Orders/" & Session.SessionID & "-" & Session("PID") & "-" & Session("OrderID") & ".jpg"), ImageFormat.Jpeg)
        Catch ex As Exception
            Response.Write(ex)
        End Try
        cImg.Controls.RemoveAt(0)
        Dim img As New HtmlImage
        img.Src = "Images/Orders/" & Session.SessionID & "-" & Session("PID") & "-" & Session("OrderID") & ".jpg"
        cImg.Controls.Add(img)

        g.Dispose()
        bmp.Dispose()
        pvtFntColl.Dispose()
    End Sub


Reply With Quote
  #2 (permalink)  
Old June 20th, 2005, 12:16 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

You should wrap all your code in a Try Catch block and make sure you call .Dispose in the Finally block so all resources are released.
Working with Graphics objects like the Bitmap causes a lock on those files until you release it by calling Dispose. That blocks other code from accessing the files.

Use Try / Catch and see if that makes any difference. Also, make sure you call .Dispose on each of the objects you're using that supports .Dispose.

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Reply With Quote
  #3 (permalink)  
Old June 21st, 2005, 01:10 AM
Registered User
 
Join Date: Jun 2005
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the reply Imar. I had used the try catch for only the situation that raised exceptions. But as per your suggestion I have now used another try..catch to include the complete code of the procedure and then dispose the objects in finally, but even this is of no help.

I am not very sure of why this happens, seems to be some problem with memory leak or something. It works for upto 15 minutes some times and then hangs, once hanged, it needs a server reboot to run again.

I have searched the net on this, but the problem seems to be a bit particular and nothing much is given on net. I found 2-3 more people with same problem but even they dont have a solution to it.

Please see if you can provide some more solution to it.

Thanks
Sandeep

Reply With Quote
  #4 (permalink)  
Old June 21st, 2005, 03:01 PM
Imar's Avatar
Wrox Author
Points: 72,073, Level: 100
Points: 72,073, Level: 100 Points: 72,073, Level: 100 Points: 72,073, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

I am afraid I don't have any more tips at this point.

A while ago I built a Windows Service that automatically imported JPG images. It reads in the embedded EXIF and IPTC data, creates three thumbs of the image, and watermarks the larger original. In the beginning, I was experiencing the same problem. The service would suck up to 500MB of memory and then come to a grinding halt. Once we shuffled things around a bit, making sure we called .Dispose on each important object at the right time, the problem went away.

It has been running for months now, importing thousands and thousands of images without a problem (or even a reboot).

So the only thing I can recommend is reevaluating your Dispose strategy. Although it is possible there is a memory leak in the .NET framework, it's more likely it's due to some coding problems....

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Trainspotting by Primal Scream (Track 3 from the album: Trainspotting) What's This?
Reply With Quote
  #5 (permalink)  
Old September 8th, 2006, 08:29 AM
Registered User
 
Join Date: Sep 2006
Location: Bergen, , Norway.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

are you sure your finally block is beeing run?
some errors might stop the finally from beeing run?

Olav Alexander Mjelde
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems in Image Data Type ayan.mukherjee VB Databases Basics 0 June 30th, 2008 03:54 AM
Default image generation while uploading an image! ostwald ASP.NET 2.0 Professional 1 September 12th, 2007 01:44 AM
background image problems Seraphim Beshoner CSS Cascading Style Sheets 3 April 11th, 2005 09:14 PM
Problems in Loading Image ctranjith General .NET 0 December 18th, 2004 06:01 AM
GDI frame image nbryson Pro VB.NET 2002/2003 0 December 4th, 2003 11:13 AM



All times are GMT -4. The time now is 08:49 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.