Rotating an Image and saving

My Boss came up to me today and said:

"Hey, you know what would be cool?  If we could distribute a simple little program that would allow out digital frame users to preview the images on their memory cards (in a simulated frame) and rotate and images that were shot with the camera in the vertical position instead of horizontal postion.. get that done soon.. ok?"

I argued a little bit that there were "100 ways for the user to do this", but he insisted that our users would "love us" for a custom solution...

Well... the problem is I have very limited programming experience (just basic VB 6) and no idea how to implement this "idea",  but I know I can probably get an ActiveX or something to do this..  
(note: I can probably do it in VB.net as at first glance it looks similar to good ole VB)

Does anyone know :

1. Where I should start
2. Where I can find an example of this or something similar
3. If there is a easy solution to this problem.

My day went from bad to worse and I need a little sunshine!


EGormlyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JackOfPHCommented:
Here is a some example...

http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_21819688.html?sfQueryTermInfo=1+imag+rotat

Here is a simple sample to rotate a Picture in a picture box....

Draw two Picture Boxes on the Form and three Command Buttons.....
Then paste the following code..... (I have taken the code from Planet Source Code website)


 Const SRCCOPY = &HCC0020
 Const Pi = 3.14159265359

Private Declare Function SetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long


Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long


Private Declare Function StretchBlt Lib "gdi32" (ByVal hdc 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 nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long




Sub bmp_rotate(pic1 As PictureBox, pic2 As PictureBox, ByVal 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

   
    Dim c1x As Integer, c1y As Integer
    Dim c2x As Integer, c2y As Integer
    Dim a As Single
    Dim p1x As Integer, p1y As Integer
    Dim p2x As Integer, p2y As Integer
    Dim n As Integer, r As Integer
   
    c1x = pic1.ScaleWidth \ 2
    c1y = pic1.ScaleHeight \ 2
    c2x = pic2.ScaleWidth \ 2
    c2y = pic2.ScaleHeight \ 2
    If c2x < c2y Then n = c2y Else n = c2x
    n = n - 1
    pic1hDC& = pic1.hdc
    pic2hDC& = pic2.hdc


    For p2x = 0 To n


        For p2y = 0 To n
            If p2x = 0 Then a = Pi / 2 Else a = Atn(p2y / p2x)
            r = Sqr(1& * p2x * p2x + 1& * p2y * p2y)
            p1x = r * Cos(a + theta!)
            p1y = r * Sin(a + theta!)
            c0& = GetPixel(pic1hDC&, c1x + p1x, c1y + p1y)
            c1& = GetPixel(pic1hDC&, c1x - p1x, c1y - p1y)
            c2& = GetPixel(pic1hDC&, c1x + p1y, c1y - p1x)
            c3& = GetPixel(pic1hDC&, c1x - p1y, c1y + p1x)
            If c0& <> -1 Then xret& = SetPixel(pic2hDC&, c2x + p2x, c2y + p2y, c0&)
            If c1& <> -1 Then xret& = SetPixel(pic2hDC&, c2x - p2x, c2y - p2y, c1&)
            If c2& <> -1 Then xret& = SetPixel(pic2hDC&, c2x + p2y, c2y - p2x, c2&)
            If c3& <> -1 Then xret& = SetPixel(pic2hDC&, c2x - p2y, c2y + p2x, c3&)
        Next

       
        t% = DoEvents()
    Next

End Sub

Private Sub Command1_Click()

    'flip horizontal

    Picture2.Cls
    px% = Picture1.ScaleWidth
    py% = Picture1.ScaleHeight
    retval% = StretchBlt(Picture2.hdc, px%, 0, -px%, py%, Picture1.hdc, 0, 0, px%, py%, SRCCOPY)

End Sub

Private Sub Command2_Click()
'flip vertical
    Picture2.Cls
    px% = Picture1.ScaleWidth
    py% = Picture1.ScaleHeight
    retval% = StretchBlt(Picture2.hdc, 0, py%, px%, -py%, Picture1.hdc, 0, 0, px%, py%, SRCCOPY)

End Sub

Private Sub Command3_Click()
'rotate 45 degrees
    Picture2.Cls
    Call bmp_rotate(Picture1, Picture2, 3.14 / 4)

End Sub


Private Sub Form_Load()
Command1.Caption = "Flip Horizontal"
    Command2.Caption = "Flip Vertical"
    Command3.Caption = "Rotate 45 Degrees"
    Picture1.ScaleMode = 3
    Picture2.ScaleMode = 3

End Sub




You can change the value of third argument to change the rotation angle...
This is the simplest technique to rotate a picture inside a picturebox......
Tell me if you need some more complex technique.....









Here is another one



http://www.experts-exchange.com/Programming/Languages/Visual_Basic/Q_10080041.html?sfQueryTermInfo=1+imag+rotat

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
EGormlyAuthor Commented:
First of all.. WOW that is a lot more than I hoped for.

>>"Tell me if you need some more complex technique....."

No nothing more complex.. but I need to save the resulting image.
Is that easy? Should I accept this and then post a new question?
0
JackOfPHCommented:
If you want to save the picture from the picture box do this.

Picture1.Savepicture("c:\rotate.jpg")
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

JackOfPHCommented:
Any progress?
0
EGormlyAuthor Commented:
Works great for rotating images!
0
EGormlyAuthor Commented:
>>Picture1.Savepicture("c:\rotate.jpg")

I tried that first.
I would have thought that would work, but it doesn't.
I also tried sending the reults to a second picture box and then saving that and I get nothing but the color of the box.

I don't think the rotation is being applied to the actual image assigned to the picture box.
It's weird but I will figure it out.

I have posted this querstion (for saving) elewhere.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.