Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

HBITMAP

Status
Not open for further replies.

BigInch

Petroleum
Jun 21, 2006
15,161
Warning! Very limited graphics programming knowledge and using VB6. Now 3 days at the helm with no sleep.

I am trying to load a texture. I have been able to do this from a disk file by using LoadImage and specifying the texture's file name,

'***********************
Public HBITMAP as Long

HBITMAP = LoadImage(0, TextureFileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE Or LR_CREATEDIBSECTION)

'***********************

I would also like to load a texture from a Device Context, but can't seem to figure out what the procedure is for doing that. I now guess its probably impossible to simply BitBlt to an HBITMAP, as I have been able to do from one DC to another, or from a DC to a picture box. Anybody know how to get a DC into a HBITMAP? Humm... looks like I should have tried creating a DIB and attempt to load with the same statements above, so I'll be trying that until...


BigInch[worm]-born in the trenches.
 
Replies continue below

Recommended for you

Here's the code I eventually came up with, if anyone's interested.

It loads the picture from picture1 into one memory bitmap, hTmp, blits from hTmp to another memory bitmap jTmp, then blits it back from j to picture1 at 1/2 size.

Make a std exe Form1 with,
Picture Box, Picture1
Command Button, Command1
Load any picture you like into the picture box.

Add this code to the form,

Code:
'DECLARES
Private Declare Function CreateCompatibleDC Lib "gdi32.dll" (ByVal hdc As Long) As Long
Private Declare Function DeleteDC Lib "gdi32.dll" (ByVal hdc As Long) As Long
Private Declare Function SelectObject Lib "gdi32.dll" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function DeleteObject Lib "gdi32.dll" (ByVal hObject As Long) As Long
Private Declare Function BitBlt Lib "gdi32.dll" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop 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 StretchBlt Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long
' END DECLARES

Code:
Private Sub Command1_Click()

    Dim hTmp As Long  'DC 1 in memory
    Dim jTmp As Long  'DC 2 in memory
    
    'Create a DC with the same settings as the picturebox
    hTmp = CreateCompatibleDC(Picture1.hdc)
    jTmp = CreateCompatibleDC(Picture1.hdc)
    
    'If the DC was created...
    If hTmp Then
        'Obtain a bitmap with compatible settings
        hBitmap = CreateCompatibleBitmap(Picture1.hdc, Picture1.Width, Picture1.Height)
        
        If hBitmap Then
            'associate this bitmap with the DC
            SelectObject hTmp, hBitmap
            
            'Copy the picture from Picture1 on to the memory DC
            BitBlt hTmp, 0, 0, Picture1.Width, Picture1.Height, Picture1.hdc, 0, 0, vbSrcCopy
            
            'Now copy it back to the picture
            Me.Picture1 = Nothing
            StretchBlt Picture1.hdc, 0, 0, Picture1.Width, Picture1.Height, hTmp, 0, 0, Picture1.Width, Picture1.Height, vbSrcCopy
            Picture1.Refresh
            
        End If
    End If
    
    'Blit hTmp to jTmp
    jBitmap = CreateCompatibleBitmap(hTmp, Picture1.Width, Picture1.Height)
        
    SelectObject jTmp, jBitmap
    'SelectObject hTmp, hBitmap
    
    BitBlt jTmp, 0, 0, Picture1.Width, Picture1.Height, hTmp, 0, 0, vbSrcCopy

    'send jTmp to picture1
    Me.Picture1 = Nothing
    SelectObject jTmp, jBitmap
    StretchBlt Picture1.hdc, 0, 0, Picture1.Width \ 2, Picture1.Height \ 2, jTmp, 0, 0, Picture1.Width, Picture1.Height, vbSrcCopy
    Picture1.Refresh
    
    'Remove these objects now
    DeleteObject hBitmap
    DeleteObject jBitmap
    DeleteDC hTmp
    DeleteDC jTmp
End Sub

Private Sub Form_Load()
    Me.ScaleMode = vbPixels
    Me.AutoRedraw = True

End Sub


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor