Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a VB.net (4.5.2) application that I want to use to resize a large number of JPg's from 2048 to 512 width in a batch type mode. I collect the file names in a list box and then index thru the list and resize each file and save it on the disk.

This process works really well for first 800 files then it crashes with an out of memory fault.

Here is a copy of the resize function.

AM I missing something in the cleanup?

My system has 32gb Ram and the PageFile drive has 1Tb with 90% percent available.
I did notice in the resource monitor that the page fault hums along nicely until it gets to the point when the application crashes, the it spikes to 100% and drops off.

Thanks
Nor

VB.NET
Public Function Resize_pix(ByRef InFileNAme As String, maxWidth As Integer, ByRef OutFileName As String) As Boolean

     ' now resize the image and save to resize_pix
     '
     Resize_pix = True
     Using newImage As Image = Image.FromFile(InFileNAme)
         '       Dim newImage As Image = Image.FromFile(FileNAme)
         Aspect = newImage.Height / newImage.Width  ' get the aspect ratio of the image
         Dim newWidth As Integer = maxWidth  ' set values to original image
         Dim newHeight As Integer = maxWidth * Aspect

         ' convert the image to a png and get a byte[]
         Using bmp As New Bitmap(newWidth, newHeight)

             Using g As Graphics = Graphics.FromImage(bmp)
                 Try
                     g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
                     g.FillRectangle(System.Drawing.Brushes.White, 0, 0, newWidth, newHeight)
                     g.DrawImage(newImage, 0, 0, newWidth, newHeight)
                 Catch ex As Exception
                     g.Dispose()
                     bmp.Dispose()
                     newImage.Dispose()
                     MsgBox("Resize [g.] : " & ex.Message)
                     Resize_pix = False
                 End Try
             End Using
             Try
                 If Resize_pix Then
                     bmp.Save(OutFileName.ToString, System.Drawing.Imaging.ImageFormat.Jpeg)
                     bmp.Dispose()
                 End If
             Catch ex As Exception
                 bmp.Dispose()
                 newImage.Dispose()
                 MsgBox("Resize [save] : " & ex.Message)
                 Resize_pix = False
             End Try
         End Using

         newImage.Dispose()
     End Using


What I have tried:

I dispose of each object in the resize process when I'm done with it. However it seems that some memory is not released.
Posted
Updated 7-Aug-21 13:00pm
v2
Comments
Dave Kreskowiak 7-Aug-21 19:08pm    
Track which file is throwing the error. You can get "Out of Memory" exceptions if GDI detects a problem with the image you're trying to load.
nor s 7-Aug-21 19:38pm    
Dave You are a genius! I looked thru the files and found on the had 0 length, nothing in it. I purged that file and the program did all 1302 file. Thank you!!!, Thank you!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900