System.OutOfMemoryException: Out of memory.

  • Thread starter Thread starter Versteijn
  • Start date Start date
V

Versteijn

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
End Function
 
Hi, Versteijn,

Did you try using the DrawImage method on the Graphics class instead? You
will have to change the following lines:
Dim inp As IntPtr = New IntPtr
Dim bmp As System.Drawing.Image = bm.GetThumbnailImage(w, h, Nothing, inp)

to:

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

Hope this helps
Martin
 
Sorry, it should be another overload:

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

It is so easy with the IntelliSence...

Greetings
Martin
 
Martin Dechev said:
Sorry, it should be another overload:

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

It is so easy with the IntelliSence...

Greetings
Martin

Hi Martin,

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

Thank you.

Freek Versteijn
 
Hi Martin,
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)
End Sub
 
Back
Top