' Rotate fr_pic by a multiple of 90 degrees
' and place the result in to_pic. Both PictureBoxes
' should have AutoRedraw = True.
Public Sub RotatePicture(fr_pic As PictureBox, to_pic As _
PictureBox, ByVal angle As Integer)
Dim fr_pixels() As RGBTriplet
Dim to_pixels() As RGBTriplet
Dim bits_per_pixel As Integer
Dim fr_wid As Long
Dim fr_hgt As Long
Dim to_wid As Long
Dim to_hgt As Long
Dim X As Integer
Dim Y As Integer
' Get the picture's image.
GetBitmapPixels fr_pic, fr_pixels, bits_per_pixel
' Get the picture's size.
fr_wid = UBound(fr_pixels, 1) + 1
fr_hgt = UBound(fr_pixels, 2) + 1
If angle = 0 Or angle = 180 Then
to_wid = fr_wid
to_hgt = fr_hgt
Else
to_wid = fr_hgt
to_hgt = fr_wid
End If
' Size the output picture to fit.
to_pic.Width = to_pic.Parent.ScaleX(to_wid, vbPixels, _
to_pic.Parent.ScaleMode) + _
to_pic.Width - to_pic.ScaleWidth
to_pic.Height = to_pic.Parent.ScaleY(to_hgt, vbPixels, _
to_pic.Parent.ScaleMode) + _
to_pic.Height - to_pic.ScaleHeight
' Copy the rotated pixels.
ReDim to_pixels(0 To to_wid - 1, 0 To to_hgt - 1)
Select Case angle
Case 0
For X = 0 To fr_wid - 1
For Y = 0 To fr_hgt - 1
to_pixels(X, Y) = fr_pixels(X, Y)
Next Y
Next X
Case 90
For X = 0 To fr_wid - 1
For Y = 0 To fr_hgt - 1
to_pixels(to_wid - Y - 1, X) = _
fr_pixels(X, Y)
Next Y
Next X
Case 180
For X = 0 To fr_wid - 1
For Y = 0 To fr_hgt - 1
to_pixels(to_wid - X - 1, to_hgt - Y - _
1) = fr_pixels(X, Y)
Next Y
Next X
Case 270
For X = 0 To fr_wid - 1
For Y = 0 To fr_hgt - 1
to_pixels(Y, to_hgt - X - 1) = _
fr_pixels(X, Y)
Next Y
Next X
Case Else
Stop
End Select
' Display the result.
SetBitmapPixels to_pic, bits_per_pixel, to_pixels
' Make the image permanent.
to_pic.Refresh
to_pic.Picture = to_pic.Image
End Sub
|