ヘッダー

VB.NET2002対応 VB.NET2003対応 VB2005対応

 

画像を回転する

1.一般的な例

以下の関数Rotateを使用すると、画像を左上を中心に任意の角度で回転させることができる。使用例はすぐ後にある。

なお、回転後の画像は回転前の画像とサイズが異なることに注意。この関数では回転後の画像のサイズを自動的に算出する。

VB.NET2002対応 VB.NET2003対応 VB2005対応

'■Rotate
''' <summary>画像を回転する。</summary>
''' <param name="Source">対象の画像</param>
''' <param name="Degree">回転角度。度単位で指定する。</param>
''' <param name="BackColor">回転後の背景色</param>
''' <returns>回転後の画像</returns>
''' <remarks>回転後の画像がすべて表示されるように画像のサイズを自動的に変更する。</remarks>
Private Function Rotate(ByVal Source As Image, ByVal Degree As Integer, ByVal BackColor As Color) As Image

    '▼引数のチェック

    If IsNothing(Source) Then
       
Throw New NullReferenceException("Sourceに値が設定されていません。")
    End
If

    '▼角度がマイナスの場合、プラスに変換。例:-10度= 350度、-400度= 320度

    If Degree < 0 Then
       
Degree = 360 - (-Degree Mod 360)
    End
If

    '▼回転角度を0度~90度に補正する

    Dim SourceClone As Image = CType(Source.Clone, Image)

    Select Case (Degree \ 90) Mod 4
        Case 1
'第二象限
           
SourceClone.RotateFlip(RotateFlipType.Rotate90FlipNone)
            Degree = Degree
Mod 90
        Case 2
'第三象限
           
SourceClone.RotateFlip(RotateFlipType.Rotate180FlipNone)
            Degree = Degree
Mod 90
        Case 3
'第四象限
           
SourceClone.RotateFlip(RotateFlipType.Rotate270FlipNone)
            Degree = Degree
Mod 90
    End
Select

    '▼回転後の画像がぴったり入る長方形の大きさを算出
    '※OffsetXは回転後の画像が第1象限に入るために必要なX軸方向の画像の平行移動量。

    Dim OffsetX As Single = CSng((SourceClone.Height * (Math.Sin(ToRadian(Degree)))))
    Dim NewWidth As Integer = CInt(SourceClone.Width * Math.Cos(ToRadian(Degree)) + OffsetX)
    Dim
NewHeight As Integer = CInt(SourceClone.Height * Math.Cos(ToRadian(Degree)) + SourceClone.Width * Math.Sin(ToRadian(Degree)))

    '▼描画実行

    Dim DestImage As New Bitmap(NewWidth, NewHeight)
    Dim g As Graphics = Graphics.FromImage(DestImage)
    Dim M As New Drawing2D.Matrix

    '背景色の描画
   
g.FillRectangle(New SolidBrush(BackColor), DestImage.GetBounds(GraphicsUnit.Pixel))

    '回転と平行移動
   
M.Translate(OffsetX, 0)
    M.Rotate(Degree)
    g.Transform = M

    g.DrawImage(SourceClone, New Point(0, 0))

    Return DestImage

End Function

'■ToRadian
''' <summary>度からラジアンを求めます。</summary>
''' <param name="Degrees">度。度は1周を度と定義する単位です。</param>
''' <returns>ラジアン</returns>
Private Shared Function ToRadian(ByVal Degrees As Double) As Double

    Return (Math.PI / 180) * Degrees

End Function

 

使用例1:C:\Sample1.bmpを30度回転して表示する

VB.NET2002対応 VB.NET2003対応 VB2005対応

PictureBox1.Image = Rotate(Image.FromFile("C:\Sample1.bmp"), 30, Color.White)
PictureBox1.Refresh()

 

使用例2:PictureBox1の画像を30度回転して表示する

VB.NET2002対応 VB.NET2003対応 VB2005対応

PictureBox1.Image = Rotate(PictureBox1.Image, 30, Color.White)
PictureBox1.Refresh()

メモ:PictureBox1にはあらかじめ画像を読み込んでおくなどしてImageプロパティに値をセットしておく必要があります。

 

2.単純に回転する例

この例では回転後の画像のサイズを算出しないのではみ出た部分は切り捨てられる。また、90度以上の角度で回転した場合は実際上画面に表示されない。

VB.NET2002対応 VB.NET2003対応 VB2005対応

Dim SourceImage As Image = PictureBox1.Image
Dim DestImage As New Bitmap(SourceImage.Size.Width, SourceImage.Size.Height)
Dim g As Graphics = Graphics.FromImage(DestImage)
Dim M As New Drawing2D.Matrix

M.Rotate(30)
g.Transform = M
g.DrawImage(SourceImage,
New Point(0, 0))

PictureBox1.Image = DestImage

メモ:PictureBox1にはあらかじめ画像を読み込んでおくなどしてImageプロパティに値をセットしておく必要があります。

 


VB6対応  VB6ではAPIやDirectX等を使用しますがかなり困難です。