Solved

Rotate an image...

Posted on 1998-09-11
3
292 Views
Last Modified: 2010-04-30
How do I rotate an image, like a picture of a car seen from above?
0
Comment
Question by:fistfunk
3 Comments
 
LVL 12

Expert Comment

by:mark2150
ID: 1434440
Are you saying you want a picture of the car viewed from the top facing in different directions or are you asking how to convert an elevation (side view) into a plan (top view)?

First is easy, 2nd is hard.

M
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1434441
For 3-D effects, it's quite difficult if you have not any experince with graphics, and the 2-D effect might happened by loading bitmaps(images) from file and wether the state is from top, you load the top.bmp and show it in your image.....

Regards,
Viktor Ivanov
0
 
LVL 14

Accepted Solution

by:
waty earned 100 total points
ID: 1434442
Use the following :

Option Explicit

Public Const IMAGE_BITMAP = &O0         ' used with LoadImage to load a bitmap
Public Const LR_LOADFROMFILE = 16       ' used with LoadImage
Public Const LR_CREATEDIBSECTION = 8192 ' used with LoadImage
Public Const SRCCOPY = &HCC0020         ' (DWORD) dest = source
Public Const PI = 3.14159

Type BITMAP '14 bytes
   bmType         As Long
   bmWidth        As Long
   bmHeight       As Long
   bmWidthBytes   As Long
   bmPlanes       As Integer
   bmBitsPixel    As Integer
   bmBits         As Long
End Type

Declare Function LoadImage Lib "user32" Alias "LoadImageA" (ByVal hInst As Long, ByVal lpsz As String, ByVal un1 As Long, ByVal n1 As Long, ByVal n2 As Long, ByVal un2 As Long) As Long
Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long

Public Sub RotateBitmap(hBitmapDC As Long, lWidth As Long, lHeight As Long, lRadians As Long)

   Dim hNewBitmapDC As Long    ' DC of the new bitmap
   Dim hNewBitmap As Long      ' handle to the new bitmap
   Dim lSine As Long           ' sine used in rotation
   Dim lCosine As Long         ' cosine used in rotation
   Dim X1 As Long              ' used in calculating new
   ' bitmap dimensions
   Dim X2 As Long              ' used in calculating new
   ' bitmap dimensions
   Dim X3 As Long              ' used in calculating new
   ' bitmap dimensions
   Dim Y1 As Long              ' used in calculating new
   ' bitmap dimensions
   Dim Y2 As Long              ' used in calculating new
   ' bitmap dimensions
   Dim Y3 As Long              ' used in calculating new
   ' bitmap dimensions
   Dim lMinX As Long           ' used in calculating new
   ' bitmap dimensions
   Dim lMaxX As Long           ' used in calculating new
   ' bitmap dimensions
   Dim lMinY As Long           ' used in calculating new
   ' bitmap dimensions
   Dim lMaxY As Long           ' used in calculating new
   ' bitmap dimensions
   Dim lNewWidth As Long       ' width of new bitmap
   Dim lNewHeight As Long      ' height of new bitmap
   Dim I As Long               ' loop counter
   Dim J As Long               ' loop counter
   Dim lSourceX As Long        ' x pixel coord we are blitting
   ' from the source  image
   Dim lSourceY As Long        ' y pixel coord we are blitting
   ' from the source image

   ' create a compatible DC from the one just brought
   ' into this function
   hNewBitmapDC = CreateCompatibleDC(hBitmapDC)

   ' compute the sine/cosinse of the radians used to
   ' rotate this image
   lSine = Sin(lRadians)
   lCosine = Cos(lRadians)

   ' compute the size of the new bitmap being created
   X1 = -lHeight * lSine
   Y1 = lHeight * lCosine
   X2 = lWidth * lCosine - lHeight * lSine
   Y2 = lHeight * lCosine + lWidth * lSine
   X3 = lWidth * lCosine
   Y3 = lWidth * lSine

   ' figure out the max/min size of the new bitmap
   lMinX = Min(0, Min(X1, Min(X2, X3)))
   lMinY = Min(0, Min(Y1, Min(Y2, Y3)))
   lMaxX = Max(X1, Max(X2, X3))
   lMaxY = Max(Y1, Max(Y2, Y3))

   ' set the new bitmap width/height
   lNewWidth = lMaxX - lMinX
   lNewHeight = lMaxY - lMinY

   ' create a new bitmap based upon the new width/height of the
   ' rotated bitmap
   hNewBitmap = CreateCompatibleBitmap _
         (hBitmapDC, lNewWidth, lNwHeight)

   ' attach the new bitmap to the new device context created
   ' above before constructing the rotated bitmap
   Call SelectObject(hNewBitmapDC, hNewBitmap)

   ' loop through and translate each pixel to its new location.
   ' this is using a standard rotation algorythmn
   For I = 0 To lNewHeight
      For J = 0 To lNewWidth
         lSourceX = (J + lMinX) * lCosine + (I + lMinY) * lSine
         lSourceY = (I + lMinY) * lCosine - (J + lMinX) * lSine
         If (lSourceX >= 0) And (lSourceX <= lWidth) And _
               (lSourceY >= 0) And (lSourceY <= lHeight) Then
            Call BitBlt(hNewBitmapDC, J, I, 1, 1, hBitmapDC, _
                  lSourceX, lSourceY, SRCCOPY)
         End If
      Next J
   Next I

   ' reset the new bitmap width and height
   lWidth = lNewWidth
   lHeight = lNewHeight

   ' return the DC to the new bitmap
   hBitmapDC = hNewBitmapDC

   ' destroy the bitmap created
   Call DeleteObject(hNewBitmap)

End Sub

Private Function Min(X1 As Long, Y1 As Long) As Long
   If X1 >= Y1 Then
      Min = Y1
   Else
      Min = X1
   End If
End Function

Private Function Max(X1 As Long, Y1 As Long) As Long
   If X1 >= Y1 Then
      Max = X1
   Else
      Max = Y1
   End If
End Function

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now