|
|
Title | Flip an image and/or rotate it by a multiple of 90 degrees in VB .NET |
Keywords | rotate, flip, image, VB.NET, graphics |
Categories | Graphics, VB.NET |
|
|
Make a new Bitmap containing the input image. Call the Bitmap's RotateFlip method passing it a parameter telling how you want to rotate and flip the image. Assign the result to a PictureBox control's Image property.
|
|
' Rotate the image.
Private Sub btnRotate_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnRotate.Click
' Make sure something is selected.
If cboRotationType.SelectedIndex < 0 Then Exit Sub
' Make the output bitmap.
Dim bm_out As New Bitmap(picSource.Image)
' Find the rotation name.
Dim rotation_name As String = cboRotationType.Text
rotation_name = rotation_name.Substring(0, _
rotation_name.IndexOf(" ("))
Select Case rotation_name
Case "Rotate180FlipNone"
bm_out.RotateFlip(RotateFlipType.Rotate180FlipNone)
Case "Rotate180FlipX"
bm_out.RotateFlip(RotateFlipType.Rotate180FlipX)
Case "Rotate180FlipXY"
bm_out.RotateFlip(RotateFlipType.Rotate180FlipXY)
Case "Rotate180FlipY"
bm_out.RotateFlip(RotateFlipType.Rotate180FlipY)
Case "Rotate270FlipNone"
bm_out.RotateFlip(RotateFlipType.Rotate270FlipNone)
Case "Rotate270FlipX"
bm_out.RotateFlip(RotateFlipType.Rotate270FlipX)
Case "Rotate270FlipXY"
bm_out.RotateFlip(RotateFlipType.Rotate270FlipXY)
Case "Rotate270FlipY"
bm_out.RotateFlip(RotateFlipType.Rotate270FlipY)
Case "Rotate90FlipNone"
bm_out.RotateFlip(RotateFlipType.Rotate90FlipNone)
Case "Rotate90FlipX"
bm_out.RotateFlip(RotateFlipType.Rotate90FlipX)
Case "Rotate90FlipXY"
bm_out.RotateFlip(RotateFlipType.Rotate90FlipXY)
Case "Rotate90FlipY"
bm_out.RotateFlip(RotateFlipType.Rotate90FlipY)
Case "RotateNoneFlipNone"
bm_out.RotateFlip(RotateFlipType.RotateNoneFlipNone)
Case "RotateNoneFlipX"
bm_out.RotateFlip(RotateFlipType.RotateNoneFlipX)
Case "RotateNoneFlipXY"
bm_out.RotateFlip(RotateFlipType.RotateNoneFlipXY)
Case "RotateNoneFlipY"
bm_out.RotateFlip(RotateFlipType.RotateNoneFlipY)
End Select
' Display the result.
picDest.Image = bm_out
End Sub
|
|
In this example, picDest has SizeMode = AutoSize so the control automatically resizes to fit the image.
|
|
|
|
|
|