利用API实现图像的旋转
有人说GDI不能实现图像旋转,我不信。图像的旋转是通过API函数PlgBlt来实现的。下面是一个完整的类模块,将其编写成ActiveX DLL 文件供VB调用。2个全局函数提供了调用参数和说明,供VB工程引用:
Option Explicit
Private Declare Sub Sleep Lib "kernel32 " (ByVal dwMilliseconds As Long)
Private Declare Function PlgBlt Lib "gdi32 " (ByVal hdcDest As Long, _
lpPoint As POINTS2D, _
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
Private Const NotPI = 3.14/180
Private Type POINTS2D
x As Long
Y As Long
End Type
'图像的旋转
Function PictureRotate(ByRef picDestHdc As Long, xPos As Long, yPos As Long,ByVal Angle As Long,ByRef picSrcHdc As Long, srcXoffset As Long, srcYoffset As Long,ByVal SrcWidth As Long, ByVal SrcHeight As Long)
Dim Points(3) As POINTS2D
Dim DefPoints(3) As POINTS2D
Dim ThetS As Single, ThetC As Single
Dim ret As Long
On Error GoTo Lhandle
'SET LOCAL AXIS / ALIGNMENT
Points(0).x = -SrcWidth * 0.5
Points(0).Y = -SrcHeight * 0.5
Points(1).x = Points(0).x + SrcWidth
Points(1).Y = Points(0).Y
Points(2).x = Points(0).x
Points(2).Y = Points(0).Y + SrcHeight
'ROTATE AROUND Z-AXIS
ThetS = Sin(Angle * NotPI)
ThetC = Cos(Angle * NotPI)
DefPoints(0).x = (Points(0).x * ThetC - Points(0).Y * ThetS) + xPos
DefPoints(0).Y = (Points(0).x * ThetS + Points(0).Y * ThetC) + yPos
DefPoints(1).x = (Points(1).x * ThetC - Points(1).Y * ThetS) + xPos
DefPoints(1).Y = (Points(1).x * ThetS + Points(1).Y * ThetC) + yPos
DefPoints(2).x = (Points(2).x * ThetC - Points(2).Y * ThetS) + xPos
DefPoints(2).Y = (Points(2).x * ThetS + Points(2).Y * ThetC) + yPos
PlgBlt picDestHdc, DefPoints(0), picSrcHdc, srcXoffset, srcYoffset, SrcWidth, SrcHeight, 0, 0, 0
Lhandle:
MsgBox( "Error! ")
End Function
'从中心旋转图像
Public Function Rotate(DestX As Long, DestY As Long, SrcWidth As Long, SrcHeight As Long, picBack As Variant, picShow As
Variant, MyTJParam128 As Long)
'DestX,DestY分别是图像中心点X,Y座标
'SrcWidth,SrcHeight分别是图像宽度和高度
'picBack,picShow分别是背景图和前景图
'MyTJParam128为特技参数,延迟参数,Long型
Dim tel As Integer
'Dim TjPa As Integer
'hDC3 = Photo.Picture3.hDC
'hDC4 = Photo.Picture4.hDC
On Error GoTo Lhandle
tel = 0
'TjPa = MyTJParam128 * 3
Do While True
picBack.Cls
PictureRotate picBack.hDC, DestX, DestY, tel, picShow.hDC, 0, 0, SrcWidth, SrcHeight
picBack.Refresh
tel = tel + 10
If tel > 360 Then GoTo Lhandle
DoEvents
Sleep (MyTJParam128)
Loop
Lhandle:
Exit Function
End Function
'从左上角旋转引入
Public Function L_U_Rotate(DestX As Long, DestY As Long, SrcWidth As Long, SrcHeight As Long, picBack As Variant, picShow As
Variant, MyTJParam129 As Integer)
'DestX,DestY分别是图像中心点
'SrcWidth,SrcHeight分别是图像宽度和高度
'picBack,picShow分别是背景图和前景图
'MyTJParam129为特技参数,延迟参数
Dim tel As Integer
'Dim TjPa As Integer
Dim i As Integer, x As Integer, Y As Integer, s As Integer, R As Integer
'W1 = Photo.Picture1.ScaleWidth
'H1 = Photo.Picture1.ScaleHeight
'hDC2 = Photo.Picture2.hDC
'hDC1 = Photo.Picture1.hDC
R = DestX - DestY
On Error GoTo Lhandle
If R > 0 Then
x = R: s = DestY
Else
Y = -R: s = DestX
End If
'Photo.Picture1.AutoRedraw = True
'Photo.Picture2.AutoRedraw = True
'Photo.Picture2.AutoSize = True
'Photo.Picture1.AutoSize = True
tel = 0
'TjPa = MyTJParam129 * 10
chenjl1031: For i = 0 To s Step Int(s / 5)
For tel = 0 To 360 Step 12
picBack.Cls
PictureRotate picBack.hDC, CLng(i + x), CLng(i + Y), tel, picShow.hDC, 0, 0, SrcWidth, SrcHeight
picBack.Refresh
'i = i + Int(DestX / 40): j = j + Int(DestY / 40)
'If i > DestX Or j > DestY Then Exit Sub
DoEvents
Sleep (MyTJParam129)
Next tel
Next i
'Exit Sub
'chenjl1031:
' Photo.Picture1.AutoRedraw = False
' Photo.Picture2.AutoRedraw = False
' Photo.Picture2.AutoSize = False
' Photo.Picture1.AutoSize = False
Lhandle:
End Function
[解决办法]
很好。
只是在PictureRotate函数中错误处理处之前要加上exit function,否则,总是有报错。
[解决办法]
貌似这个应该放到API区...
而且是19日的帖子了,VB区真没什么人气....
[解决办法]
帮你顶...
[解决办法]
不错的帖子,顶
[解决办法]
肯定可以 旋转我看过一本 游戏编程的书 写过
[解决办法]
一定的顶一下了!
[解决办法]
可以的啊,我在MNDSOFT看到一个源代码的,翻转速度还很快。
[解决办法]
DIB最快
可以参考网上zyl910的代码
[解决办法]
UP,mark