Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleRotate a picture by a multiple of 90 degrees in Visual Basic 6
DescriptionThis example shows how to rotate a picture by a multiple of 90 degrees in Visual Basic 6.
Keywordsrotate, picture, image, image processing, Visual Basic 6
CategoriesAlgorithms, Graphics
 
Use the GetBitmapPixels subroutine to load the pixel values. GetBitmapPixels is a subroutine from my book Visual Basic Graphics Programming that uses the GetBitmapBits API function to quickly load a picture's pixel values into an array. See the code or the book for a description of this routine.

Next create an array to hold the rotated pixels and copy the pixels into it. Then use SetBitmapPixels to copy the pixel values into the output PictureBox.

 
' 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
 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated