P
Patrick Dugan
I have a subroutine (posted here) that takes a image from a file and resizes
it and loads it onto a PictureBox.
There is a timer elsewhere that sends the routine a new filename to use.
The routine works fine until the 77th file or so. (The file names are
randomly assigned so they vary each time, but the number of succesful loops
is around 77 before the error is thrown.)
The error is:
Process Image ErrorOutOfMemoryExceptionat
Microsoft.AGL.Common.MISC.HandleAr()
at System.Drawing.Image.FromHbitmap()
at ShowSlide.Form1.ProcessImage()
at ShowSlide.Form1.Timer1_Tick()
at System.Windows.Forms.Timer._WnProc()
at System.Windows.Forms.ApplicationThreadContext._InternalContextMessages()
at Microsoft.AGL.Forms.EVL.EnterMainLoop()
at System.Windows.Forms.Application.Run()
at ShowSlide.Form1.Main()
The error continues to happen each time after that.
Apparently I have a memory leak somewhere that I am not closing but I cannot
tell where.
The code below disposes and closes all objects that can be (as far as I
know)
Does anyone have any ideas why the PDA is running out of memory?
What is being left "open" that is eating memory in this routine?
Any help would be appreciated!
'---------------------------------------------------------
' Get the desktop dimensions...
Dim desktop_wid As Int32 = Me.Width
Dim desktop_hgt As Int32 = Me.Height
PictureBox1.Image = New Bitmap(desktop_wid, desktop_hgt,
Imaging.PixelFormat.Format24bppRgb)
PictureBox1.Width = desktop_wid
PictureBox1.Height = desktop_hgt
' Fill the wallpaper background with the selected color...
Dim gr0 As Graphics = Graphics.FromImage(PictureBox1.Image)
Dim rect1 As New Rectangle(0, 0, PictureBox1.Image.Width,
PictureBox1.Image.Height)
Dim BackgroundColor As New Color
BackgroundColor = Color.Black
gr0.FillRectangle(New SolidBrush(BackgroundColor), rect1)
Dim HBT As New Bitmap(InputFilename)
Dim Hnd As New System.IntPtr
Hnd = HBT.GetHbitmap
Dim img As Image = Image.FromHbitmap(Hnd) '<<<<<<< THIS IS WHERE THE
ERROR OCCURS...>>>>>>>
HBT.Dispose()
' Get the requested image dimensions
Dim image_wid As Int32 = img.Width
Dim image_hgt As Int32 = img.Height
Dim intNewImageWidth As Integer
Dim intNewImageHeight As Integer
Dim TJpegWidth As Integer = CInt((img.Width * desktop_hgt) / img.Height)
Dim TJpegHeight As Integer = CInt((img.Height * desktop_wid) / img.Width)
If desktop_hgt >= desktop_wid Then
If TJpegHeight <= desktop_hgt Then
intNewImageWidth = desktop_wid
intNewImageHeight = CInt(TJpegHeight)
Else
intNewImageWidth = CInt(TJpegWidth)
intNewImageHeight = desktop_hgt
End If
Else
If TJpegHeight <= desktop_hgt Then
intNewImageWidth = desktop_wid
intNewImageHeight = CInt(TJpegHeight)
Else
intNewImageWidth = CInt(TJpegWidth)
intNewImageHeight = desktop_hgt
End If
End If
Dim srcrect As New Rectangle(0, 0, img.Width, img.Height)
Dim destrect As New Rectangle( _
CInt((desktop_wid - intNewImageWidth) \ 2), _
CInt((desktop_hgt - intNewImageHeight) \ 2), _
CInt(intNewImageWidth), _
CInt(intNewImageHeight))
gr0.DrawImage(img, destrect, srcrect, GraphicsUnit.Pixel)
PictureBox1.Refresh()
img.Dispose()
gr0.Dispose()
it and loads it onto a PictureBox.
There is a timer elsewhere that sends the routine a new filename to use.
The routine works fine until the 77th file or so. (The file names are
randomly assigned so they vary each time, but the number of succesful loops
is around 77 before the error is thrown.)
The error is:
Process Image ErrorOutOfMemoryExceptionat
Microsoft.AGL.Common.MISC.HandleAr()
at System.Drawing.Image.FromHbitmap()
at ShowSlide.Form1.ProcessImage()
at ShowSlide.Form1.Timer1_Tick()
at System.Windows.Forms.Timer._WnProc()
at System.Windows.Forms.ApplicationThreadContext._InternalContextMessages()
at Microsoft.AGL.Forms.EVL.EnterMainLoop()
at System.Windows.Forms.Application.Run()
at ShowSlide.Form1.Main()
The error continues to happen each time after that.
Apparently I have a memory leak somewhere that I am not closing but I cannot
tell where.
The code below disposes and closes all objects that can be (as far as I
know)
Does anyone have any ideas why the PDA is running out of memory?
What is being left "open" that is eating memory in this routine?
Any help would be appreciated!
'---------------------------------------------------------
' Get the desktop dimensions...
Dim desktop_wid As Int32 = Me.Width
Dim desktop_hgt As Int32 = Me.Height
PictureBox1.Image = New Bitmap(desktop_wid, desktop_hgt,
Imaging.PixelFormat.Format24bppRgb)
PictureBox1.Width = desktop_wid
PictureBox1.Height = desktop_hgt
' Fill the wallpaper background with the selected color...
Dim gr0 As Graphics = Graphics.FromImage(PictureBox1.Image)
Dim rect1 As New Rectangle(0, 0, PictureBox1.Image.Width,
PictureBox1.Image.Height)
Dim BackgroundColor As New Color
BackgroundColor = Color.Black
gr0.FillRectangle(New SolidBrush(BackgroundColor), rect1)
Dim HBT As New Bitmap(InputFilename)
Dim Hnd As New System.IntPtr
Hnd = HBT.GetHbitmap
Dim img As Image = Image.FromHbitmap(Hnd) '<<<<<<< THIS IS WHERE THE
ERROR OCCURS...>>>>>>>
HBT.Dispose()
' Get the requested image dimensions
Dim image_wid As Int32 = img.Width
Dim image_hgt As Int32 = img.Height
Dim intNewImageWidth As Integer
Dim intNewImageHeight As Integer
Dim TJpegWidth As Integer = CInt((img.Width * desktop_hgt) / img.Height)
Dim TJpegHeight As Integer = CInt((img.Height * desktop_wid) / img.Width)
If desktop_hgt >= desktop_wid Then
If TJpegHeight <= desktop_hgt Then
intNewImageWidth = desktop_wid
intNewImageHeight = CInt(TJpegHeight)
Else
intNewImageWidth = CInt(TJpegWidth)
intNewImageHeight = desktop_hgt
End If
Else
If TJpegHeight <= desktop_hgt Then
intNewImageWidth = desktop_wid
intNewImageHeight = CInt(TJpegHeight)
Else
intNewImageWidth = CInt(TJpegWidth)
intNewImageHeight = desktop_hgt
End If
End If
Dim srcrect As New Rectangle(0, 0, img.Width, img.Height)
Dim destrect As New Rectangle( _
CInt((desktop_wid - intNewImageWidth) \ 2), _
CInt((desktop_hgt - intNewImageHeight) \ 2), _
CInt(intNewImageWidth), _
CInt(intNewImageHeight))
gr0.DrawImage(img, destrect, srcrect, GraphicsUnit.Pixel)
PictureBox1.Refresh()
img.Dispose()
gr0.Dispose()