Solved

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

Posted on 2014-02-04
4
1,055 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

624 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