Solved

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

Posted on 2014-02-04
4
994 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

742 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