Difference between revisions of "StretchBlt"

From HashVB
Jump to: navigation, search
 
m (Tidied up the last edit and removed the duplicate sample)
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 +
{{VB6}}
 
===What is StretchBlt?===
 
===What is StretchBlt?===
 
StretchBlt is a GDI API that is very similar to [http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_0fzo.asp BitBlt] in its operation, except that it not only copies part of a DC to another DC, but it will also stretch it.
 
StretchBlt is a GDI API that is very similar to [http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdi/bitmaps_0fzo.asp BitBlt] in its operation, except that it not only copies part of a DC to another DC, but it will also stretch it.
  
 
===Simple StretchBlt Use===
 
===Simple StretchBlt Use===
The following is a simple example of how to use stretchblt (along with a previous code sample - [http://earlsoft.co.uk/hashvb/Drawing_an_image_to_a_DC]) in order to stretch an image to the form's size.
+
The following is a simple example of how to use stretchblt (along with a previous code sample - [[Drawing an image to a DC]]) in order to stretch an image to the form's size.
  
 
   Private Sub Form_Paint()
 
   Private Sub Form_Paint()
Line 35: Line 36:
 
So here's the code:
 
So here's the code:
  
 +
  'Put below constants in Genaral -> Declarations
 +
  Private Const HALFTONE As Long = 4
 +
  Private Const COLORONCOLOR As Long = 3
 +
 
   Private Sub Form_Paint()
 
   Private Sub Form_Paint()
 
       Dim p As StdPicture
 
       Dim p As StdPicture
 
       Dim mDC As Long
 
       Dim mDC As Long
 
        
 
        
       Set p = LoadPicture("J:\apachefriends\xampp\htdocs\wakjah\cigarman.jpg")
+
       Set p = LoadPicture("'''some path'''")
 
        
 
        
 
       mDC = CreateCompatibleDC(Me.hdc)
 
       mDC = CreateCompatibleDC(Me.hdc)
Line 58: Line 63:
 
       Set p = Nothing
 
       Set p = Nothing
 
   End Sub
 
   End Sub
 +
 +
===See also===
 +
 +
* {{API link|StretchBlt}}
 +
 +
[[Category:API_Calls]]

Latest revision as of 10:28, 20 November 2006

float
 This article is based on Visual Basic 6. Find other Visual Basic 6 articles.

What is StretchBlt?

StretchBlt is a GDI API that is very similar to BitBlt in its operation, except that it not only copies part of a DC to another DC, but it will also stretch it.

Simple StretchBlt Use

The following is a simple example of how to use stretchblt (along with a previous code sample - Drawing an image to a DC) in order to stretch an image to the form's size.

 Private Sub Form_Paint()
     Dim p As StdPicture
     Dim mDC As Long
     
     Set p = LoadPicture("some path")
     
     mDC = CreateCompatibleDC(Me.hdc)
     DeleteObject SelectObject(mDC, p.Handle)
     
     StretchBlt _
         Me.hdc, _
         0, 0, _
         ScaleX(Me.ScaleWidth, vbTwips, vbPixels), ScaleY(Me.ScaleHeight, vbTwips, vbPixels), _
         mDC, _
         0, 0, _
         ScaleX(p.Width, vbHimetric, vbPixels), ScaleY(p.Height, vbHimetric, vbPixels), _
         vbSrcCopy
     
     DeleteDC mDC
     
     Set p = Nothing
 End Sub

NOTE: You will need to replace "some path" in the LoadPicture call with an actual image path for this code to work.

Caveats and refinements of this method

You may have noticed, while playing with this code, that when you are stretching the image to a size smaller than original, you begin to get horrible drawing artifacts (especially with photos). But don't despair! MS thought of this and included two StretchBlt modes that eliminate this. To change the StretchBlt mode for a DC, you have to call SetStretchBltMode with one of the available modes. The two modes that eliminate this horrid effect are: COLORONCOLOR and HALFTONE. HALFTONE is higher quality but takes longer.

So here's the code:

 'Put below constants in Genaral -> Declarations
 Private Const HALFTONE As Long = 4
 Private Const COLORONCOLOR As Long = 3

 Private Sub Form_Paint()
     Dim p As StdPicture
     Dim mDC As Long
     
     Set p = LoadPicture("some path")
     
     mDC = CreateCompatibleDC(Me.hdc)
     DeleteObject SelectObject(mDC, p.Handle)
     
     SetStretchBltMode Me.hdc, HALFTONE
     StretchBlt _
         Me.hdc, _
         0, 0, _
         ScaleX(Me.ScaleWidth, vbTwips, vbPixels), ScaleY(Me.ScaleHeight, vbTwips, vbPixels), _
         mDC, _
         0, 0, _
         ScaleX(p.Width, vbHimetric, vbPixels), ScaleY(p.Height, vbHimetric, vbPixels), _
         vbSrcCopy
     
     DeleteDC mDC
     
     Set p = Nothing
 End Sub

See also