VB.Net Tutorial/2D Graphics/Matrix — различия между версиями

Материал из VB Эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 12:55, 26 мая 2010

Assign Matrix to Graphics.Transform

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class ScalingMatrixOrder
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class
public class Form1
  Inherits System.Windows.Forms.Form
  Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        Dim g As Graphics = Me.CreateGraphics()
        g.Clear(Me.BackColor)
        Dim curBitmap As New Bitmap("yourfile.jpg") "
        Dim X As New Matrix
        X.Scale(2, 1, MatrixOrder.Append)
        g.Transform = X
        g.DrawImage(curBitmap, New Rectangle(0, 0, 200, 200), 0, 0, curBitmap.Width, curBitmap.Height, GraphicsUnit.Pixel)
        curBitmap.Dispose()
        g.Dispose()
  End Sub
  Public Sub New()
   
    MyBase.New()
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(292, 273)
    Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
  End Sub
End Class

Matrix.Invert

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class MatrixReflection
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class
public class Form1
  Inherits System.Windows.Forms.Form
  Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        Dim g As Graphics = Me.CreateGraphics()
        g.Clear(Me.BackColor)
        Dim curBitmap As New Bitmap("yourfile.jpg") "
        Dim X As New Matrix
        X.Invert()
        g.Transform = X
        g.DrawImage(curBitmap, New Rectangle(0, 0, 200, 200), 0, 0, curBitmap.Width, curBitmap.Height, GraphicsUnit.Pixel)
        " Dispose
        curBitmap.Dispose()
        g.Dispose()
  End Sub
  Public Sub New()
   
    MyBase.New()
    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
    Me.ClientSize = New System.Drawing.Size(292, 273)
    Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
  End Sub
End Class

Matrix Operation Invert

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class Test
   public Shared Sub Main
        Console.WriteLine("Original values: ")
        " Create a Matrix object
        Dim X As New Matrix(2, 1, 3, 1, 0, 4)
        " Write its values
        Dim i As Integer
        For i = 0 To X.Elements.Length - 1
            Console.WriteLine(X.Elements(i))
        Next i
        Console.WriteLine("Inverted values: ")
        " Invert Matrix
        X.Invert()
        Dim pts As Single() = X.Elements
        " Read inverted Matrix
        Dim i1 As Integer
        For i = 0 To pts.Length - 1
            Console.WriteLine(pts(i))
        Next i
   End Sub
End class

Matrix Operation Multiply

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class Test
   public Shared Sub Main
        Dim X As New Matrix(2.0F, 1.0F, 3.0F, 1.0F, 0.0F, 4.0F)
        Dim Y As New Matrix(0.0F, 1.0F, -1.0F, 0.0F, 0.0F, 0.0F)
        X.Multiply(Y, MatrixOrder.Append)
        " Read the result Matrix
        Dim i As Integer
        For i = 0 To X.Elements.Length - 1
            Console.WriteLine(X.Elements(i).ToString())
        Next i

   End Sub
End class

Matrix: RotateAt

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class MatrixRotateAt
   public Shared Sub Main
        Application.Run(New RotateForm)
   End Sub
End class

Public Class RotateForm
    Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
    Public Sub New()
        MyBase.New()
        "This call is required by the Windows Form Designer.
        InitializeComponent()
        "Add any initialization after the InitializeComponent() call
    End Sub
    "Form overrides dispose to clean up the component list.
    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
    "Required by the Windows Form Designer
    Private components As System.ruponentModel.IContainer
    "NOTE: The following procedure is required by the Windows Form Designer
    "It can be modified using the Windows Form Designer.  
    "Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        "
        "RotateForm
        "
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(280, 214)
        Me.Name = "RotateForm"
        Me.Text = "RotateForm"
    End Sub
#End Region

    Sub RotateForm_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint
        Dim g As Graphics = e.Graphics
        Dim x As Integer = 400
        Dim y As Integer = 400
        Dim width As Integer = 250
        Dim height As Integer = 250
        Dim textWidth As Single = g.MeasureString("00", Me.Font).Width
        Dim length As Single = 250
        Dim textRect As RectangleF = New RectangleF(x + length, y - Me.Font.GetHeight(g) / 2, length, textWidth)
        Dim format As StringFormat = New StringFormat()
        format.Alignment = StringAlignment.Near
        format.LineAlignment = StringAlignment.Center
        Dim i As Integer
        For i = 0 To 360 Step 5
            Dim matrix As Matrix = New Matrix()
            matrix.RotateAt(i, New PointF(x, y))
            g.Transform = matrix
            g.DrawLine(Pens.Black, x, y, x + length, y)
            g.DrawString(i.ToString(), Me.Font, Brushes.Black, textRect, format)
        Next
    End Sub
End Class

Matrix: Shear(0,0)

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class Shearing0
   public Shared Sub Main
        Application.Run(New ShearingForm)
   End Sub
End class

Public Class ShearingForm
    Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
    Public Sub New()
        MyBase.New()
        "This call is required by the Windows Form Designer.
        InitializeComponent()
        "Add any initialization after the InitializeComponent() call
    End Sub
    "Form overrides dispose to clean up the component list.
    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
    "Required by the Windows Form Designer
    Private components As System.ruponentModel.IContainer
    "NOTE: The following procedure is required by the Windows Form Designer
    "It can be modified using the Windows Form Designer.  
    "Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        components = New System.ruponentModel.Container()
        Me.Text = "ShearingForm"
    End Sub
#End Region

    Sub Foo(ByVal g As Graphics)
        Dim matrix As Matrix = New Matrix()
        matrix.Translate(10, 20)
        matrix.Scale(2, 3)
        matrix.Reset()
        matrix.Scale(2, 3)
        matrix.Translate(10, 20, MatrixOrder.Append)
        g.Transform = matrix
        g.DrawString("(0,0)", Me.Font, Brushes.Black, 0, 0)
    End Sub
    Sub ShearingForm_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint
        Dim g As Graphics = e.Graphics
        Dim width As Single = 200
        Dim height As Single = 50
        Dim format As StringFormat = New StringFormat()
        format.Alignment = StringAlignment.Center
        format.LineAlignment = StringAlignment.Center
        Dim rect As RectangleF = New RectangleF(0, 0, width, height)
        Dim matrix As Matrix = New Matrix()
        matrix.Shear(0.0F, 0.0F)
        g.Transform = matrix
        g.DrawString("Shear(0,0)", Me.Font, Brushes.Black, rect, format)
        g.DrawRectangle(Pens.Black, rect.X, rect.Y, rect.Width, rect.Height)
    End Sub
End Class

Matrix: Shear(0)

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class Shearing05
   public Shared Sub Main
        Application.Run(New ShearingForm)
   End Sub
End class

Public Class ShearingForm
    Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
    Public Sub New()
        MyBase.New()
        "This call is required by the Windows Form Designer.
        InitializeComponent()
        "Add any initialization after the InitializeComponent() call
    End Sub
    "Form overrides dispose to clean up the component list.
    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
    "Required by the Windows Form Designer
    Private components As System.ruponentModel.IContainer
    "NOTE: The following procedure is required by the Windows Form Designer
    "It can be modified using the Windows Form Designer.  
    "Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        components = New System.ruponentModel.Container()
        Me.Text = "ShearingForm"
    End Sub
#End Region
    Sub ShearingForm_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint
        Dim g As Graphics = e.Graphics
        Dim width As Single = 200
        Dim height As Single = 50
        Dim format As StringFormat = New StringFormat()
        format.Alignment = StringAlignment.Center
        format.LineAlignment = StringAlignment.Center
        Dim rect As RectangleF = New RectangleF(0, 0, width, height)
        Dim matrix As Matrix = New Matrix()
        matrix.Shear(0.5F, 0.0F)
        g.Transform = matrix
        g.DrawString("Shear(0,0)", Me.Font, Brushes.Black, rect, format)
        g.DrawRectangle(Pens.Black, rect.X, rect.Y, rect.Width, rect.Height)
    End Sub
End Class

Matrix: Shear(1,0)

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class Shearing11
   public Shared Sub Main
        Application.Run(New ShearingForm)
   End Sub
End class

Public Class ShearingForm
    Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
    Public Sub New()
        MyBase.New()
        "This call is required by the Windows Form Designer.
        InitializeComponent()
        "Add any initialization after the InitializeComponent() call
    End Sub
    "Form overrides dispose to clean up the component list.
    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
    "Required by the Windows Form Designer
    Private components As System.ruponentModel.IContainer
    "NOTE: The following procedure is required by the Windows Form Designer
    "It can be modified using the Windows Form Designer.  
    "Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        components = New System.ruponentModel.Container()
        Me.Text = "ShearingForm"
    End Sub
#End Region
    Sub ShearingForm_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint
        Dim g As Graphics = e.Graphics
        Dim width As Single = 200
        Dim height As Single = 50
        Dim format As StringFormat = New StringFormat()
        format.Alignment = StringAlignment.Center
        format.LineAlignment = StringAlignment.Center
        Dim rect As RectangleF = New RectangleF(0, 0, width, height)
        Dim matrix As Matrix = New Matrix()
        matrix.Shear(1.0F, 0.0F)
        g.Transform = matrix
        g.DrawRectangle(Pens.Black, rect.X, rect.Y, rect.Width, rect.Height)
    End Sub
End Class

Matrix: Translate

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class MatrixTranslationDemo
   public Shared Sub Main
        Application.Run(New TranslationForm)
   End Sub
End class

Public Class TranslationForm
    Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
    Public Sub New()
        MyBase.New()
        "This call is required by the Windows Form Designer.
        InitializeComponent()
        "Add any initialization after the InitializeComponent() call
    End Sub
    "Form overrides dispose to clean up the component list.
    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
    "Required by the Windows Form Designer
    Private components As System.ruponentModel.IContainer
    "NOTE: The following procedure is required by the Windows Form Designer
    "It can be modified using the Windows Form Designer.  
    "Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        components = New System.ruponentModel.Container()
        Me.Text = "TranslationForm"
    End Sub
#End Region
    Sub TranlationForm_Pain(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint
        Dim g As Graphics = e.Graphics
        Dim rect As RectangleF = New RectangleF(0, 0, 125, 125)
        g.FillRectangle(Brushes.White, rect)
        g.DrawRectangle(Pens.Black, rect.X, rect.Y, rect.Width, rect.Height)
        Dim matrix As Matrix = New Matrix()
        matrix.Translate(150, 150)
        g.Transform = matrix
        g.FillRectangle(Brushes.White, rect)
        g.DrawRectangle(Pens.Black, rect.X, rect.Y, rect.Width, rect.Height)
    End Sub
End Class

Mirror Text: use Drawing2D.Matrix to transform Graphics

"Visual Basic 2005 Cookbook Solutions for VB 2005 Programmers
"by Tim Patrick (Author), John Craig (Author)
"# Publisher: O"Reilly Media, Inc. (September 21, 2006)
"# Language: English
"# ISBN-10: 0596101775
"# ISBN-13: 978-0596101770

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class MirrorText
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class
Public Class Form1
    Private Const QuoteText As String = "www.vbex.ru"
    Private Sub VerticalMirror_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VerticalMirror.CheckedChanged
        MirroredText.Invalidate()
    End Sub
    Private Sub MirroredText_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MirroredText.Paint
        Dim drawingArea As Rectangle
        Dim saveState As Drawing2D.GraphicsState
        Dim mirrorMatrix As Drawing2D.Matrix
        e.Graphics.Clear(Color.White)
        If (VerticalMirror.Checked = True) Then
            drawingArea = New Rectangle(5, 5, _
                (MirroredText.ClientRectangle.Width \ 2) - 10, _
                MirroredText.ClientRectangle.Height - 10)
            e.Graphics.DrawLine(Pens.Black, MirroredText.ClientRectangle.Width \ 2, _
                5, MirroredText.ClientRectangle.Width \ 2, _
                MirroredText.ClientRectangle.Height - 10)
        Else
            drawingArea = New Rectangle(5, 5, _
                MirroredText.ClientRectangle.Width - 10, _
                (MirroredText.ClientRectangle.Height \ 2) - 10)
            e.Graphics.DrawLine(Pens.Black, 5, _
                MirroredText.ClientRectangle.Height \ 2, _
                MirroredText.ClientRectangle.Width - 10, _
                MirroredText.ClientRectangle.Height \ 2)
        End If
        e.Graphics.DrawString(QuoteText, MirroredText.Font, _
            Brushes.Black, drawingArea)
        saveState = e.Graphics.Save()
        If (VerticalMirror.Checked = True) Then
            mirrorMatrix = New Drawing2D.Matrix(-1, 0, 0, 1, MirroredText.ClientRectangle.Width, 0)
        Else
            mirrorMatrix = New Drawing2D.Matrix(1, 0, 0, -1, 0, MirroredText.ClientRectangle.Height)
        End If
        e.Graphics.Transform = mirrorMatrix
        e.Graphics.DrawString(QuoteText, MirroredText.Font, _
            Brushes.Black, drawingArea)
        e.Graphics.Restore(saveState)
    End Sub
End Class

<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form
    "Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub
    "Required by the Windows Form Designer
    Private components As System.ruponentModel.IContainer
    "NOTE: The following procedure is required by the Windows Form Designer
    "It can be modified using the Windows Form Designer.  
    "Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.VerticalMirror = New System.Windows.Forms.RadioButton
        Me.MirroredText = New System.Windows.Forms.PictureBox
        Me.HorizontalMirror = New System.Windows.Forms.RadioButton
        CType(Me.MirroredText, System.ruponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        "
        "VerticalMirror
        "
        Me.VerticalMirror.AutoSize = True
        Me.VerticalMirror.Checked = True
        Me.VerticalMirror.Location = New System.Drawing.Point(8, 8)
        Me.VerticalMirror.Name = "VerticalMirror"
        Me.VerticalMirror.Size = New System.Drawing.Size(60, 17)
        Me.VerticalMirror.TabIndex = 0
        Me.VerticalMirror.TabStop = True
        Me.VerticalMirror.Text = "&Vertical"
        Me.VerticalMirror.UseVisualStyleBackColor = True
        "
        "MirroredText
        "
        Me.MirroredText.BackColor = System.Drawing.Color.White
        Me.MirroredText.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.MirroredText.Location = New System.Drawing.Point(104, 8)
        Me.MirroredText.Name = "MirroredText"
        Me.MirroredText.Size = New System.Drawing.Size(304, 168)
        Me.MirroredText.TabIndex = 1
        Me.MirroredText.TabStop = False
        "
        "HorizontalMirror
        "
        Me.HorizontalMirror.AutoSize = True
        Me.HorizontalMirror.Location = New System.Drawing.Point(8, 32)
        Me.HorizontalMirror.Name = "HorizontalMirror"
        Me.HorizontalMirror.Size = New System.Drawing.Size(72, 17)
        Me.HorizontalMirror.TabIndex = 1
        Me.HorizontalMirror.Text = "&Horizontal"
        Me.HorizontalMirror.UseVisualStyleBackColor = True
        "
        "Form1
        "
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(419, 187)
        Me.Controls.Add(Me.HorizontalMirror)
        Me.Controls.Add(Me.MirroredText)
        Me.Controls.Add(Me.VerticalMirror)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
        Me.MaximizeBox = False
        Me.Name = "Form1"
        Me.Text = "Mirror Text"
        CType(Me.MirroredText, System.ruponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)
        Me.PerformLayout()
    End Sub
    Friend WithEvents VerticalMirror As System.Windows.Forms.RadioButton
    Friend WithEvents MirroredText As System.Windows.Forms.PictureBox
    Friend WithEvents HorizontalMirror As System.Windows.Forms.RadioButton
End Class