Picturebox rotating

Mister Zippo

Active member
Joined
Dec 27, 2009
Messages
40
Programming Experience
1-3
Hello World,

I introduce myself:
Mister Zippo, but you can just call me zippo.I am new of that forum, but with already little experience.

Please, Can you help me with this;)?!?!?

I would to rotate starting with central position una picturebox with inside the picture of one ship.
My program need to reproduce the ship rolling in the seas.

How I can do it in the right way?!?!?!

Waiting for your kind reply.

My best.
Zippo
 
Anthony, good morning and happy new year.

Thanks for reply!!!!

Yes, it is only an image (one ship) and I would to rotate it depending on inclinometer serial input.

Is that possible?!?
and why?!?!'

please. HELP.

my best.
zippo
 
yeah, all is possible ... hopefully you can get the degrees from the serial port ... well here is a class I made up really quick (there isnt any error handling and it uses the same image so after a while it gets burry ... you can fix that by using the starting image everytime instead of rotating the same image over and over ... but it gets the point across ) ... I will also inculde the sample project I just made

VB.NET:
Imports System.Math


Public Class Rotate

    Private intPictureBox As PictureBox



    Public Sub New(ByVal PictureBox As PictureBox)
        intPictureBox = PictureBox
    End Sub

    Public Sub Rotate(ByVal Degrees As Integer)
        Dim bm_in As New Bitmap(intPictureBox.Image)

        Dim wid As Single = bm_in.Width
        Dim hgt As Single = bm_in.Height
        Dim corners As Point() = {New Point(0, 0), New Point(wid, 0), New Point(0, hgt), New Point(wid, hgt)}
        Dim cx As Single = wid / 2
        Dim cy As Single = hgt / 2


        For i As Int16 = 0 To 3

            corners(i).X -= cx

            corners(i).Y -= cy

        Next i

        Dim theta As Single = Degrees * PI / 180.0

        Dim sin_theta As Single = Sin(theta)

        Dim cos_theta As Single = Cos(theta)

        Dim X As Single

        Dim Y As Single

        For i = 0 To 3

            X = corners(i).X

            Y = corners(i).Y

            corners(i).X = X * cos_theta + Y * sin_theta

            corners(i).Y = -X * sin_theta + Y * cos_theta

        Next i

        Dim xmin As Single = corners(0).X

        Dim ymin As Single = corners(0).Y

        For i = 1 To 3

            If xmin > corners(i).X Then xmin = corners(i).X

            If ymin > corners(i).Y Then ymin = corners(i).Y

        Next i

        For i = 0 To 3

            corners(i).X -= xmin

            corners(i).Y -= ymin

        Next i

        Dim bm_out As New Bitmap(CInt(-2 * xmin), CInt(-2 * ymin))

        Dim gr_out As Graphics = Graphics.FromImage(bm_out)

        ReDim Preserve corners(2)

        gr_out.DrawImage(bm_in, corners)

        intPictureBox.Image = bm_out

    End Sub


End Class

It that helps dont forget to give me a good rating :)
 

Attachments

  • WindowsApplication6.zip
    17 KB · Views: 35
Dear friend Anthony,

Sure for you a good rating will be ready!!:D
And I thank you very very much for this very very good and professsional help!

But, there is still a problem...
What I need is one image (front image of the ship), rolling in right direction and left direction, remaining fixed to one exact point (approximaively the middle center of the image (the ship).
In that case you have kindly explain me, the image don't remain rolling in one point, but is moving everywhere..

There is solution for my problem?!?!:confused:

Can you switch on my light?!?!?!:D

Thank you very much!!!!

your,
zippo
 
I think your this is what your talking about ... replace the rotate class with this

VB.NET:
Imports System.Math


Public Class Rotate

    Private intPictureBox As PictureBox

    Private StartImage As Image


    Public Sub New(ByVal PictureBox As PictureBox)
        intPictureBox = PictureBox
        If PictureBox.Image IsNot Nothing Then
            StartImage = PictureBox.Image
        End If
    End Sub

    Public Sub SetImage(ByVal Image As Image)
        StartImage = Image
    End Sub

    Public Sub Rotate(ByVal Degrees As Integer)
        Dim bm_in As New Bitmap(StartImage)

        Dim wid As Single = bm_in.Width
        Dim hgt As Single = bm_in.Height
        Dim corners As Point() = {New Point(0, 0), New Point(wid, 0), New Point(0, hgt), New Point(wid, hgt)}
        Dim cx As Single = wid / 2
        Dim cy As Single = hgt / 2


        For i As Int16 = 0 To 3

            corners(i).X -= cx

            corners(i).Y -= cy

        Next i

        Dim theta As Single = Degrees * PI / 180.0

        Dim sin_theta As Single = Sin(theta)

        Dim cos_theta As Single = Cos(theta)

        Dim X As Single

        Dim Y As Single

        For i = 0 To 3

            X = corners(i).X

            Y = corners(i).Y

            corners(i).X = X * cos_theta + Y * sin_theta

            corners(i).Y = -X * sin_theta + Y * cos_theta

        Next i

        Dim xmin As Single = corners(0).X

        Dim ymin As Single = corners(0).Y

        For i = 1 To 3

            If xmin > corners(i).X Then xmin = corners(i).X

            If ymin > corners(i).Y Then ymin = corners(i).Y

        Next i

        For i = 0 To 3

            corners(i).X -= xmin

            corners(i).Y -= ymin

        Next i

        Dim bm_out As New Bitmap(CInt(-2 * xmin), CInt(-2 * ymin))

        Dim gr_out As Graphics = Graphics.FromImage(bm_out)

        ReDim Preserve corners(2)

        gr_out.DrawImage(bm_in, corners)

        intPictureBox.Image = bm_out

    End Sub


End Class
 
Anthony,
YOU ARE FANTASTIC!!!!

I don't know how to thank you...!
Really thank you for the help you have give me..it's perfect, you are genially.:D

Thank you my friend.

I have tried it done as you've tell me and it's working fine. thanks.

Now I am thinking to do a sinusoid graphic that reproducing the rolling motion of the picturebox(the ship) , that will be great.
I don't know at npw I will do, I will study it.
Do you have some idea to launce me??

Anyway, thank alot for consistent help.

All my best and happy new year!!!

your,
Zippo
 
Back
Top