Drawing an image to a DC

From HashVB
Jump to: navigation, search
float
 This article is based on Visual Basic 6. Find other Visual Basic 6 articles.

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