Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

i am new and beginner in programming
right now, i am trying to create a simple simulation about ship navigation.
i have a problem on creating a compass.

i found this code (i am not the one that create this code)

VB
Imports System.Math
Public Class Form1

    Dim A, B As String
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub btnRotate_Click(sender As Object, e As EventArgs) Handles btnRotate.Click
        Dim bm_in As New Bitmap(picSource.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

        Dim i As Long

        For i = 0 To 3

            corners(i).X -= cx

            corners(i).Y -= cy

        Next i

        Dim theta As Single = Single.Parse(txtAngle.Text) * 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)

        picDest.Image = bm_out
        'picSource.Image = bm_out
        'A = TB1.Value
        'txtAngle.Text = TB1.Value
        B = txtAngle.Text
        TB1.Value = txtAngle.Text
    End Sub

    Private Sub TB1_Scroll(sender As Object, e As EventArgs) Handles TB1.Scroll

        Dim bm_in As New Bitmap(picSource.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

        Dim i As Long

        For i = 0 To 3

            corners(i).X -= cx

            corners(i).Y -= cy

        Next i

        Dim theta As Single = Single.Parse(TB1.Value) * 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)

        picDest.Image = bm_out
        'picSource.Image = bm_out
        A = TB1.Value
        txtAngle.Text = TB1.Value
    End Sub

    Private Sub txtAngle_TextChanged(sender As Object, e As EventArgs) Handles txtAngle.TextChanged

    End Sub
End Class


i make it so it can be rotated with trackbar and button click

The problem is the picture in the picture box does not rotate at the center.
Can anyone help me? (sorry for bad grammar)
Posted

1 solution

First of all, it's a really bad idea to rotate anything in PictureBox. This control is not designed for anything even minimally complicated. You can either do graphical conversion on a System.Drawing.Bitmap, or, if your real goal is showing something on screen, render it immediately by overriding the virtual method System.Windows.Control.OnPaint or handling the event System.Windows.Control.Paint and using the instance of System.Drawing.Graphics passed to you in even arguments. If you use PictureBox, it does not help you even a bit, but only creates extra hassles and consumes extra resources and CPU time. Don't do it.

For further detail on that, please see my past answers:
Append a picture within picturebox[^],
draw a rectangle in C#[^],
How do I clear a panel from old drawing[^].

See also:
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
How to speed up my vb.net application?[^],
capture the drawing on a panel[^],
Drawing Lines between mdi child forms[^].

You can even do both bitmap and on-screen rendering in one method, which is the best approach if you need to do both independently. You only need to write a separate method with the parameter of the type System.Drawing.Graphics. In one call, the instance of the Graphics will be from pain event arguments, in another call, from an instance of Bitmap.

Now, what to do with rotation, as well as other transformations? What you do can work, but you are doing it in a hard way. Many basic problems like that are already solved for you in the System.Drawing.Graphics. This is reliably done via the Transform which is of the type System.Drawing.Drawing2D.Matrix. You can start from the unit matrix (doing identical transform) and modify it according to a set of transforms you need. This type provides you with predefined operations for rotations, shearing, translation, scaling, reflections, as well as the custom matrix transforms.

Please see:
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.aspx[^],
http://msdn.microsoft.com/en-us/library/system.drawing.graphics.transform.aspx[^],
http://msdn.microsoft.com/en-us/library/system.drawing.drawing2d.matrix.aspx[^].

This is easy to do and fun to try, not as boring and error-prone as your trivial ad-hoc linear algebra operations in linear metrics space. Someone else already done most of the job. :-)

—SA
 
Share this answer
 
v2
Comments
imunksky 12-Jul-13 1:33am    
Thx! Gonna try it now :D
Sergey Alexandrovich Kryukov 12-Jul-13 1:34am    
Great. Will you accept the answer formally (green button)?
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900