当前位置: 源码素材网 » VB教程 » 详情页

VB 旋转图片的简单操作代码

  •   时间:2020-12-22
  • 概述:图片旋转

本代码演示VB旋转一张图片,可了解下VB的一些图像控制方面的基础技巧,运行后的效果如下图所示:

VB 旋转图片的简单操作代码

下面的具体的代码实现:

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 srcopy = &HCC0020
Const PI = 3.1416
Private Sub Form_Load()
Picture2.Picture = Picture1.Picture
End Sub
Private Sub Slider1_Change()
    Picture2.Visible = True
    Picture1.Visible = False
    Picture2.Cls
    turn (Slider1.Value)
End Sub
Private Sub turn(ByVal angle As Single)
    Dim p(3) As POINTAPI
    Dim X As Integer, Y As Integer   '点在Picture1中的坐标
    Dim x1 As Integer, y1 As Integer '过渡坐标
    Dim X2 As Double, Y2 As Double   '过渡坐标
    Dim X3 As Double, Y3 As Double   '点经过旋转后在Picture2中的坐标
    Dim picH As Double, picW As Double
    Dim radian As Double             '旋转的弧度
    Dim M, H
    radian = angle * PI / 180    '获得旋转的弧度
    '    '原来右下角坐标
    x1 = Picture1.ScaleWidth - Picture1.ScaleWidth \ 2
    y1 = Picture1.ScaleHeight - Picture1.ScaleHeight \ 2
    '            '将坐标轴原点移动到Picture1的中心后原来坐标为(x,y)的点的新坐标为(X1,Y1)
    X2 = x1 * Cos(radian) + y1 * Sin(radian)   '获得在更改坐标原点后旋转后该点的X坐标
    Y2 = y1 * Cos(radian) - x1 * Sin(radian)   '获得在更改坐标原点后旋转后该点的Y坐标
    X3 = X2 + Picture1.ScaleWidth \ 2   '获得恢复坐标原点后旋转后的X坐标
    Y3 = Y2 + Picture1.ScaleHeight \ 2  '获得恢复坐标原点后旋转后的Y坐标
    If angle > 90 And angle < 180 Then H = -Y3
    If angle > 180 And angle <= 270 Then M = -X3
    If angle > 270 And angle <= 360 Then picH = Y3
    If angle < 90 Then picW = X3
    '原来左上角
    x1 = 0 - Picture1.ScaleWidth \ 2
    y1 = 0 - Picture1.ScaleHeight \ 2
    X2 = x1 * Cos(radian) + y1 * Sin(radian)   '获得在更改坐标原点后旋转后该点的X坐标
    Y2 = y1 * Cos(radian) - x1 * Sin(radian)   '获得在更改坐标原点后旋转后该点的Y坐标
    X3 = X2 + Picture1.ScaleWidth \ 2   '获得恢复坐标原点后旋转后的X坐标
    Y3 = Y2 + Picture1.ScaleHeight \ 2  '获得恢复坐标原点后旋转后的Y坐标
    If angle <= 90 Then M = -X3
    If angle > 270 And angle <= 360 Then H = -Y3
    If angle > 90 And angle < 180 Then picH = Y3
    If angle > 180 And angle < 270 Then picW = X3
    p(0).X = X3  '--原来的左上角
    p(0).Y = Y3
    '原来右上角
    x1 = Picture1.ScaleWidth \ 2
    y1 = 0 - Picture1.ScaleHeight \ 2
    '将坐标轴原点移动到Picture1的中心后原来坐标为(x,y)的点的新坐标为(X1,Y1)
    X2 = x1 * Cos(radian) + y1 * Sin(radian)   '获得在更改坐标原点后旋转后该点的X坐标
    Y2 = y1 * Cos(radian) - x1 * Sin(radian)   '获得在更改坐标原点后旋转后该点的Y坐标
    X3 = X2 + Picture1.ScaleWidth \ 2   '获得恢复坐标原点后旋转后的X坐标
    Y3 = Y2 + Picture1.ScaleHeight \ 2  '获得恢复坐标原点后旋转后的Y坐标
    If angle <= 90 Then H = -Y3
    If angle > 90 And angle < 180 Then M = -X3
    If angle > 180 And angle < 270 Then picH = Y3
    If angle > 270 And angle <= 360 Then picW = X3
    p(1).X = X3
    p(1).Y = Y3
    '原来左下角
    x1 = 0 - Picture1.ScaleWidth \ 2
    y1 = Picture1.ScaleHeight \ 2
    '将坐标轴原点移动到Picture1的中心后原来坐标为(x,y)的点的新坐标为(X1,Y1)
    X2 = x1 * Cos(radian) + y1 * Sin(radian)   '获得在更改坐标原点后旋转后该点的X坐标
    Y2 = y1 * Cos(radian) - x1 * Sin(radian)   '获得在更改坐标原点后旋转后该点的Y坐标
    X3 = X2 + Picture1.ScaleWidth \ 2   '获得恢复坐标原点后旋转后的X坐标
    Y3 = Y2 + Picture1.ScaleHeight \ 2
    If angle > 180 And angle < 270 Then H = -Y3
    If angle > 270 And angle <= 360 Then M = -X3
    If angle <= 90 Then picH = Y3
    If angle > 90 And angle < 180 Then picW = X3
    p(0).X = p(0).X + M '--原来的左上角
    p(0).Y = p(0).Y + H
    p(1).X = p(1).X + M
    p(1).Y = p(1).Y + H
    p(2).X = X3 + M '--原来的左下角
    p(2).Y = Y3 + H '
    Picture2.Height = picH + H
    Picture2.Width = picW + M
    Set Picture2.Picture = Nothing
    PlgBlt Picture2.hDC, p(0), Picture1.hDC, 0, 0, Picture1.ScaleWidth, Picture1.ScaleHeight, 0, 0, 0
    Picture2.Refresh
End Sub

    相关声明:

      若“VB 旋转图片的简单操作代码”有损您的权益,请告之我们删除内容。
      部分文章来源于网络,版权归原作者所有。