Rotate an Image : Image Operation « 2D Graphics « VB.Net Tutorial






Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

public class RotateImage
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class

Public Class Form1
    Inherits System.Windows.Forms.Form

    Public Sub New()
        MyBase.New()
        InitializeComponent()

    End Sub

    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    Private components As System.ComponentModel.IContainer

    Friend WithEvents Button2 As System.Windows.Forms.Button
    Friend WithEvents Button3 As System.Windows.Forms.Button
    Friend WithEvents Button4 As System.Windows.Forms.Button
    Friend WithEvents Panel1 As System.Windows.Forms.Panel
    Friend WithEvents Panel2 As System.Windows.Forms.Panel
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button2 = New System.Windows.Forms.Button
        Me.Button4 = New System.Windows.Forms.Button
        Me.Panel1 = New System.Windows.Forms.Panel
        Me.Panel2 = New System.Windows.Forms.Panel
        Me.SuspendLayout()
        '
        'Button2
        '
        Me.Button2.Location = New System.Drawing.Point(88, 232)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(64, 24)
        Me.Button2.TabIndex = 3
        Me.Button2.Text = "Rotate"
        '
        'Button4
        '
        Me.Button4.Location = New System.Drawing.Point(328, 232)
        Me.Button4.Name = "Button4"
        Me.Button4.Size = New System.Drawing.Size(56, 24)
        Me.Button4.TabIndex = 5
        Me.Button4.Text = "Exit"
        '
        'Panel1
        '
        Me.Panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.Panel1.Location = New System.Drawing.Point(24, 16)
        Me.Panel1.Name = "Panel1"
        Me.Panel1.Size = New System.Drawing.Size(200, 184)
        Me.Panel1.TabIndex = 10
        '
        'Panel2
        '
        Me.Panel2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.Panel2.Location = New System.Drawing.Point(248, 16)
        Me.Panel2.Name = "Panel2"
        Me.Panel2.Size = New System.Drawing.Size(208, 184)
        Me.Panel2.TabIndex = 11
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(464, 286)
        Me.Controls.Add(Me.Panel2)
        Me.Controls.Add(Me.Panel1)
        Me.Controls.Add(Me.Button4)
        Me.Controls.Add(Me.Button3)
        Me.Controls.Add(Me.Button2)
        Me.ResumeLayout(False)

    End Sub

    Dim pic1, pic2 As Bitmap


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Panel2.Invalidate()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        End
    End Sub

    Private Sub Panel1_Paint1(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
        Dim g As Graphics = e.Graphics
        g.DrawImage(pic1, 0, 0)
    End Sub

    Private Sub Panel2_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel2.Paint
        Dim g As Graphics = e.Graphics
        Dim x As Integer = Panel2.Width
        Dim col As Color = New Color
        
        Dim pic3 As Bitmap
        pic3 = New Bitmap(x, x)
        Dim i, j As Integer
        For i = 0 To x - 1
            For j = 0 To x - 1
                col = pic2.GetPixel(i, j)
                pic3.SetPixel(i, x - 1 - j, col)
            Next
        Next
        g.DrawImage(pic3, 0, 0)
        pic2 = New Bitmap(pic3)
    End Sub


    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Panel1.Height = Panel1.Width
        Panel2.Width = Panel1.Width
        Panel2.Height = Panel2.Width
        Dim pic As Image = Image.FromFile("YourFile.bmp")
        pic1 = New Bitmap(pic, Panel1.Width, Panel1.Height)
        pic2 = New Bitmap(pic1)
    End Sub

End Class








17.49.Image Operation
17.49.1.Rotate an Image
17.49.2.Mirror an Image
17.49.3.Move an image and paintMove an image and paint
17.49.4.Scale an imageScale an image
17.49.5.Scale image with different Graphics UnitsScale image with different Graphics Units
17.49.6.Skew imageSkew image
17.49.7.Make an image transparentMake an image transparent
17.49.8.Invert an ImageInvert an Image
17.49.9.Invert an Image unsafeInvert an Image unsafe
17.49.10.Zoom ImageZoom Image
17.49.11.Zoom and Skew an ImageZoom and Skew an Image
17.49.12.Skew an ImageSkew an Image