I have a problem with a memory leak.
I have this code in a button_click
:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim ms As New IO.MemoryStream
Dim bm As New Bitmap("\Application Data\imgs\IMG22.jpg")
bm.Save(ms, Drawing.Imaging.ImageFormat.Jpeg)
End Sub
This code works just fine when I'm running the .exe at my laptop (I mean under windows7/32bits with the full .net framework) but when I run the app in a device with WindowsMobile 6.1 the app throws this exception:
SmartDeviceProject22.exe
OutOfMemoryException
at
Microsoft.AGL.Common.MISC.HandleAr(PAL_ERROR ar)
at
System.Drawing.Image.Save(Stream stream, ImageFormat format)
at
SmartDeviceProject22.Form1.Button3_Click(Object sender, EventArgs e)
at
....
The image size is around 200kb and the width and height around 1500px. Details of image:
- Dimension: 1536x2048
- Horizontal Resolution: 72dpi
- Horizontal Resolution: 72dpi
- Bit depth: 24
- Resolution unit: 2
- Color representation: sRGB -
Any help it will be really appreciated.
I try the code of @asawyer even remove ALL the code,reference, etc and the problem keeps, I guess it's something about the width/height of the image or with the compact framework.
Any other advice ?
Solution and explanation of the problem Well after test somethings the real problem it was not a memory leak, just as @pdriegen said its a problem of memory available .
I change my code to this (and tested at the mobile device):
Dim fs As IO.FileStream = IO.File.OpenRead("\Application Data\ryder\IMG23.jpg")
Dim arrb(fs.Length) As Byte
fs.Read(arrb, 0, arrb.Length)
fs.Close()
fs.Dispose()
And with the code above (apparently) I get a byte() (array) of the image to store in the database using dataSet.
In conclusion: load a bitmap object to memoryStream, bad idea. Many thanks to everyone who take its time to read my problem,and specially those who post their answer.
Solution (if you need to show the image in a picture box):
After a few weeks, this probably the best (for free) solution: Implement an ImageHelper as is explained here: ImageHelper
This class/sample uses the Drawing NameSpace from OpenNetCF (http://www.opennetcf.com/)
It works great and it solve my memory troubles loading big bitmaps to memory, actually we load a thumbnail, so the size in memory is reduced considerably and avoid the OutOfMemory exception problem.
About Chris Tacke I just realize that the author of the post about ImageHelper and co-founder of OpenNetCF it's here at stackoverflow, here is his profile: https://stackoverflow.com/users/13154/ctacke