画像を回転させる(T)          <TOP>


画像を指定の角度に回転させます。

GetPixel 指定された座標のピクセルのRGB値を取得

SetPixel 指定した座標に点を配置する

GetDC デバイスコンテキストを取得

ReleaseDC デバイスコンテキストの解放

 

回転角度を指定して画像を回転させてみます。左から3〜5番目のピクチャボックスには、それぞれ90°、180°、270°回転させています。

あまり大きな数値を入れるとピクチャボックスからはみ出ます・・

 
'================================================================
'= 画像を回転させる
'=    (PicRotate.bas)
'================================================================
#include "Windows.bi"

' 指定された座標のピクセルのRGB値を取得
Declare Function Api_GetPixel& Lib "gdi32" Alias "GetPixel" (ByVal hDC&, ByVal X&, ByVal Y&)

' 指定した座標に点を配置する
Declare Function Api_SetPixel& Lib "gdi32" Alias "SetPixel" (ByVal hDC&, ByVal X&, ByVal Y&, ByVal crColor&)

' 指定されたウィンドウのクライアント領域または画面全体を表すディスプレイデバイスコンテキストのハンドルを取得
Declare Function Api_GetDC& Lib "user32" Alias "GetDC" (ByVal hWnd&)

' デバイスコンテキストを解放
Declare Function Api_ReleaseDC& Lib "user32" Alias "ReleaseDC" (ByVal hWnd&, ByVal hDC&)

Var Shared Picture(4) As Object
Var Shared Text1 As Object
Var Shared Edit1 As Object
Var Shared Button1 As Object
Var Shared Bitmap As Object

For i = 0 To 4
    Picture(i).Attach GetDlgItem("Picture" & Trim$(Str$(i + 1)))
Next
Text1.Attach GetDlgItem("Text1") : Text1.SetFontSize 14
Edit1.Attach GetDlgItem("Edit1") : Edit1.SetFontSize 14
Button1.Attach GetDlgItem("Button1") : Button1.SetFontSize 14
BitmapObject Bitmap

Var Shared hDC(4) As Long

'================================================================
'=
'================================================================
Declare Sub MainForm_Start edecl ()
Sub MainForm_Start()
    Var i As Integer

    Bitmap.LoadFile "bike1.bmp"
    Picture(0).DrawBitmap Bitmap, 12, 14
    Bitmap.DeleteObject

    For i = 0 To 4
        hDC(i) = Api_GetDC(Picture(i).GethWnd)
    Next i
End Sub

'================================================================
'=
'================================================================
Declare Sub Button1_on edecl ()
Sub Button1_on()
    Var pai As single
  Var col As Long
  Var x As Long
    Var y As Long
  Var x1 As Long
    Var x2 As Long
    Var x3 As Long
    Var x4 As Long
  Var y1 As Long
    Var y2 As Long
    Var y3 As Long
    Var y4 As Long
  Var sin1 As double
    Var sin2 As double
    Var sin3 As double
    Var sin4 As double
  Var cos1 As double
    Var cos2 As double
    Var cos3 As double
    Var cos4 As double
    Var angle As Integer
    Var pWidth As Long
    Var pHeight As Long
    Var Ret As Long

    pai = 3.14159265
    angle = Val(Edit1.GetWindowtext)

    pWidth = Picture(0).GetWidth
    pHeight = Picture(0).GetHeight

  cos1 = Cos(angle * pai / 180)
  sin1 = Sin(angle * pai / 180)

  cos2 = Cos( 90 * pai / 180)
  sin2 = Sin( 90 * pai / 180)

  cos3 = Cos(180 * pai / 180)
  sin3 = Sin(180 * pai / 180)

  cos4 = Cos(270 * pai / 180)
  sin4 = Sin(270 * pai / 180)

    For i = 1 To 4
        Picture(i).Cls
    Next i

  For x = -pWidth To pWidth
    For y = -pHeight To pHeight
      x1 = x * cos1 - y * sin1 + angle - 4
      y1 = x * sin1 + y * cos1 - 20

      x2 = x * cos2 - y * sin2 + pWidth
      y2 = x * sin2 + y * cos2

      x3 = x * cos3 - y * sin3 + pWidth
      y3 = x * sin3 + y * cos3 + pHeight

      x4 = x * cos4 - y * sin4
      y4 = x * sin4 + y * cos4 + pHeight

      '指定された座標のピクセルのRGB値を取得
            col = Api_GetPixel(hDC(0), x, y)

      '角度を変更して描画
            Ret = Api_SetPixel(hDC(1), x1, y1, col)
            Ret = Api_SetPixel(hDC(2), x2, y2, col)
            Ret = Api_SetPixel(hDC(3), x3, y3, col)
            Ret = Api_SetPixel(hDC(4), x4, y4, col)
    Next y
  Next x
End Sub

'================================================================
'=
'================================================================
Declare Sub Mainform_QueryClose edecl ()
Sub Mainform_QueryClose()
    Var i As Integer
    Var Ret As Long

    For i = 0 To 4
        Ret = Api_ReleaseDC(Picture(i).GethWnd, hDC(i))
    Next i
End Sub

'================================================================
'=
'================================================================
While 1
    WaitEvent
Wend
Stop
End