How to Rotate a Bitmap in VB for Windows

Last reviewed: June 21, 1995
Article ID: Q80406
The information in this article applies to:

- Microsoft Visual Basic Programming System for Windows,

  versions 1.0, 2.0, and 3.0

SUMMARY

This article contains a program example that uses Visual Basic for Windows statements and functions to rotate a bitmap.

MORE INFORMATION

Steps to Create Example Program

  1. Run Visual Basic for Windows, or from the File menu, choose New Project (ALT, F, N) if Visual Basic for Windows is already running. Form1 will be created by default.

  2. Place two picture boxes named Picture1 and Picture2 on Form1. Assign a bitmap to the Picture property of Picture1.

  3. Set the ScaleMode property of both picture boxes to 3 - Pixel.

  4. Set the AutoSize property of Picture1 to True (-1).

  5. Set the AutoRedraw property of Picture1 and Picture2 to True (-1).

  6. Place a command button named Command1 on Form1.

  7. Enter the following code in the Command1_Click event procedure:

    ' Example of how to call bmp_rotate. Sub Command1_Click ()

             Const Pi = 3.14159265359
    
             For angle = Pi / 6 To 2 * Pi Step Pi / 6
                picture2.Cls
                Call bmp_rotate(picture1, picture2, angle)
             Next
         End Sub
    
    

  8. Enter the following code in the general Declarations section:

    ' bmp_rotate(pic1, pic2, theta) ' Rotate the image in a picture box.

         '   pic1 is the picture box with the bitmap to rotate
         '   pic2 is the picture box to receive the rotated bitmap
         '   theta is the angle of rotation
         '
         Sub bmp_rotate (pic1 As Control, pic2 As Control, ByVal theta!)
           Const Pi = 3.14159265359
           Dim c1x As Integer  ' Center of pic1.
           Dim c1y As Integer  '   "
           Dim c2x As Integer  ' Center of pic2.
           Dim c2y As Integer  '   "
           Dim a As Single     ' Angle of c2 to p2.
           Dim r As Integer    ' Radius from c2 to p2.
           Dim p1x As Integer  ' Position on pic1.
           Dim p1y As Integer  '   "
           Dim p2x As Integer  ' Position on pic2.
           Dim p2y As Integer  '   "
           Dim n As Integer    ' Max width or height of pic2.
    
           ' Compute the centers.
           c1x = pic1.scalewidth / 2
           c1y = pic1.scaleheight / 2
           c2x = pic2.scalewidth / 2
           c2y = pic2.scaleheight / 2
    
           ' Compute the image size.
           n = pic2.scalewidth
           If n < pic2.scaleheight Then n = pic2.scaleheight
           n = n / 2 - 1
           ' For each pixel position on pic2.
           For p2x = 0 To n
              For p2y = 0 To n
                 ' Compute polar coordinate of p2.
                 If p2x = 0 Then
                   a = Pi / 2
                 Else
                   a = Atn(p2y / p2x)
                 End If
                 r = Sqr(1& * p2x * p2x + 1& * p2y * p2y)
    
                 ' Compute rotated position of p1.
                 p1x = r * Cos(a + theta)
                 p1y = r * Sin(a + theta)
    
                 ' Copy pixels, 4 quadrants at once.
                 c0& = pic1.Point(c1x + p1x, c1y + p1y)
                 c1& = pic1.Point(c1x - p1x, c1y - p1y)
                 c2& = pic1.Point(c1x + p1y, c1y - p1x)
                 c3& = pic1.Point(c1x - p1y, c1y + p1x)
                 If c0& <> -1 Then pic2.PSet (c2x + p2x, c2y + p2y),c0&
                 If c1& <> -1 Then pic2.PSet (c2x - p2x, c2y - p2y),c1&
                 If c2& <> -1 Then pic2.PSet (c2x + p2y, c2y - p2x),c2&
                 If c3& <> -1 Then pic2.PSet (c2x - p2y, c2y + p2x),c3&
              Next
              ' Allow pending Windows messages to be processed.
              t% = DoEvents()
           Next
         End Sub
    
    

  9. Assign a bitmap image to the Picture1 Picture property.

  10. To start the program, press F5, then click the Command1 button. The program rotates the image of Picture1 by 30 degrees and places the rotated image in Picture2. It continues to draw the image rotated at successive multiples of 30 degrees until it has rotated the picture by 360 degrees.

To save the new bitmap created in Picture2, you can use the following statement:

   SavePicture Picture2.Image, "filename.bmp"


Additional reference words: 1.00
KBCategory: kbprg kbcode
KBSubcategory: PrgCtrlsStd


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.