question

Theodosis-7186 avatar image
0 Votes"
Theodosis-7186 asked Theodosis-7186 edited

How can I load multiple images and resize them as bitmaps without getting the error "Out of memory"(Visual Basic)?(SOLVED)

(I know there are solutions for this problem and i tried most of them at least, but with no result)
Im making a program where I try load multiple images at once, resize each one and add them in a listview.


When I add them directly to listview, there is an error.
When I add them to imagelist first, there is an error.
When I add them to an image array(i think its called like that)
(e.x. dim img() as image), there is an error.

When I try all the above, but firstly resize them, there is also an error.
When I say "an error", I mean "Out of memory".

Specifically, the error occurs when I try to load the third image as a bitmat, either for changing its dimensions, or for directly adding them to the listview.
[e.x. Dim img as new Bitmap(Image.fromfile(D:\example.png)]

i try to load 893 images with dimensions 8k or 16k and their size varies between 900mb to 2.2GB each.

Code:

 Public Sub addicon(imagepath As String, ImageNameNoExtension As String)
    
 'the point where I get the error'
 '-----------------------'
         Dim bm_source As New Bitmap(Image.FromFile(imagepath))
 '--------------------------'
         Dim gr_dest As Graphics
    
                 If bm_source.Width = bm_source.Height Then
                     w = 300
                     h = 300
    
                 Else
                     If bm_source.Width > bm_source.Height Then
                         Dim fw = bm_source.Width
                         w = 300
                         factor = w / fw
                         h = bm_source.Height * factor
    
                     Else
                         Dim fh = bm_source.Height
                         h = 300
                         factor = h / fh
                         w = bm_source.Width * factor
    
                     End If
                 End If
    
                 Dim bm_desk As New Bitmap(bm_source, CInt(w), CInt(h))
                 bm_desk.SetResolution(10, 10)
    
    
                 ImageList1.Images.Add(ImageNameNoExtension, bm_desk)
                 ListView1.LargeImageList = ImageList1
                 ListView1.Items.Add(ImageNameNoExtension, ImageNameNoExtension)
    
                 ListView1.Refresh()
              
    
     End Sub


Any suggestions about the problem or any further questions are appriciated.

dotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Viorel-1 avatar image
0 Votes"
Viorel-1 answered Theodosis-7186 commented

According to documentation, "Out of Memory" is not always caused by unavailable memory. Did you identify the file that generates this error? Maybe it is invalid or unsupported.

If bm_source is used locally, then consider disposing:

 Using bm_source As New Bitmap(Image.FromFile(imagepath))
    . . .
 End Using


· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I forgot to mention that I tried this method(using bm_source..) but the same error occurs.

I thought it would be a nice idea to save all of them (the resized ones) for once and then load them from a local directory, but still cannot do that because of the error.




'Out of memory'
'error-----------------------------------------------------------------'
Using bm_source As New Bitmap(Image.FromFile(PicInLocal))
'error-----------------------------------------------------------------'
......................
.............................
...................................

                 bm_desk.Save(SecondaryIconpath & "\" & MatName & ".png")
    
                 bm_desk.Dispose()
             End Using

0 Votes 0 ·

About the file that generates this error, well its not corrupted
Just a minute ago I tried the code again and for some reason 3 images were saved.
The exact moment I restarted the program and test it one more time.
Only two images were saved

0 Votes 0 ·

Hi @Theodosis-7186 ,
I make a test based on your code, but I get no exception.
Here's my code.

 Public Sub addicon(imagepath As String, ImageNameNoExtension As String)
    
         Using bm_source As New Bitmap(Image.FromFile(imagepath))
             ......
    
             Using bm_desk As New Bitmap(bm_source, CInt(w), CInt(h))
                 bm_desk.SetResolution(10, 10)
    
                 bm_desk.Save("...")
                 ImageList1.Images.Add(ImageNameNoExtension, bm_desk)
    
                 ListView1.LargeImageList = ImageList1
                 ListView1.Items.Add(ImageNameNoExtension, ImageNameNoExtension)
    
                 ListView1.Refresh()
             End Using
         End Using
     End Sub
1 Vote 1 ·
Show more comments