重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
'GetPixel和SetPixel太慢了.系统有现成的API用. '本例子需要两个PictureBox,名称分别为PicBack和PicShow.一个CommandButton,名称:Command1. '在PicShow里载入一张图片,然后运行,点command1按钮,你就可以看到效果. Option Explicit Private Declare Function PlgBlt Lib "gdi32" (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 Private Type POINTAPI x As Long y As Long End Type Const NotPI = 3.14159265238 / 180 Private Sub DanRotate(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 POINTAPI Dim DefPoints(3) As POINTAPI Dim sSin As Single, sCos As Single Dim ret As Long 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 sSin = Sin(Angle * NotPI) sCos = Cos(Angle * NotPI) DefPoints(0).x = (Points(0).x * sCos - Points(0).y * sSin) + xPos DefPoints(0).y = (Points(0).x * sSin + Points(0).y * sCos) + yPos DefPoints(1).x = (Points(1).x * sCos - Points(1).y * sSin) + xPos DefPoints(1).y = (Points(1).x * sSin + Points(1).y * sCos) + yPos DefPoints(2).x = (Points(2).x * sCos - Points(2).y * sSin) + xPos DefPoints(2).y = (Points(2).x * sSin + Points(2).y * sCos) + yPos PlgBlt picDestHdc, DefPoints(0), picSrcHdc, srcXoffset, srcYoffset, srcWidth, srcHeight, 0, 0, 0 End Sub Private Sub Form_Load() picShow.ScaleMode = vbPixels picBack.ScaleMode = vbPixels End Sub Private Sub Command1_Click() DanRotate picBack.hDC, 100, 100, 45, picShow.hDC, 0, 0, picShow.ScaleWidth,picShow.ScaleHeight '(函数解释)DanRotate 目标(输出)的设备场景,X坐标,Y坐标,旋转角度,源设备场景,源设备场景X坐标,源设备场景Y坐标 picBack.Refresh '刷新窗体 End Sub
网站建设哪家好,找成都创新互联公司!专注于网页设计、网站建设、微信开发、成都微信小程序、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了广阳免费建站欢迎大家使用!
启动vb6建立一个标准exe工程,首先添加两个图片框(picture1和picture2),添加三个命令按钮command1(caption=“正常显示”)、command2(caption=“180度倒立”)、command3(caption=“45度旋转”),双击窗体,写入以下代码:
PrivateConstSRCCOPY=HCC0020
PrivateConstPi=3.14
PrivateDeclareFunctionSetPixelLib"gdi32"(ByValhdcAsLong, ByValxAsLong,ByValyAsLong,ByValcrColorAsLong)AsLong
PrivateDeclareFunctionGetPixelLib"gdi32"(ByValhdcAsLong, ByValxAsLong,ByValyAsLong)AsLong
PrivateDeclareFunctionStretchBltLib"gdi32"(ByValhdcAsLong, ByValxAsLong,ByValyAsLong,ByValnWidthAsLong,ByValnHeightAsLong, ByValhSrcDCAsLong,ByValxSrcAsLong,ByValySrcAsLong,ByValnSrcWidth AsLong,ByValnSrcHeightAsLong,ByValdwRopAsLong)AsLong
privateSubbmp_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$#@60;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$#@60;$#@62;-1ThenSetPixelpic2hDC,c2x+p2x,c2y+p2y,c0
Ifc1$#@60;$#@62;-1ThenSetPixelpic2hDC,c2x-p2x,c2y-p2y,c1
Ifc2$#@60;$#@62;-1ThenSetPi pic2hDC,c2x+p2y,c2y-p2x,c2
Ifc3$#@60;$#@62;-1ThenSetPixelpic2hDC,c2x-p2y,c2y+p2x,c3
Next
Next
EndSub
PrivateSubCommand1_Click()‘正常复制
Picture2.Cls
px=Picture1.ScaleWidth
py=Picture1.ScaleHeight
StretchBltPicture2.hdc,px,0,-px,py,Picture1.hdc,0,0,px,py,SRCCOPY
EndSub
PrivateSubCommand2_Click()‘180度倒立
Picture2.Cls
px=Picture1.ScaleWidth
py=Picture1.ScaleHeight
StretchBltPicture2.hdc,0,py,px,-py,Picture1.hdc,0,0,px,py,SRCCOPY
EndSub
PrivateSubCommand3_Click()‘45旋转
Picture2.Cls
Callbmp_rotate(Picture1,Picture2,3.14/4)
EndSub
PrivateSubForm_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
文字编辑结束后,选择“移动”工具(工具箱第一个工具,快捷键V),然后按Ctrl+T快捷键进行“自由变换”,会在文字上出现控制框,把鼠标移到控制框外边指针就会变成旋转的样式,按住鼠标左键拖动就可以进行任意角度旋转了。
这个问题不是小问题。如果只是90度转,vb.net的picture控件有RotateFlip方法。
任意角度,可以参考这个
;cs-lang=csharp
图片的变换涉及坐标处理,要自己算。
还有可以用Windows Presentation Foundation (WPF) 编程,里面的image控件好像也能旋转图片。
如果是VB6,恐怕只能用API函数了,百度一下“vb 旋转图片” “VB api函数”了解一下
图形平滑,边缘可不平滑
模块:
Option Explicit
Public Declare Function GdiplusStartup Lib "gdiplus" (token As Long, inputbuf As GdiplusStartupInput, Optional ByVal outputbuf As Long = 0) As GpStatus
Public Declare Function GdiplusShutdown Lib "gdiplus" (ByVal token As Long) As GpStatus
Public Declare Function GdipCreateFromHDC Lib "gdiplus" (ByVal hDC As Long, graphics As Long) As GpStatus
Public Declare Function GdipDeleteGraphics Lib "gdiplus" (ByVal graphics As Long) As GpStatus
Public Declare Function GdipSetSmoothingMode Lib "gdiplus" (ByVal graphics As Long, ByVal SmoothingMd As SmoothingMode) As GpStatus
Public Declare Function GdipSetInterpolationMode Lib "gdiplus" (ByVal graphics As Long, ByVal interpolation As InterpolationMode) As GpStatus
Public Declare Function GdipSetCompositingQuality Lib "gdiplus" (ByVal graphics As Long, ByVal CompositingQlty As CompositingQuality) As GpStatus
Public Declare Function GdipRotateWorldTransform Lib "gdiplus" (ByVal graphics As Long, ByVal angle As Single, ByVal order As MatrixOrder) As GpStatus
Public Declare Function GdipTranslateWorldTransform Lib "gdiplus" (ByVal graphics As Long, ByVal dx As Single, ByVal dy As Single, ByVal order As MatrixOrder) As GpStatus
Public Declare Function GdipLoadImageFromFile Lib "gdiplus" (ByVal filename As Long, Image As Long) As GpStatus
Public Declare Function GdipDisposeImage Lib "gdiplus" (ByVal Image As Long) As GpStatus
Public Declare Function GdipGetImageWidth Lib "gdiplus" (ByVal Image As Long, Width As Long) As GpStatus
Public Declare Function GdipGetImageHeight Lib "gdiplus" (ByVal Image As Long, Height As Long) As GpStatus
Public Declare Function GdipDrawImageRect Lib "gdiplus" (ByVal graphics As Long, ByVal Image As Long, ByVal x As Single, ByVal y As Single, ByVal Width As Single, ByVal Height As Single) As GpStatus
Public Enum MatrixOrder
MatrixOrderPrepend = 0
MatrixOrderAppend = 1
End Enum
Public Enum QualityMode
QualityModeInvalid = -1
QualityModeDefault = 0
QualityModeLow = 1
QualityModeHigh = 2
End Enum
Public Enum SmoothingMode
SmoothingModeInvalid = QualityModeInvalid
SmoothingModeDefault = QualityModeDefault
SmoothingModeHighSpeed = QualityModeLow
SmoothingModeHighQuality = QualityModeHigh
SmoothingModeNone
SmoothingModeAntiAlias
End Enum
Public Enum InterpolationMode
InterpolationModeInvalid = QualityModeInvalid
InterpolationModeDefault = QualityModeDefault
InterpolationModeLowQuality = QualityModeLow
InterpolationModeHighQuality = QualityModeHigh
InterpolationModeBilinear
InterpolationModeBicubic
InterpolationModeNearestNeighbor
InterpolationModeHighQualityBilinear
InterpolationModeHighQualityBicubic
End Enum
Public Enum CompositingQuality
CompositingQualityInvalid = QualityModeInvalid
CompositingQualityDefault = QualityModeDefault
CompositingQualityHighSpeed = QualityModeLow
CompositingQualityHighQuality = QualityModeHigh
CompositingQualityGammaCorrected
CompositingQualityAssumeLinear
End Enum
Public Type GdiplusStartupInput
GdiplusVersion As Long
DebugEventCallback As Long
SuppressBackgroundThread As Long
SuppressExternalCodecs As Long
End Type
Public Enum GpStatus
Ok = 0
GenericError = 1
InvalidParameter = 2
OutOfMemory = 3
ObjectBusy = 4
InsufficientBuffer = 5
NotImplemented = 6
Win32Error = 7
WrongState = 8
Aborted = 9
FileNotFound = 10
ValueOverflow = 11
AccessDenied = 12
UnknownImageFormat = 13
FontFamilyNotFound = 14
FontStyleNotFound = 15
NotTrueTypeFont = 16
UnsupportedGdiplusVersion = 17
GdiplusNotInitialized = 18
PropertyNotFound = 19
PropertyNotSupported = 20
End Enum
窗体(AutoRedraw=True)
Option Explicit
Dim token As Long
Dim graphics As Long
Dim img As Long, w As Long, h As Long
Private Sub InitGDIPlus()
Dim uInput As GdiplusStartupInput
uInput.GdiplusVersion = 1
If GdiplusStartup(token, uInput) Ok Then
MsgBox "GDI+ 初始化错误。程序即将关闭。", vbCritical, "InitError"
End
End If
End Sub
Private Sub TerminateGDIPlus()
GdipDisposeImage img
GdipDeleteGraphics graphics
GdiplusShutdown token
End Sub
Private Sub Form_Load()
InitGDIPlus
GdipCreateFromHDC Me.hDC, graphics
'GdipSetSmoothingMode graphics, SmoothingModeAntiAlias
'GdipSetInterpolationMode graphics, InterpolationModeHighQuality
'GdipSetCompositingQuality graphics, CompositingQualityHighQuality
GdipLoadImageFromFile StrPtr(App.Path "\1.png"), img
GdipGetImageWidth img, w
GdipGetImageHeight img, h
Rotate 15, graphics, 200, 200
End Sub
Sub Rotate(angle As Single, g As Long, x As Single, y As Integer)
GdipRotateWorldTransform g, angle, MatrixOrderAppend
GdipTranslateWorldTransform g, x, y, MatrixOrderAppend
GdipDrawImageRect g, img, -w, -h / 2, w, h
End Sub
Private Sub Form_Unload(Cancel As Integer)
TerminateGDIPlus
End Sub