Rotating an image in vb.net

Diverimento 20 Reputation points
2023-11-17T02:31:21.94+00:00

Hello...

I've written a small program to learn the means of rotating images. I have four buttons, each with it's own image. The images on the first three buttons work fine, but the fourth button rotates by 180 degrees.

I'm programming to keep my brain ticking over and using vb 2008

I'd appreciate if someone could provide a solution :o)

Here is the code

Imports System.Drawing

Public Class Form1
    Dim WithEvents oneButton As Button = Nothing
    Dim btnList = New List(Of Button)


'Declare button variables
Dim btnSize As Integer = 80
Dim btnX As Integer = 90
Dim btnY As Integer = 30
Dim xOffset As Integer = 60

Dim btnCount As Integer = 4

Dim bitmap As Bitmap = Nothing

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Me.Size = New Size(480, 180)

    MakeButtons()

   
End Sub

Private Sub MakeButtons()

    For i As Integer = 0 To btnCount - 1
        oneButton = New Button() 'Initialise button
        oneButton.Size = New Size(btnSize, btnSize)

        Select Case i
            Case 0
                oneButton.Image = My.Resources.btn1
            Case 1
                oneButton.Image = My.Resources.btn2
            Case 2
                oneButton.Image = My.Resources.btn3
            Case 3
                oneButton.Image = My.Resources.btn4
        End Select

        oneButton.Location = New Point((btnX * i) + xOffset, btnY)
        oneButton.FlatStyle = FlatStyle.Standard

        Me.Controls.Add(oneButton) 'Add oneButton

        AddHandler oneButton.Click, AddressOf RotateButton_Click 'Add handler to rotate image

        btnList.Add(oneButton) 'Add button to list
    Next
End Sub

Private Sub RotateButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles oneButton.Click
    bitmap = sender.Image
    bitmap.RotateFlip(RotateFlipType.Rotate90FlipNone)
End Sub

End Class



Developer technologies .NET Other
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 34,221 Reputation points Microsoft External Staff
    2023-11-17T02:49:10.6866667+00:00

    Hi @Diverimento .

    Remove Handles oneButton.Click.

    Private Sub RotateButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles oneButton.Click
    

    It causes the oneButton in your last loop to have two event handlers, resulting in two spins being triggered.

    Best Regards.

    Jiachen Li


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.