| Home Search What's New Index Books Links Q & A Newsletter Banners Feedback Tip Jar |
| Old Pages Old Index Site Map What's New Books How To Tips & Tricks Tutorials Stories Performance Essays Links Q & A New in VB6 Free Stuff Pictures |

Title | Rotate a picture 90 degrees
Keywords | rotate, image, picture, rotation, GetBitmapBits, SetBitmapBits
Categories | 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.
    Public Sub RotatePicture(fr_pic As PictureBox, to_pic As _
        PictureBox)
    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
        to_wid = fr_hgt
        to_hgt = fr_wid

        ' Size the output picture to fit.
        to_pic.Width = to_pic.Parent.ScaleX(fr_hgt, vbPixels, _
            to_pic.Parent.ScaleMode) + _
            to_pic.Width - to_pic.ScaleWidth
        to_pic.Height = to_pic.Parent.ScaleY(fr_wid, vbPixels, _
            to_pic.Parent.ScaleMode) + _
            to_pic.Height - to_pic.ScaleHeight

        ' Copy the pixels rotated 90 degrees.
        ReDim to_pixels(0 To to_wid - 1, 0 To to_hgt - 1)
        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

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