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

Theodosis 41 Reputation points
2021-05-06T17:39:42.46+00:00

(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.

Developer technologies VB
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.5K Reputation points
    2021-05-06T17:59:59.7+00:00

    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
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.