Quantcast
Channel: VBForums - CodeBank - Visual Basic 6 and earlier
Viewing all articles
Browse latest Browse all 1529

FYI: Store many images as StdPicture without hitting the GDI objects limit

$
0
0
I recently had to store many images that are StdPicture objects.
The problem is that each one create a new GDI object, and the (default) limit of max GDI objects is 1000.
(It is set on the key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Windows\GDIProcessHandleQuota)

One solution would be to store them in disk, but that would have been slow (to save and load), so I wanted to keep them on memory.

To convert StdPictures to byte arrays using API would have required a lot of work, since the pictures can be Icons, Bitmaps or Windows metafiles. Also if they were just bitmaps, they can be 8, 16, 32 bits, with palette or even monochrome.
To handle all possible formats would have required lot of code and testing.

But there is a very simple solution: use PropertyBags.

It is easy to store pictures into a PropertyBag object, and when needed back to StdPicture.

I had code like this (air code):

Code:

Private mPictures() as StdPicture

Private Sub StorePictures()
    Dim c As Long

    ReDim mPictures(12000)
    For c = 0 To 12000   
        Set mPictures(c) = [Some pic  in StdPicture format]
    Next
End Sub

Public Property Get MyPicture(Index As Long) As StdPicture
    Set MyPicture = mPictures(Index)
End Property

Turned to this:

Code:

Private mPictures() as PropertyBag

Private Sub StorePictures()
    Dim c As Long
    Dim pb As PropertyBag

    ReDim mPictures(12000)
    For c = 0 To 12000   
        Set pb = New PropertyBag
        pb.WriteProperty "i",  [Some pic  in StdPicture format]
        Set mPictures(c) = pb
    Next
End Sub

Public Property Get MyPicture(Index As Long) As StdPicture
    Set MyPicture = mPictures(Index).ReadProperty("i")
End Property

It does not use any system GDI object.
It is of course a bit slower, but not much.

Viewing all articles
Browse latest Browse all 1529

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>