Drawing an image to a DC

From HashVB
Revision as of 21:04, 13 October 2005 by Wakjah (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Drawing an image to a DC in GDI is relatively simple - it is achieved in 4 steps:

  • Load the image
  • Create a temporary DC
  • Select the image's bitmap into the DC
  • Blt from the temp DC to the destination DC

The code to do so, therefore, is as follows:

 Private Sub DrawPic(FilePath As String, hDestDC As Long, x As Long, y As Long)
     Dim p As StdPicture
     Dim hDC As Long
     Dim hOldMapMode As Long
     
     Set p = LoadPicture(FilePath)
     
     hDC = CreateCompatibleDC(hDestDC)
     DeleteObject SelectObject(hDC, p.Handle)
     
     hOldMapMode = SetMapMode(hDestDC, MM_HIMETRIC)
     BitBlt _
         hDestDC, _
         x, y, _
         ScaleX(p.Width, vbHimetric, vbPixels), ScaleY(p.Height, vbHimetric, vbPixels), _
         hDC, _
         0, 0, _
         vbSrcCopy
     SetMapMode hDestDC, hOldMapMode
     
     DeleteDC hDC
     
     Set p = Nothing
 End Sub