简体   繁体   中英

How to rotate a PictureBox on a windows form using vb.net

I need to rotate a picture box 180 degrees when a condition in my if statement is met. Is this possible?

I'll assume that you want to rotate the image inside, because rotating the box itself doesn't make much sense (and is impossible anyway).

Try this:

myPictureBox.Image.RotateFlip(RotateFlipType.Rotate180FlipNone);
PictureBox1.Image.RotateFlip(RotateFlipType.Rotate180FlipNone)
PictureBox1.Refresh()

When you try to rotate your image with:

PictureBox1.Image.RotateFlip(RotateFlipType.Rotate180FlipNone)

nothing will happen until you close the form and open it again (not the project, just the form). If you want to rotate at once then you should use PictureBox1.Refresh() .

The System.Drawing.Image.RotateFlip() method allows you to rotate the actual image displayed in the picturebox. See this page

Dim bitmap1 As Bitmap

Private Sub InitializeBitmap()
    Try
        bitmap1 = CType(Bitmap.FromFile("C:\Documents and Settings\All Users\" _
            & "Documents\My Music\music.bmp"), Bitmap)
        PictureBox1.SizeMode = PictureBoxSizeMode.AutoSize
        PictureBox1.Image = bitmap1
    Catch ex As System.IO.FileNotFoundException
        MessageBox.Show("There was an error. Check the path to the bitmap.")
    End Try


End Sub

Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click

    If bitmap1 IsNot Nothing Then
   bitmap1.RotateFlip(RotateFlipType.Rotate180FlipY)
        PictureBox1.Image = bitmap1
    End If

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM