all groups > dotnet drawing api > february 2004 >
You're in the

dotnet drawing api

group:

System.OutOfMemoryException: Out of memory.


System.OutOfMemoryException: Out of memory. versteijn NO[at]SPAM 538mail.nl
2/29/2004 1:02:47 PM
dotnet drawing api: Hello all

I have a ASPX file called scaleimage.aspx that scales my images down
to a given width. This page is used very much in web project I am
working on.

But due to the large size of the images I randomly get [X] images
(error) in Internet Explorer. When I open one individual image at that
point I read this exception:

[OutOfMemoryException: Out of memory.]
System.Drawing.Bitmap..ctor(String filename) +197
tekno.scaleimage.Page_Load(Object sender, EventArgs e)
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +35
System.Web.UI.Page.ProcessRequestMain() +731

Any suggestions are more than welcome. I hope you can help me.
Thank you in advance.

Regards,

Freek Versteijn



See here my code:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Request("path") Is Nothing Or Request("width") Is Nothing
Then
Return
End If

Dim img As Image = New
Bitmap(Server.MapPath(Server.UrlDecode(Request("path"))))
img = resizeImage(img, Request("width"), 0)

Response.ClearHeaders()
Response.ClearContent()
Response.Clear()
Response.ContentType = "Image/JPEG"

img.Save(Response.OutputStream, Imaging.ImageFormat.Jpeg)
Response.End()

'Dispose the image object immediately to limit memory use in
case highres/large images are used
img.Dispose()
End Sub

Public Function ResizeImage(ByRef image As Image, ByVal maxWidth
As Integer, ByVal maxHeight As Integer)
Dim imgHeight, imgWidth As Double

Dim bm As Bitmap = New Bitmap(image)

imgHeight = bm.Height
imgWidth = bm.Width

If (maxWidth > 0 And imgWidth > maxWidth) Or (maxHeight > 0
And imgHeight > maxHeight) Then
'Determine what dimension is off by more
Dim deltaWidth As Double = imgWidth - maxWidth
Dim deltaHeight As Double = imgHeight - maxHeight
Dim scaleFactor As Double

'If (deltaHeight > deltaWidth) Then
'Scale by the height
'scaleFactor = maxHeight / imgHeight
'Else
' 'Scale by the Width
scaleFactor = maxWidth / imgWidth
'End If

imgWidth *= scaleFactor
imgHeight *= scaleFactor
End If

Dim w As Integer = Convert.ToInt32(imgWidth)
Dim h As Integer = Convert.ToInt32(imgHeight)
Dim inp As IntPtr = New IntPtr
Dim bmp As System.Drawing.Image = bm.GetThumbnailImage(w, h,
Nothing, inp)

Return bmp
Re: System.OutOfMemoryException: Out of memory. versteijn NO[at]SPAM 538mail.nl
3/1/2004 8:22:04 AM
[quoted text, click to view]

Hi Martin,

Thanks, I'll try it! Why do you think this will spare memory?

Thank you.

Re: System.OutOfMemoryException: Out of memory. Martin Dechev
3/1/2004 11:03:29 AM
Hi, Versteijn,

Did you try using the DrawImage method on the Graphics class instead? You
will have to change the following lines:

[quoted text, click to view]

to:

Dim bmp As New Bitmap(w, h)
Graphics.FromImage(bmp).DrawImage(bm, w, h)

Hope this helps
Martin

Re: System.OutOfMemoryException: Out of memory. Martin Dechev
3/1/2004 11:19:19 AM
Sorry, it should be another overload:

Graphics.FromImage(bmp).DrawImage(bm, 0, 0, w, h)

It is so easy with the IntelliSence...

Greetings
Martin

Re: System.OutOfMemoryException: Out of memory. Martin Dechev
3/1/2004 5:29:53 PM
Hi, Freek Versteijn,

I don't know from which call the OutOfMemory originates, but I tested with a
2 MB bitmap scaling it to ~75% and it was working with the both methods. As
it is stated in the documentation for the GetThumbnailImage method it gives
good quality for 120x120, but lacks in quality for higher resolutions.

http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemDrawingImageClassGetThumbnailImageTopic.asp

Greetings
Martin
[quoted text, click to view]

Re: System.OutOfMemoryException: Out of memory. versteijn NO[at]SPAM 538mail.nl
3/6/2004 10:07:57 AM
[quoted text, click to view]

Hi Martin,

Changed my code a bit (see under), but ever call to the page uses
about 10-50 mb RAM, which is not freed after the response has been
send. After a while the process can't address any more memory.

The cause of the high memory use is the GetThumbnailImage function.

Regards,

Freek Versteijn




Here is my current code:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Request("path") Is Nothing Or Request("width") Is Nothing
Then
Return
End If

Dim bmp As Bitmap = New
Bitmap(Server.MapPath(Server.UrlDecode(Request("path"))))
ResizeImage(bmp, Request("width"), 0)

Response.ClearHeaders()
Response.ClearContent()
Response.Clear()
Response.ContentType = "Image/JPEG"

bmp.Save(Response.OutputStream, Imaging.ImageFormat.Jpeg)
Response.End()

'Dispose the image object immediately to limit memory use in
case highres/large images are used
bmp.Dispose()
End Sub

Public Sub ResizeImage(ByRef bmp As Bitmap, ByVal maxWidth As
Integer, ByVal maxHeight As Integer)
Dim imgHeight, imgWidth As Double
Dim bm As Bitmap = New Bitmap(bmp)

imgHeight = bm.Height
imgWidth = bm.Width

If (maxWidth > 0 And imgWidth > maxWidth) Or (maxHeight > 0
And imgHeight > maxHeight) Then
'Determine what dimension is off by more
Dim deltaWidth As Double = imgWidth - maxWidth
Dim deltaHeight As Double = imgHeight - maxHeight
Dim scaleFactor As Double

'If (deltaHeight > deltaWidth) Then
'Scale by the height
'scaleFactor = maxHeight / imgHeight
'Else
' 'Scale by the Width
scaleFactor = maxWidth / imgWidth
'End If

imgWidth *= scaleFactor
imgHeight *= scaleFactor
End If

Dim w As Integer = Convert.ToInt32(imgWidth)
Dim h As Integer = Convert.ToInt32(imgHeight)
Dim inp As IntPtr = New IntPtr
bmp = bm.GetThumbnailImage(w, h, Nothing, inp)
Re: System.OutOfMemoryException: Out of memory. DotNetJunkies User
8/6/2004 2:28:33 AM
Not that I can add anything you probably doen't now but I have the exact same problem.
See here:
http://gotdotnet.com/Community/MessageBoard/Thread.aspx?id=251562&Page=1#251562

So if you find a solution, feel free to post it here. As will I.

I'll look into it, and if I find anything i'll try to remember to post it here...

Thanks.

Søren Bjerre | Denmark



---
Posted using Wimdows.net NntpNews Component -

Re: System.OutOfMemoryException: Out of memory. Freek Versteijn
8/6/2004 7:16:46 AM
Hello,

The answer is to explicitly call Dispose() on every image _instance_.
This should be done for any object (often resources) that has this
method, according to Microsoft Developer Support :S

Strange and not well documented, but okay:S

Good luck.

Freek Versteijn

*** Sent via Developersdex http://www.developersdex.com ***
AddThis Social Bookmark Button