CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: StretchBlt

  1. #1
    Join Date
    Jun 2001
    Location
    Sweden
    Posts
    5

    StretchBlt

    Hi all Guru's,

    I need som help with the API call StretchBlt. I use it to scale a picture from one picturebox to another. This works fine... no to the problem. It only works on Windows 2000(so far). I have also tested it on Windows 95 and Windows Me and it just gives me a White area instead of my beautiful picture. Are there any problems with this API call and different OS's. And if there is... can I do it another way ?? I tried to putting the picture into an Image control which scales the picture but the resolution is crappy. Can't see anything... no good. Any ideas out there.

    Best regards
    Hogger



  2. #2
    Join Date
    Apr 2000
    Location
    South Carolina,USA
    Posts
    2,210

    Re: StretchBlt

    I use the StretchBlt copying pictureboxes on Windows ME with no problems resizeing at will. Perhaps playing with autosize or Redraw properties or maybe the picture is coming outside the bounds of the Target picturebox.
    below is a sample running program.
    You need two picture boxes and a command button with a picture in Picture1

    option Explicit
    Const SRCCOPY = &HCC0020 ' Copies image exactly
    Const TwipsperInch = 1440
    private Declare Function CreateCompatibleDC Lib "gdi32" (byval hdc as Long) as Long
    private Declare Function SelectObject Lib "gdi32" (byval hdc as Long, byval hObject as Long) as Long
    private Declare Function DeleteObject Lib "gdi32" (byval hObject 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 nHght 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
    private Declare Function DeleteDC Lib "gdi32" (byval hdc as Long) as Long

    public Sub CopyPictureToTarget(Src as PictureBox, tgt as Variant, l, t, w, h)
    ' Do some Magic
    Dim hMemoryDC as Long
    Dim hOldBitMap as Long
    Dim lret
    ' Do some Magic
    hMemoryDC = CreateCompatibleDC(Src.hdc)
    hOldBitMap = SelectObject(hMemoryDC, Src.Picture)
    '
    ' Copy picture to the target hdc

    lret = StretchBlt(tgt.hdc, l, t, w, h, _
    hMemoryDC, 0, 0, Src.ScaleWidth, _
    Src.ScaleHeight, SRCCOPY)
    '
    If lret = 0 then Stop
    ' Restore the magic
    hOldBitMap = SelectObject(hMemoryDC, hOldBitMap)
    ' Cleanup
    lret = DeleteDC(hMemoryDC)

    End Sub

    private Sub Command1_Click()
    Dim OneInchX as Long
    Dim OneInchY as Long

    OneInchX = TwipsperInch / Screen.TwipsPerPixelY
    OneInchY = TwipsperInch / Screen.TwipsPerPixelY
    print OneInchX; " "; OneInchY
    Picture1.ScaleMode = vbPixels
    Picture2.ScaleMode = vbPixels
    CopyPictureToTarget Picture1, Picture2, 0, 0, OneInchX, OneInchY
    Picture2.Refresh
    End Sub




    John G

  3. #3
    Join Date
    Jun 2001
    Location
    Sweden
    Posts
    5

    Re: StretchBlt

    Thanks John,

    It seams to work. Only tried it on Windows Me. I didn't use the CreateCompatibleDC, SelectObject stuff you used. That might be my mistake. Anyway thanks a lot.... you just made my life a little easier... :-).

    /Hogger


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

Featured