I’ve been trying to scale a image that is filling a canvas on MouseDrag. I’m resizing the window manually on MouseDrag, and the image is a picture property of the window containing the canvas.
I’ve been trying to use the StretchBlt function to accomplish this because the quality of a BitBlt capture was somewhat better than what I was able to get using DrawInto.
My problem is when I am resizing the window and scaling the image during MouseDrag the image gets stretched out incorrectly. I have code that recalculates and paints to the canvas on MouseUp that resets it correctly but I want the image to scale properly as the canvas is resized.
I’ve modified some code I found elsewhere on here to use the StretchBlt function:
Function ScaleRect(X As Integer, Y As Integer, Handle As Int32, Width As Integer, Height As Integer, OldWidth As Integer, OldHeight As Integer) As Picture
#If TargetWin32 Then
Declare Function GetDesktopWindow Lib “User32” () As Integer
Declare Function GetDC Lib “User32” (HWND As Integer) As Integer
Declare Function StretchBlt Lib “GDI32” (DCdest As Integer, nxOriginDest As Integer, nyOriginDest As Integer, nWidthDest As Integer, _
nHeightDest As Integer, DCsource As Integer, nxOriginSource As Integer, nyOriginSource As Integer, nWidthSource As Integer, _
nHeightSource As Integer, rasterOp As Integer) As Boolean
Declare Function ReleaseDC Lib “User32” (HWND As Integer, DC As Integer) As Integer
Const SRCCOPY = &h00CC0020
Const CAPTUREBLT = &h40000000
If Width = 0 Or Height = 0 Then Return Nil
Dim ret As Picture = New Picture(Width, Height, 24)
Dim HWND As Integer = Handle
Dim SourceDC As Integer = GetDC(HWND)
Dim DestDC As Integer = screenCap.Graphics.Handle(screenCap.Graphics.HandleTypeHDC)
Call StretchBlt(DestDC, X, Y, Width, Height, SourceDC, X, Y, oldWidth, oldHeight, SRCCOPY)
Call ReleaseDC(HWND, SourceDC)
On MouseDrag I’m setting the previous Width and Height of the canvas to variables before calling the scaling function using the current sizes.
oldWidth = cnvBackground.Width oldHeight = cnvBackground.Height ////////////////////////////////////////////// ' Window resize stuff here... ///////////////////////////////////////////// ' Scale image and paint to canvas p = ScaleRect(0, 0, cnvBackground.Handle, cnvBackground.Width, cnvBackground.Height, oldWidth, oldHeight)
Any help would be appreciated.