Results 1 to 7 of 7

Thread: Anyone?? How does StretchBlt work?

  1. #1

    Thread Starter
    Fanatic Member arsmakman's Avatar
    Join Date
    Dec 2001
    Location
    Leiden, Netherlands.
    Posts
    719

    Question Anyone?? How does StretchBlt work?

    Hello,

    I need to resize an image to the size of a picturebox (or anything else) and then save it in that size, not in jpg, gif or whatever just bmp.
    I have search through old threads, but didn't find a real answer...
    I know it is possible with StretchBlt, but how??

    Thanx in advance.
    Last edited by arsmakman; Dec 22nd, 2001 at 05:31 AM.
    No matter how fool-proof your program is, there will always be a better fool.

    Was a post helpful to you? Rate it!

  2. #2

    Thread Starter
    Fanatic Member arsmakman's Avatar
    Join Date
    Dec 2001
    Location
    Leiden, Netherlands.
    Posts
    719
    Hello?
    No matter how fool-proof your program is, there will always be a better fool.

    Was a post helpful to you? Rate it!

  3. #3

    Thread Starter
    Fanatic Member arsmakman's Avatar
    Join Date
    Dec 2001
    Location
    Leiden, Netherlands.
    Posts
    719
    Why won't anyone explain StretchBlt to me?
    No matter how fool-proof your program is, there will always be a better fool.

    Was a post helpful to you? Rate it!

  4. #4
    Addicted Member
    Join Date
    Apr 2001
    Location
    Phoenix
    Posts
    150
    I have never used that api nor do I know how it works, but I would suggest searching http://www.vbapi.com or http://www.pscode.com. Search on these and see what you find.
    Patience and perserverence have a magical effect before which difficulties disappear and obstacles vanish.
    John Quincy Adams

  5. #5
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    We had a long discussion about StretchBlt once in this forum (it must be a couple of years ago ) and, believe it or not, we found out that using PaintPicture is faster.
    (Just because I mention it this thread will probably turn into a new long discussion about this fact but guys try it and you'll see).
    The regular BitBlt is much faster though.

    Anyway to answer your question I will show you both StretchBlt and PaintPicture.
    To use the API you first have to declare it
    VB Code:
    1. Private Declare Function StretchBlt _
    2.  Lib "gdi32" ( _
    3.  ByVal hdc As Long, _
    4.  ByVal x As Long, _
    5.  ByVal y As Long, _
    6.  ByVal nWidth As Long, _
    7.  ByVal nHeight As Long, _
    8.  ByVal hSrcDC As Long, _
    9.  ByVal xSrc As Long, _
    10.  ByVal ySrc As Long, _
    11.  ByVal nSrcWidth As Long, _
    12.  ByVal nSrcHeight As Long, _
    13.  ByVal dwRop As Long) As Long
    Now, not to make this post to long just try the following:
    Put the above declaration in the General Declaration section of a Form.
    Add two picture boxes to that form.
    One of the picture boxes we will use to load the image in it's normal size and format.
    This picturebox don't have to be visible so set the Visible property to False.
    It must resize though to the same size as the image we load into it so set the AutoSize property to True.
    Also since the StretchBlt function uses Pixels as messurment set the ScaleMode property of both PictureBoxes to vbPixels.

    The second PictureBox we use to resize the image.
    So size it to any size you like.
    Now since we're going to draw at it set the AutoRedraw property to True.
    If you forgot it or didn't read it a few lines above: Make sure the ScaleMode property is set to Pixels.

    Now in the code I'm using I've named the PictureBoxes to picOriginal and picStretch (guess which is which )
    In this example I use the Click event of a CommandButton to do the stretch and save...
    VB Code:
    1. Private Sub Command1_Click()
    2.     picOriginal.Picture = LoadPicture("c:\ThePic.gif") 'or any supported picture format
    3.     With picStretch
    4.         StretchBlt .hdc, 0, 0, .ScaleWidth, .ScaleHeight, _
    5.                    picOriginal.hdc, 0, 0, _
    6.                    picOriginal.ScaleWidth, picOriginal.ScaleHeight, _
    7.                    vbSrcCopy
    8.         .Refresh
    9.     End With
    10.     SavePicture picStretch.Image, "c:\stretchImage.bmp"
    11. End Sub
    And there you go....
    If you would like to test the PaintPicture function instead simply change the code in the Command1_Click event to this:
    VB Code:
    1. Private Sub Command1_Click()
    2.     picOriginal.Picture = LoadPicture("c:\ThePic.gif") 'or any supported picture format
    3.     With picStretch
    4.         .PaintPicture picOriginal.Picture, 0, 0, .ScaleWidth, .ScaleHeight, _
    5.                       0, 0, picOriginal.ScaleWidth, picOriginal.ScaleHeight, _
    6.                       vbSrcCopy
    7.         .Refresh
    8.     End With
    9.     SavePicture picStretch.Image, "c:\stretchImage.bmp"
    10. End Sub
    and if you use the PaintPicture method you don't need the API declaration.

    Best regards

  6. #6

    Thread Starter
    Fanatic Member arsmakman's Avatar
    Join Date
    Dec 2001
    Location
    Leiden, Netherlands.
    Posts
    719
    These codes don't work...

    They give no image and saves a picture of an empty picturebox.
    No matter how fool-proof your program is, there will always be a better fool.

    Was a post helpful to you? Rate it!

  7. #7
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649
    Set the AutoRedraw property of picOriginal to True as well and it will work.

    Best regards

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width