Results 1 to 10 of 10

Thread: How to center-rotate an image in VB.NET?

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    384

    How to center-rotate an image in VB.NET?

    Hi, I saw on one of jmcilhinney's replies a quite reliable code to easily do that someday. (Link will be added as soon as I find it again.)

    The problem are 2 things:
    - In case of removing picturebox, I'm unable to find center of image itself inside a form or a panel. It is literally rotating from center of screen but from top-left corner.
    - In case of resizing, image will not follow and exceeds in x,y and It goes out of the box.
    I'm trying to upload a gif/video. to describe more detailed.

    Consider you want to represent a sort of compass control. Radial rotation center anchored resizable/scalable north-peak-symbol-kind-of-thing.

    Here's the entire code I used:
    Code:
    Imports System.Drawing.Drawing2D
    Public Class Form3
        Public HeelValue As Integer = 0
        Private picture As Image = My.Resources.Untitled.png
        Private heelangle As Integer = 0
    
        Private Sub Form3_Resize(sender As Object, e As EventArgs) Handles Me.Resize
            Me.Refresh()
            Panel1.CreateGraphics.DrawLine(Pens.Red, Panel1.Width \ 2, 0, Panel1.Width \ 2, Panel1.Height)
            Panel1.CreateGraphics.DrawLine(Pens.Red, 0, Panel1.Height \ 2, Panel1.Width, Panel1.Height \ 2)
        End Sub
    
    
        Private Sub Panel1_Paint(sender As Object, e As PaintEventArgs) Handles Panel1.Paint
            With e.Graphics
                'Move the origin to the centre of the PictureBox.
                .TranslateTransform(Me.Panel1.Width / 2, Me.Panel1.Height / 2)
    
                'Rotate the world.
                .RotateTransform(Me.heelangle)
    
                'Size/Scale
                '.ScaleTransform((Panel1.Height / picture.Height) * 2, (Panel1.Height / picture.Height) * 2)
    
                'Draw the image so its centre coincides with the origin.
                '.DrawImage(Me.picture, (Me.Panel1.Width \ 2) - (Me.picture.Width \ 2), (Me.Panel1.Height \ 2) - (Me.picture.Height \ 2))
                .DrawImage(picture, (Me.Panel1.Width \ 2) - (picture.Width \ 2), (Panel1.Height \ 2) - (picture.Height \ 2))
            End With
            Panel1.CreateGraphics.DrawLine(Pens.Red, Panel1.Width \ 2, 0, Panel1.Width \ 2, Panel1.Height)
            Panel1.CreateGraphics.DrawLine(Pens.Red, 0, Panel1.Height \ 2, Panel1.Width, Panel1.Height \ 2)
        End Sub
    
        Private Sub TrackBar2_Scroll(sender As Object, e As EventArgs) Handles TrackBar2.Scroll
            'NOT THIS EVENT
        End Sub
    
        Private Sub TrackBar2_ValueChanged(sender As Object, e As EventArgs) Handles TrackBar2.ValueChanged
            Me.heelangle = TrackBar2.Value / 10 Mod 360
            Panel1.CreateGraphics.DrawImage(picture, 0, 0)
            Me.Refresh()
        End Sub
    End Class
    *Update: What I tried:
    - Subtract DrawImage argument from a proportion of image size (and location) to achieve required results but it acts even more weird.
    - This is intended to being introduced to more possible properties of e.graphics which do things more easier... I'm not familiar with
    Last edited by pourkascheff; Oct 28th, 2022 at 08:00 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width