Solved

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

Posted on 2014-02-04
4
869 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
Comment Utility
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

Expert Comment

by:Jacques Bourgeois (James Burger)
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

728 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now