Solved

Rotate picture (png) about its centre in vb.net

Posted on 2014-02-04
4
968 Views
Last Modified: 2014-02-06
I would like to rotate a picture about its centre in vb express.  I have tried lots of different codes but they all rotate about a fixed point (i think the forms origin) and no matter what i try i can not change it - i simply want an object to spin.

Is this possible - must be?
0
Comment
Question by:SweetingA
  • 2
4 Comments
 

Author Comment

by:SweetingA
ID: 39834281
Imports System.Drawing.Drawing2D

This is what i have tried and i don't get any errors but i don't see anything rotate either.

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Label1.Text = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff")
        Timer1.Enabled = True
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Label1.Text = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff")
        PictureBox1.Invalidate()
    End Sub

    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        Static ang As Integer = 0
        Dim W As Integer = My.Resources.ALANS_DIAL.Width
        Dim H As Integer = My.Resources.ALANS_DIAL.Height
        Dim gr As Graphics = e.Graphics
        Dim m As Matrix = New Matrix
        m.RotateAt(ang, New Point(W \ 2, H \ 2))
        gr.Transform = m
        gr.DrawImage(My.Resources.ALANS_DIAL, New Point(W \ 2, H \ 2))
        ang = (ang + 25) Mod 360
    End Sub

End Class
0
 
LVL 40
ID: 39834504
Very simple.

Put a square PictureBox on the screen

Set its SizeMode to Zomm

Load your image in it's Image property

Do the following, playing to the different possible parameters of the Rotate method to get the level of rotation that you want:

            PictureBox1.Image.RotateFlip(RotateFlipType.Rotate90FlipNone)
            PictureBox1.Refresh()
0
 

Author Comment

by:SweetingA
ID: 39834945
Yes, i understand that but i need to rotate around any angle not fixed steps such as 90/180/270 etc

Regards
0
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 39835028
Your code does work for me, just rotating around a corner point which is not what you want.

If you really see nothing it might be that it's out of view or something else is going on.

Anyway, this slightly modified code rotates the image around its centre point, at the centre of the picture box.

If you still don't see it, check the picturebox properties and also make sure the code is being called by putting in a breakpoint for example.
    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        Static ang As Integer = 0
        Dim W As Integer = My.Resources.ALANS_DIAL.Width
        Dim H As Integer = My.Resources.ALANS_DIAL.Height
        Dim gr As Graphics = e.Graphics
        Dim m As Matrix = New Matrix
        m.Translate(PictureBox1.Width \ 2, PictureBox1.Height \ 2)
        m.RotateAt(ang, New Point(0, 0))
        gr.Transform = m
        gr.DrawImage(My.Resources.ALANS_DIAL, New Point(-W \ 2, -H \ 2))
        ang = (ang + 25) Mod 360
    End Sub

Open in new window

capture
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

679 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