Link to home
Start Free TrialLog in
Avatar of smiffe
smiffe

asked on

Load resource bmp to memory

i need to know how to load a resource bmp id 101 into memory with a dc so i can use bitblt and also how to free the memory on unload
Avatar of ameba
ameba
Flag of Croatia image

Last 10 Grades Given: C A A C C A A A C A  
Not to mention an open question from December.

Community Support says that you should always assign an A grade. If you need clarification of the answer, ask.
ASKER CERTIFIED SOLUTION
Avatar of sharmon
sharmon

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of smiffe
smiffe

ASKER

thanks for the input sharmon but this seems to be distorting the image can't i do this in a function and get rid of the class

Public sub loadBmptoMem()

what variables would i have to pass
There is no way it can distort the image.  Unless you are bitblt'ing it incorrectly.  You can convert it into just a Module if you want, but it's the same thing.  The class just makes sure that when it's destroyed the memory being used by the dc and the image is cleared.  Plus the class will allow you to open multiple bitmaps if needed by just declaring a new instance of it.  I would leave it as a class but feel free to convert it, I guess it's up to you.
Avatar of smiffe

ASKER

never mind i figured it out it had to do with the height and width.  here's what i ended up with for my module


Option Explicit

Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal Hdc As Long) As Long
Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal Hdc As Long, _
    ByVal nWidth As Long, ByVal nHeight As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal Hdc As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal Hdc As Long, ByVal _
    hObject As Long) As Long

Dim MemHdc() As Long
Dim numDCs As Long

Public Function CreateMemHdc(screenHdc As Long, resID As Long) As Long
    ReDim Preserve MemHdc(numDCs) As Long
    Dim bitmapHdc As Long
       
    MemHdc(numDCs) = CreateCompatibleDC(screenHdc)
    If MemHdc(numDCs) Then
        bitmapHdc = CreateCompatibleBitmap(screenHdc, 0, 0)
        If bitmapHdc Then
            SelectObject MemHdc(numDCs), LoadResPicture(resID, vbResBitmap)
            CreateMemHdc = MemHdc(numDCs)
        End If
    End If
   
    DeleteObject bitmapHdc
    numDCs = numDCs + 1
End Function

Public Sub DestroyMemHdcs()
    Dim i As Integer
    For i = 0 To numDCs - 1
        DeleteDC MemHdc(i)
    Next i
End Sub

it seems to work and i don't think it leaks memory.
thanks for the help sharmon.

Glad I could help.  Take care...