Const SRCCOPY = &HCC0020 Const Pi = 3.14159265359 Private Declare Function SetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y 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 nHeight 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 Sub Bmp_Rotate(pic1 As PictureBox, pic2 As PictureBox, ByVal theta!) Dim clx As Integer, cly As Integer Dim c2x As Integer, c2y As Integer Dim a As Single Dim p1x As Integer, ply As Integer Dim p2x As Integer, p2y As Integer Dim n As Integer Dim r As Integer Dim pic1hdc As Long Dim pic2hdc As Long clx = pic1.ScaleWidth / 2 cly = pic1.ScaleHeight / 2 c2x = pic2.ScaleWidth / 2 c2y = pic2.ScaleHeight / 2 If c2x < c2y Then n = c2y Else n = c2x n = n - 1 pic1hdc = pic1.hdc pic2hdc = pic2.hdc For p2x = 0 To n For p2y = 0 To n If p2x = 0 Then a = Pi / 2 Else a = Atn(p2y / p2x) r = Sqr(1& * p2x * p2x + 1& * p2y * p2y) plx = r * Cos(a + theta!) ply = r * Sin(a + theta!) c0& = GetPixel(pic1hdc, clx + plx, cly + ply) c1& = GetPixel(pic1hdc, clx - plx, cly - ply) c2& = GetPixel(pic1hdc, clx + ply, cly - plx) c3& = GetPixel(pic1hdc, clx - ply, cly + plx) If c0& <> -1 Then xret& = SetPixel(pic2hdc, c2x + p2x, c2y + p2y, c0&) If c1& <> -1 Then xret& = SetPixel(pic2hdc, c2x - p2x, c2y - p2y, c1&) If c2& <> -1 Then xret& = SetPixel(pic2hdc, c2x + p2y, c2y - p2x, c2&) If c3& <> -1 Then xret& = SetPixel(pic2hdc, c2x - p2y, c2y + p2x, c3&) Next p2y t% = DoEvents() Next p2x End Sub 旋转90度 Private Sub Command1_Click() Picture2.Cls Call Bmp_Rotate(Picture1, Picture2, -Pi / 2) Picture2.Refresh End Sub'垂直倒置 Private Sub Command2_Click() Picture2.Cls px% = Picture1.ScaleWidth py% = Picture1.ScaleHeight retval% = StretchBlt(Picture2.hdc, 0, py%, px%, -py%, Picture1.hdc, 0, 0, px%, py%, SRCCOPY) End Sub'旋转-45度 Private Sub Command3_Click() Picture2.Cls Call Bmp_Rotate(Picture1, Picture2, Pi / 4) Picture2.Refresh End SubPrivate Sub Form_Load() Picture1.ScaleMode = 3 Picture2.ScaleMode = 3 Picture2.AutoRedraw = True End Sub
【声明】 Public Declare Function PlgBlt Lib "gdi32" Alias "PlgBlt" (ByVal hdcDest As Long, lpPoint As POINTAPI, ByVal hdcSrc As Long, ByVal nXSrc As Long, ByVal nYSrc As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hbmMask As Long, ByVal xMask As Long, ByVal yMask As Long) As Long 【说明】 复制一幅位图,同时将其转换成一个平行四边形。利用它可对位图进行旋转处理 【返回值】 Long,非零表示成功,零表示失败。会设置GetLastError 【参数表】 hdcDest -------- Long,图象使用的目标设备场景 lpPoint -------- POINTAPI,POINTAPI结构数组中使用的第一个条目。第一个点对应于一个平行四边形左上角位置;第二个点代表右下角位置;第三个点代表左下角位置;第四个点是在前三个点的基础上导出的 hdcSrc --------- Long,图象的源设备场景 nXSrc,nYSrc ---- Long,源图象左上角的x,y坐标,采用逻辑坐标系统表示 nWidth,nHeight - Long,源图象大小,用逻辑坐标表示 hbmMask -------- Long,一个可选的句柄,指向一个单色掩模。如设定了这个参数,那么只有与掩模值1对应的二进制位才会传输到目的地 xMask,yMask ---- Long,掩模位图欲使用区域左上角的x,y坐标 适用平台 Windows NT 【其它】 如果对源图象应用了旋转或剪切处理,这个函数的执行就会失败。可用GetDeviceCaps判断这个函数是否得到了一个特定设备场景的支持
看了这里贴的这么多代码,简直就是,说得不客气点,乱答!只有Microsoft的代码好点,不过用BitBlt拷贝一点简直是浪费,看看这个代码: http://expert.csdn.net/Expert/topic/900/900205.xml?temp=.3715021 这是没有二次插值的任意角度旋转(当然是包括90度了),速度不是很快,200×300的图片旋转45度500ms左右,但是也比上面的代码快, 再看这里(声明,是模仿叶子的代码): Function CircumvolvingBits(FromPicture As PictureBox, ToPicture As PictureBox, Angle As Single, Zoom As Boolean) On Error Resume Next Const Pi = 3.14159265358979 '定义的Pi值,好象是越多越好,于是就定义了这么多位:) Dim i As Long, j As Long, k As Long Dim AngleA As Single Dim X As Long, Y As Long Dim XsFalse As Single, YsFalse As Single Dim DisXsFalse As Single, DisYsFalse As Single Dim HalfWidth As Long, HalfHeight As Long, DisPlusWidth As Long, DisPlusHeight As Long Dim X1 As Long, Y1 As Long Dim AmendX As Long, AmendY As Long Dim X2 As Single, Y2 As Single Dim X3 As Single, Y3 As Single Dim CulRGB0 As Long, CulRGB As Long, CulRGB1 As Long Dim bit2Width As Long Dim bitWidth As Long Dim hOldMap As Long Dim Pic2Bits() As Byte Dim PicBits() As Byte Dim iBitmap As Long, iDC As Long Dim i2Bitmap As Long, i2DC As Long Dim bi24BitInfo As BITMAPINFO Dim bi24Bit2Info As BITMAPINFO Dim HuDu As Single Dim SinHuDu As Single, CosHuDu As Single '清除图片框ToPicture ToPicture.Cls '将角度转换为弧度 FromPicture.ScaleMode = vbPixels ToPicture.ScaleMode = vbPixels HuDu = Angle * Pi / 180 AngleA = Angle If Zoom = True Then If Angle < 90 Then HuDu = Angle * Pi / 180 ToPicture.Width = FromPicture.Width * Cos(HuDu) + FromPicture.Height * Sin(HuDu) ToPicture.Height = FromPicture.Height * Cos(HuDu) + FromPicture.Width * Sin(HuDu) If Angle = 0 Then HuDu = Angle * Pi / 180 ToPicture.Width = FromPicture.Width ToPicture.Height = FromPicture.Height End If Else If Angle = 90 Then HuDu = Angle * Pi / 180 ToPicture.Width = FromPicture.Height ToPicture.Height = FromPicture.Width End If If Angle < 180 And Angle > 90 Then Angle = Angle - 90 HuDu = Angle * Pi / 180 ToPicture.Width = FromPicture.Height * Cos(HuDu) + FromPicture.Width * Sin(HuDu) ToPicture.Height = FromPicture.Width * Cos(HuDu) + FromPicture.Height * Sin(HuDu) Else If Angle = 180 Then HuDu = Angle * Pi / 180 ToPicture.Width = FromPicture.Width ToPicture.Height = FromPicture.Height End If If Angle < 270 And Angle > 180 Then Angle = Angle - 180 HuDu = Angle * Pi / 180 ToPicture.Width = FromPicture.Width * Cos(HuDu) + FromPicture.Height * Sin(HuDu) ToPicture.Height = FromPicture.Height * Cos(HuDu) + FromPicture.Width * Sin(HuDu) Else If Angle = 270 Then HuDu = Angle * Pi / 180 ToPicture.Width = FromPicture.Height ToPicture.Height = FromPicture.Width End If If Angle < 360 And Angle > 270 Then Angle = Angle - 270 HuDu = Angle * Pi / 180 ToPicture.Width = FromPicture.Height * Cos(HuDu) + FromPicture.Width * Sin(HuDu) ToPicture.Height = FromPicture.Width * Cos(HuDu) + FromPicture.Height * Sin(HuDu) End If End If End If End If End If HuDu = (360 - AngleA) * Pi / 180 CosHuDu = Cos(-HuDu) SinHuDu = Sin(-HuDu) Select Case AngleA Case 0 CosHuDu = 1 SinHuDu = 0 Case 90 CosHuDu = 0 SinHuDu = 1 AmendY = 1 Case 180 CosHuDu = -1 SinHuDu = 0 AmendY = 1 Case 270 CosHuDu = 0 SinHuDu = -1 AmendX = 1 Case 360 CosHuDu = 1 SinHuDu = 0 End Select Angle = AngleA With bi24BitInfo.bmiHeader '定义一个DIB位图结构 .biBitCount = 32 '定义为32位的DIB位图(很方便,以前我用24位的走了不少弯路),32位DIB位图每个象素有四个字节,分别是R、G、B、Alpha .biCompression = BI_RGB .biPlanes = 1 .biSize = Len(bi24BitInfo.bmiHeader) .biWidth = FromPicture.ScaleWidth .biHeight = FromPicture.ScaleHeight .biSizeImage = .biWidth * 4 * .biHeight End With iDC = CreateCompatibleDC(0) '创建位图hDC iBitmap = CreateDIBSection(iDC, bi24BitInfo, DIB_RGB_COLORS, ByVal 0&, ByVal 0&, ByVal 0&) '创建位图 If iBitmap Then hOldMap = SelectObject(iDC, iBitmap) '使i2DC与i2Bitmap建立关联 Else DeleteObject iDC Exit Function End If With bi24Bit2Info.bmiHeader '定义一个DIB位图结构 .biBitCount = 32 '定义为32位的DIB位图(很方便,以前我用24位的走了不少弯路),32位DIB位图每个象素有四个字节,分别是R、G、B、Alpha .biCompression = BI_RGB .biPlanes = 1 .biSize = Len(bi24BitInfo.bmiHeader) .biWidth = ToPicture.ScaleWidth .biHeight = ToPicture.ScaleHeight .biSizeImage = .biWidth * 4 * .biHeight End With i2DC = CreateCompatibleDC(0) '创建位图hDC i2Bitmap = CreateDIBSection(i2DC, bi24Bit2Info, DIB_RGB_COLORS, ByVal 0&, ByVal 0&, ByVal 0&) '创建位图 If i2Bitmap Then hOldMap = SelectObject(i2DC, i2Bitmap) '使i2DC与i2Bitmap建立关联 Else DeleteObject i2DC Exit Function End If BitBlt iDC, 0, 0, bi24BitInfo.bmiHeader.biWidth, bi24BitInfo.bmiHeader.biHeight, FromPicture.hDC, 0, 0, vbSrcCopy '将FromPicture上的图象拷贝到iDC中 bitWidth = bi24BitInfo.bmiHeader.biWidth * 4 '横向的字节总数 ReDim PicBits(1 To 4, 1 To bi24BitInfo.bmiHeader.biWidth, 1 To bi24BitInfo.bmiHeader.biHeight) As Byte '重新定义动态数组 GetBitmapBits iBitmap, bi24BitInfo.bmiHeader.biSizeImage, PicBits(1, 1, 1) '将iBitmap读取到PicBits数组中 BitBlt i2DC, 0, 0, bi24Bit2Info.bmiHeader.biWidth, bi24Bit2Info.bmiHeader.biHeight, ToPicture.hDC, 0, 0, vbSrcCopy '将ToPicture上的图象拷贝到i2DC中 bit2Width = bi24Bit2Info.bmiHeader.biWidth * 4 '横向的字节总数 ReDim Pic2Bits(1 To 4, 1 To bi24Bit2Info.bmiHeader.biWidth, 1 To bi24Bit2Info.bmiHeader.biHeight) As Byte '重新定义动态数组 GetBitmapBits i2Bitmap, bi24Bit2Info.bmiHeader.biSizeImage, Pic2Bits(1, 1, 1) '将i2Bitmap读取到Pic2Bits数组中 '逐点旋转象素,并逐点复制 HalfWidth = ToPicture.ScaleWidth / 2 HalfHeight = ToPicture.ScaleHeight / 2 DisPlusWidth = FromPicture.ScaleWidth / 2 DisPlusHeight = FromPicture.ScaleHeight / 2 For X = 0 To ToPicture.ScaleWidth X3 = X - HalfWidth For Y = 0 To ToPicture.ScaleHeight Y3 = Y - HalfHeight X2 = X3 * CosHuDu - Y3 * SinHuDu + DisPlusWidth Y2 = Y3 * CosHuDu + X3 * SinHuDu + DisPlusHeight X1 = Int(X2) Y1 = Int(Y2) If Angle = 0 Or Angle = 90 Or Angle = 180 Or Angle = 270 Or Angle = 360 Then For i = 1 To 3 Pic2Bits(i, X + AmendX, Y + AmendY) = PicBits(i, X1, Y1) Next i Else If (X1 > 0) And (X1 < FromPicture.ScaleWidth) And (Y1 > 0) And (Y1 < FromPicture.ScaleHeight) Then XsFalse = X2 - X1 YsFalse = Y2 - Y1 DisXsFalse = 1 - XsFalse DisYsFalse = 1 - YsFalse For i = 1 To 3 CulRGB = DisXsFalse * PicBits(i, X1, Y1) CulRGB0 = CulRGB + XsFalse * PicBits(i, X1 + 1, Y1) CulRGB = DisXsFalse * PicBits(i, X1, Y1 + 1) CulRGB1 = CulRGB + XsFalse * PicBits(i, X1 + 1, Y1 + 1) CulRGB = DisYsFalse * CulRGB0 + YsFalse * CulRGB1 Pic2Bits(i, X, Y) = ksCheckBounds(CulRGB) Next i End If End If Next Y Next X SetBitmapBits i2Bitmap, bi24Bit2Info.bmiHeader.biSizeImage, Pic2Bits(1, 1, 1) '将Pic2Bits赋与i2Bitmap BitBlt ToPicture.hDC, 0, 0, bi24Bit2Info.bmiHeader.biWidth, bi24Bit2Info.bmiHeader.biHeight, i2DC, 0, 0, vbSrcCopy '将i2DC拷贝到ToPicture中 '释放对象 If hOldMap Then DeleteObject SelectObject(iDC, hOldMap) If hOldMap Then DeleteObject SelectObject(i2DC, hOldMap) DeleteObject iDC ' DeleteObject i2DC ToPicture.Refresh CircumvolvingBits = True '返回真值 End Function
Const Pi = 3.14159265359
Private Declare Function SetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y 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 nHeight 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
Sub Bmp_Rotate(pic1 As PictureBox, pic2 As PictureBox, ByVal theta!)
Dim clx As Integer, cly As Integer
Dim c2x As Integer, c2y As Integer
Dim a As Single
Dim p1x As Integer, ply As Integer
Dim p2x As Integer, p2y As Integer
Dim n As Integer
Dim r As Integer
Dim pic1hdc As Long
Dim pic2hdc As Long
clx = pic1.ScaleWidth / 2
cly = pic1.ScaleHeight / 2
c2x = pic2.ScaleWidth / 2
c2y = pic2.ScaleHeight / 2
If c2x < c2y Then n = c2y Else n = c2x
n = n - 1
pic1hdc = pic1.hdc
pic2hdc = pic2.hdc
For p2x = 0 To n
For p2y = 0 To n
If p2x = 0 Then a = Pi / 2 Else a = Atn(p2y / p2x)
r = Sqr(1& * p2x * p2x + 1& * p2y * p2y)
plx = r * Cos(a + theta!)
ply = r * Sin(a + theta!)
c0& = GetPixel(pic1hdc, clx + plx, cly + ply)
c1& = GetPixel(pic1hdc, clx - plx, cly - ply)
c2& = GetPixel(pic1hdc, clx + ply, cly - plx)
c3& = GetPixel(pic1hdc, clx - ply, cly + plx)
If c0& <> -1 Then xret& = SetPixel(pic2hdc, c2x + p2x, c2y + p2y, c0&)
If c1& <> -1 Then xret& = SetPixel(pic2hdc, c2x - p2x, c2y - p2y, c1&)
If c2& <> -1 Then xret& = SetPixel(pic2hdc, c2x + p2y, c2y - p2x, c2&)
If c3& <> -1 Then xret& = SetPixel(pic2hdc, c2x - p2y, c2y + p2x, c3&)
Next p2y
t% = DoEvents()
Next p2x
End Sub
旋转90度
Private Sub Command1_Click()
Picture2.Cls
Call Bmp_Rotate(Picture1, Picture2, -Pi / 2)
Picture2.Refresh
End Sub'垂直倒置
Private Sub Command2_Click()
Picture2.Cls
px% = Picture1.ScaleWidth
py% = Picture1.ScaleHeight
retval% = StretchBlt(Picture2.hdc, 0, py%, px%, -py%, Picture1.hdc, 0, 0, px%, py%, SRCCOPY)
End Sub'旋转-45度
Private Sub Command3_Click()
Picture2.Cls
Call Bmp_Rotate(Picture1, Picture2, Pi / 4)
Picture2.Refresh
End SubPrivate Sub Form_Load()
Picture1.ScaleMode = 3
Picture2.ScaleMode = 3
Picture2.AutoRedraw = True
End Sub
Public Declare Function PlgBlt Lib "gdi32" Alias "PlgBlt" (ByVal hdcDest As Long, lpPoint As POINTAPI, ByVal hdcSrc As Long, ByVal nXSrc As Long, ByVal nYSrc As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hbmMask As Long, ByVal xMask As Long, ByVal yMask As Long) As Long
【说明】
复制一幅位图,同时将其转换成一个平行四边形。利用它可对位图进行旋转处理
【返回值】
Long,非零表示成功,零表示失败。会设置GetLastError
【参数表】
hdcDest -------- Long,图象使用的目标设备场景 lpPoint -------- POINTAPI,POINTAPI结构数组中使用的第一个条目。第一个点对应于一个平行四边形左上角位置;第二个点代表右下角位置;第三个点代表左下角位置;第四个点是在前三个点的基础上导出的 hdcSrc --------- Long,图象的源设备场景 nXSrc,nYSrc ---- Long,源图象左上角的x,y坐标,采用逻辑坐标系统表示 nWidth,nHeight - Long,源图象大小,用逻辑坐标表示 hbmMask -------- Long,一个可选的句柄,指向一个单色掩模。如设定了这个参数,那么只有与掩模值1对应的二进制位才会传输到目的地 xMask,yMask ---- Long,掩模位图欲使用区域左上角的x,y坐标
适用平台
Windows NT
【其它】
如果对源图象应用了旋转或剪切处理,这个函数的执行就会失败。可用GetDeviceCaps判断这个函数是否得到了一个特定设备场景的支持
PrivateConstSRCCOPY=&HCC0020
PrivateConstPi=3.14PrivateDeclareFunctionSetPixelLib"gdi32"(ByValhdcAsLong, ByValxAsLong,ByValyAsLong,ByValcrColorAsLong)AsLong
PrivateDeclareFunctionGetPixelLib"gdi32"(ByValhdcAsLong, ByValxAsLong,ByValyAsLong)AsLongPrivateDeclareFunctionStretchBltLib"gdi32"(ByValhdcAsLong, ByValxAsLong,ByValyAsLong,ByValnWidthAsLong,ByValnHeightAsLong, ByValhSrcDCAsLong,ByValxSrcAsLong,ByValySrcAsLong,ByValnSrcWidth AsLong,ByValnSrcHeightAsLong,ByValdwRopAsLong)AsLongprivateSubbmp_rotate(pic1AsPictureBox,pic2AsPictureBox,ByValtheta)‘45度旋转
Dimc1xAsInteger,c1yAsInteger
Dimc2xAsInteger,c2yAsInteger
DimaAsSingle
Dimp1xAsInteger,p1yAsInteger
Dimp2xAsInteger,p2yAsInteger
DimnAsInteger,rAsInteger c1x=pic1.ScaleWidth\2
c1y=pic1.ScaleHeight\2
c2x=pic2.ScaleWidth\2
c2y=pic2.ScaleHeight\2
Ifc2x<c2yThenn=c2yElsen=c2x
n=n-1
pic1hDC=pic1.hdc
pic2hDC=pic2.hdc
Forp2x=0Ton
Forp2y=0Ton
Ifp2x=0Thena=Pi/2Elsea=Atn(p2y/p2x)
r=Sqr(1&*p2x*p2x+1&*p2y*p2y)
p1x=r*Cos(a+theta)
p1y=r*Sin(a+theta)
c0&=GetPixel(pic1hDC,c1x+p1x,c1y+p1y)
c1&=GetPixel(pic1hDC,c1x-p1x,c1y-p1y)
c2&=GetPixel(pic1hDC,c1x+p1y,c1y-p1x)
c3&=GetPixel(pic1hDC,c1x-p1y,c1y+p1x)
Ifc0&<>-1ThenSetPixelpic2hDC,c2x+p2x,c2y+p2y,c0
Ifc1&<>-1ThenSetPixelpic2hDC,c2x-p2x,c2y-p2y,c1
Ifc2&<>-1ThenSetPixelpic2hDC,c2x+p2y,c2y-p2x,c2
Ifc3&<>-1ThenSetPixelpic2hDC,c2x-p2y,c2y+p2x,c3
Next
Next
EndSubPrivateSubCommand1_Click()‘正常复制
Picture2.Cls
px=Picture1.ScaleWidth
py=Picture1.ScaleHeight
StretchBltPicture2.hdc,px,0,-px,py,Picture1.hdc,0,0,px,py,SRCCOPY
EndSubPrivateSubCommand2_Click()‘180度倒立
Picture2.Cls
px=Picture1.ScaleWidth
py=Picture1.ScaleHeight
StretchBltPicture2.hdc,0,py,px,-py,Picture1.hdc,0,0,px,py,SRCCOPY
EndSubPrivateSubCommand3_Click()‘45旋转
Picture2.Cls
Callbmp_rotate(Picture1,Picture2,3.14/4)
EndSubPrivateSubForm_Load()
OnErrorResumeNext
Me.Caption=App.Title"添加应用程序标题
Me.Left=(Screen.Width-Me.Width)/2
Me.Top=(Screen.Height-Me.Height)/2"窗体具中
Picture1.ScaleMode=3
Picture2.ScaleMode=3
EndSub
这个也行
http://www.yesky.com/20001031/128684.shtml
http://expert.csdn.net/Expert/topic/900/900205.xml?temp=.3715021
这是没有二次插值的任意角度旋转(当然是包括90度了),速度不是很快,200×300的图片旋转45度500ms左右,但是也比上面的代码快,
再看这里(声明,是模仿叶子的代码):
Function CircumvolvingBits(FromPicture As PictureBox, ToPicture As PictureBox, Angle As Single, Zoom As Boolean)
On Error Resume Next
Const Pi = 3.14159265358979 '定义的Pi值,好象是越多越好,于是就定义了这么多位:)
Dim i As Long, j As Long, k As Long
Dim AngleA As Single
Dim X As Long, Y As Long
Dim XsFalse As Single, YsFalse As Single
Dim DisXsFalse As Single, DisYsFalse As Single
Dim HalfWidth As Long, HalfHeight As Long, DisPlusWidth As Long, DisPlusHeight As Long
Dim X1 As Long, Y1 As Long
Dim AmendX As Long, AmendY As Long
Dim X2 As Single, Y2 As Single
Dim X3 As Single, Y3 As Single
Dim CulRGB0 As Long, CulRGB As Long, CulRGB1 As Long
Dim bit2Width As Long
Dim bitWidth As Long
Dim hOldMap As Long
Dim Pic2Bits() As Byte
Dim PicBits() As Byte
Dim iBitmap As Long, iDC As Long
Dim i2Bitmap As Long, i2DC As Long
Dim bi24BitInfo As BITMAPINFO
Dim bi24Bit2Info As BITMAPINFO
Dim HuDu As Single
Dim SinHuDu As Single, CosHuDu As Single
'清除图片框ToPicture
ToPicture.Cls
'将角度转换为弧度
FromPicture.ScaleMode = vbPixels
ToPicture.ScaleMode = vbPixels
HuDu = Angle * Pi / 180
AngleA = Angle
If Zoom = True Then
If Angle < 90 Then
HuDu = Angle * Pi / 180
ToPicture.Width = FromPicture.Width * Cos(HuDu) + FromPicture.Height * Sin(HuDu)
ToPicture.Height = FromPicture.Height * Cos(HuDu) + FromPicture.Width * Sin(HuDu)
If Angle = 0 Then
HuDu = Angle * Pi / 180
ToPicture.Width = FromPicture.Width
ToPicture.Height = FromPicture.Height
End If
Else
If Angle = 90 Then
HuDu = Angle * Pi / 180
ToPicture.Width = FromPicture.Height
ToPicture.Height = FromPicture.Width
End If
If Angle < 180 And Angle > 90 Then
Angle = Angle - 90
HuDu = Angle * Pi / 180
ToPicture.Width = FromPicture.Height * Cos(HuDu) + FromPicture.Width * Sin(HuDu)
ToPicture.Height = FromPicture.Width * Cos(HuDu) + FromPicture.Height * Sin(HuDu)
Else
If Angle = 180 Then
HuDu = Angle * Pi / 180
ToPicture.Width = FromPicture.Width
ToPicture.Height = FromPicture.Height
End If
If Angle < 270 And Angle > 180 Then
Angle = Angle - 180
HuDu = Angle * Pi / 180
ToPicture.Width = FromPicture.Width * Cos(HuDu) + FromPicture.Height * Sin(HuDu)
ToPicture.Height = FromPicture.Height * Cos(HuDu) + FromPicture.Width * Sin(HuDu)
Else
If Angle = 270 Then
HuDu = Angle * Pi / 180
ToPicture.Width = FromPicture.Height
ToPicture.Height = FromPicture.Width
End If
If Angle < 360 And Angle > 270 Then
Angle = Angle - 270
HuDu = Angle * Pi / 180
ToPicture.Width = FromPicture.Height * Cos(HuDu) + FromPicture.Width * Sin(HuDu)
ToPicture.Height = FromPicture.Width * Cos(HuDu) + FromPicture.Height * Sin(HuDu)
End If
End If
End If
End If
End If
HuDu = (360 - AngleA) * Pi / 180
CosHuDu = Cos(-HuDu)
SinHuDu = Sin(-HuDu)
Select Case AngleA
Case 0
CosHuDu = 1
SinHuDu = 0
Case 90
CosHuDu = 0
SinHuDu = 1
AmendY = 1
Case 180
CosHuDu = -1
SinHuDu = 0
AmendY = 1
Case 270
CosHuDu = 0
SinHuDu = -1
AmendX = 1
Case 360
CosHuDu = 1
SinHuDu = 0
End Select
Angle = AngleA
With bi24BitInfo.bmiHeader '定义一个DIB位图结构
.biBitCount = 32 '定义为32位的DIB位图(很方便,以前我用24位的走了不少弯路),32位DIB位图每个象素有四个字节,分别是R、G、B、Alpha
.biCompression = BI_RGB
.biPlanes = 1
.biSize = Len(bi24BitInfo.bmiHeader)
.biWidth = FromPicture.ScaleWidth
.biHeight = FromPicture.ScaleHeight
.biSizeImage = .biWidth * 4 * .biHeight
End With
iDC = CreateCompatibleDC(0) '创建位图hDC
iBitmap = CreateDIBSection(iDC, bi24BitInfo, DIB_RGB_COLORS, ByVal 0&, ByVal 0&, ByVal 0&) '创建位图
If iBitmap Then
hOldMap = SelectObject(iDC, iBitmap) '使i2DC与i2Bitmap建立关联
Else
DeleteObject iDC
Exit Function
End If
With bi24Bit2Info.bmiHeader '定义一个DIB位图结构
.biBitCount = 32 '定义为32位的DIB位图(很方便,以前我用24位的走了不少弯路),32位DIB位图每个象素有四个字节,分别是R、G、B、Alpha
.biCompression = BI_RGB
.biPlanes = 1
.biSize = Len(bi24BitInfo.bmiHeader)
.biWidth = ToPicture.ScaleWidth
.biHeight = ToPicture.ScaleHeight
.biSizeImage = .biWidth * 4 * .biHeight
End With
i2DC = CreateCompatibleDC(0) '创建位图hDC
i2Bitmap = CreateDIBSection(i2DC, bi24Bit2Info, DIB_RGB_COLORS, ByVal 0&, ByVal 0&, ByVal 0&) '创建位图
If i2Bitmap Then
hOldMap = SelectObject(i2DC, i2Bitmap) '使i2DC与i2Bitmap建立关联
Else
DeleteObject i2DC
Exit Function
End If
BitBlt iDC, 0, 0, bi24BitInfo.bmiHeader.biWidth, bi24BitInfo.bmiHeader.biHeight, FromPicture.hDC, 0, 0, vbSrcCopy '将FromPicture上的图象拷贝到iDC中
bitWidth = bi24BitInfo.bmiHeader.biWidth * 4 '横向的字节总数
ReDim PicBits(1 To 4, 1 To bi24BitInfo.bmiHeader.biWidth, 1 To bi24BitInfo.bmiHeader.biHeight) As Byte '重新定义动态数组
GetBitmapBits iBitmap, bi24BitInfo.bmiHeader.biSizeImage, PicBits(1, 1, 1) '将iBitmap读取到PicBits数组中
BitBlt i2DC, 0, 0, bi24Bit2Info.bmiHeader.biWidth, bi24Bit2Info.bmiHeader.biHeight, ToPicture.hDC, 0, 0, vbSrcCopy '将ToPicture上的图象拷贝到i2DC中
bit2Width = bi24Bit2Info.bmiHeader.biWidth * 4 '横向的字节总数
ReDim Pic2Bits(1 To 4, 1 To bi24Bit2Info.bmiHeader.biWidth, 1 To bi24Bit2Info.bmiHeader.biHeight) As Byte '重新定义动态数组
GetBitmapBits i2Bitmap, bi24Bit2Info.bmiHeader.biSizeImage, Pic2Bits(1, 1, 1) '将i2Bitmap读取到Pic2Bits数组中
'逐点旋转象素,并逐点复制
HalfWidth = ToPicture.ScaleWidth / 2
HalfHeight = ToPicture.ScaleHeight / 2
DisPlusWidth = FromPicture.ScaleWidth / 2
DisPlusHeight = FromPicture.ScaleHeight / 2
For X = 0 To ToPicture.ScaleWidth
X3 = X - HalfWidth
For Y = 0 To ToPicture.ScaleHeight
Y3 = Y - HalfHeight
X2 = X3 * CosHuDu - Y3 * SinHuDu + DisPlusWidth
Y2 = Y3 * CosHuDu + X3 * SinHuDu + DisPlusHeight
X1 = Int(X2)
Y1 = Int(Y2)
If Angle = 0 Or Angle = 90 Or Angle = 180 Or Angle = 270 Or Angle = 360 Then
For i = 1 To 3
Pic2Bits(i, X + AmendX, Y + AmendY) = PicBits(i, X1, Y1)
Next i
Else
If (X1 > 0) And (X1 < FromPicture.ScaleWidth) And (Y1 > 0) And (Y1 < FromPicture.ScaleHeight) Then
XsFalse = X2 - X1
YsFalse = Y2 - Y1
DisXsFalse = 1 - XsFalse
DisYsFalse = 1 - YsFalse
For i = 1 To 3
CulRGB = DisXsFalse * PicBits(i, X1, Y1)
CulRGB0 = CulRGB + XsFalse * PicBits(i, X1 + 1, Y1)
CulRGB = DisXsFalse * PicBits(i, X1, Y1 + 1)
CulRGB1 = CulRGB + XsFalse * PicBits(i, X1 + 1, Y1 + 1)
CulRGB = DisYsFalse * CulRGB0 + YsFalse * CulRGB1
Pic2Bits(i, X, Y) = ksCheckBounds(CulRGB)
Next i
End If
End If
Next Y
Next X
SetBitmapBits i2Bitmap, bi24Bit2Info.bmiHeader.biSizeImage, Pic2Bits(1, 1, 1) '将Pic2Bits赋与i2Bitmap
BitBlt ToPicture.hDC, 0, 0, bi24Bit2Info.bmiHeader.biWidth, bi24Bit2Info.bmiHeader.biHeight, i2DC, 0, 0, vbSrcCopy '将i2DC拷贝到ToPicture中
'释放对象
If hOldMap Then DeleteObject SelectObject(iDC, hOldMap)
If hOldMap Then DeleteObject SelectObject(i2DC, hOldMap)
DeleteObject iDC '
DeleteObject i2DC
ToPicture.Refresh
CircumvolvingBits = True '返回真值
End Function