Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-02-04
4
Medium Priority
?
1,101 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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

715 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