复制图片
接上个帖子:http://bbs.csdn.net/topics/390964203?page=1#post-398764714
问题是如何将picture2内显示的裁剪后的图形复制到picture3内,与picture3的源图片合为一体,进行保存为图片。
8楼的最后一张图片看起来是裁剪后的图片是显示在picture3内,实际是显示在picture2内。
现在我把picture2的图形转移到picture3内时,转移的却还是picture1内没裁剪以前的图片。
难道SetWindowRgn 函数只能是用来观看效果的? 不能用来保存裁剪后的图片?
为了大家方便调试,我把源码全部放出,picture1装入一个需要裁剪的图片,picture2 显示裁剪后的图片,picture3装入一个背景图片。picture2放在picture3上面,是上下层的关系,不要放入picture3内。
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Type Rect
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function CreatePolygonRgn Lib "gdi32" (lpPoint As POINTAPI, ByVal nCount As Long, ByVal nPolyFillMode As Long) As Long
Private Declare Function SetWindowRgn Lib "user32" (ByVal hwnd As Long, ByVal hRgn As Long, ByVal bRedraw As Long) As Long
Private Declare Function SelectClipRgn Lib "gdi32" (ByVal hdc As Long, ByVal hRgn As Long) As Long
Private Declare Function GetRgnBox Lib "gdi32" (ByVal hRgn As Long, lpRect As Rect) As Long
Private Declare Function GetClipRgn Lib "gdi32" (ByVal hdc As Long, ByVal hRgn As Long) As Long
Private Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ReleaseCapture Lib "user32" () As Long
Dim blnDragging As Boolean
Dim offsetX As Single, offsetY As Single
Dim n As Integer
Dim ap() As POINTAPI
Private Sub Command1_Click()
Dim hRgn As Long
With Picture1
hRgn = CreatePolygonRgn(ap(0), n, 1)
SetWindowRgn .hwnd, hRgn, True '将图片框Pic裁剪为多边形
SelectClipRgn .hdc, hRgn
End With
hRgn = CreatePolygonRgn(ap(0), n, 1)
SetWindowRgn Picture2.hwnd, hRgn, True
SelectClipRgn Picture2.hdc, hRgn
Picture2.Picture = Picture1.Image
Picture3.AutoRedraw = True
SavePicture Picture3.Image, "d:/1234.bmp"
End Sub
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Picture1.PSet (X, Y)
ReDim Preserve ap(n)
ap(n).X = X
ap(n).Y = Y
If n > 0 Then Picture1.Line (ap(n - 1).X, ap(n - 1).Y)-(ap(n).X, ap(n).Y)
ap(n).Y = Y
n = n + 1
End Sub
Private Sub Form_Load()
Picture1.ForeColor = vbBlue
Picture1.DrawWidth = 2
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If blnDragging Then
Picture2.Move X - offsetX, Y - offsetY
End If
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
blnDragging = False
ReleaseCapture
End Sub
Private Sub Picture2_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
blnDragging = True
SetCapture Me.hwnd
offsetX = X
offsetY = Y
End If
End Sub