VB.Net Tutorial/2D Graphics/Clip

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

Clip by GraphicPath

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class GraphicPathClip
   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 txt As String = "www.vbex.ru"
        Dim graphics_path As New GraphicsPath
        graphics_path.AddString(txt, _
            New FontFamily("Times New Roman"), _
            FontStyle.Bold, 150, _
            New Point(0, 0), _
            New StringFormat)
        e.Graphics.SetClip(graphics_path)
        e.Graphics.FillRectangle(Brushes.White, Me.ClientRectangle)
        Dim rnd As New Random
        Dim x1, y1, x2, y2 As Integer
        For i As Integer = 1 To 400
            x1 = rnd.Next(-20, Me.ClientSize.Width - 1)
            y1 = rnd.Next(0, Me.ClientSize.Height - 1)
            x2 = rnd.Next(500, Me.ClientSize.Width - 1)
            y2 = rnd.Next(0, Me.ClientSize.Height - 1)
            e.Graphics.DrawLine(Pens.Black, x1, y1, x2, y2)
        Next i
  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

Clip Region

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class RegionClip
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class
Public Class Form1
    Private Sub Form11_Paint(ByVal sender As Object, _
          ByVal e As System.Windows.Forms.PaintEventArgs) _
          Handles Me.Paint
        Dim canvas As Graphics
        Dim fencePath As GraphicsPath
        Dim slottedRegion As Region
        canvas = e.Graphics
        canvas.Clear(Color.White)
        fencePath = New GraphicsPath
        fencePath.AddRectangle(New Rectangle(2 * 10, 0, 20, 500))
        fencePath.AddRectangle(New Rectangle(8 * 10, 0, 20, 500))
        fencePath.AddRectangle(New Rectangle(14 * 10, 0, 20, 500))
        fencePath.AddRectangle(New Rectangle(20 * 10, 0, 20, 500))
        slottedRegion = New Region(fencePath)
        canvas.Clip = slottedRegion
        canvas.Clear(Color.Aqua)
        canvas.DrawString("www.vbex.ru", New Font("Times New Roman", 77),Brushes.Blue, 20, 20)
    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.SuspendLayout()
        "
        "Form1
        "
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(617, 197)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
        Me.MaximizeBox = False
        Me.Name = "Form1"
        Me.Text = "Using Regions Effectively"
        Me.ResumeLayout(False)
    End Sub
End Class

Clip Region Exclude

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class ClipRegionExclude
   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 pen As New Pen(Color.Red, 5)
        Dim brush As New SolidBrush(Color.Red)
        Dim rect1 As New Rectangle(50, 0, 50, 150)
        Dim rect2 As New Rectangle(0, 50, 150, 50)
        Dim [region] As New [Region](rect1)
        [region].Exclude(rect2)
        g.FillRegion(brush, [region])
        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

Clip Region Intersect

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class ClipRegionIntersect
   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 pen As New Pen(Color.Red, 5)
        Dim brush As New SolidBrush(Color.Red)
        Dim rect1 As New Rectangle(50, 0, 50, 150)
        Dim rect2 As New Rectangle(0, 50, 150, 50)
        Dim [region] As New [Region](rect1)
        [region].Intersect(rect2)
        g.FillRegion(brush, [region])
        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

Clip Region Union

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class ClipRegionUnion
   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 pen As New Pen(Color.Red, 5)
        Dim brush As New SolidBrush(Color.Red)
        Dim rect1 As New Rectangle(50, 0, 50, 150)
        Dim rect2 As New Rectangle(0, 50, 150, 50)
        Dim [region] As New [Region](rect1)
        [region].Union(rect2)
        g.FillRegion(brush, [region])
        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

Clip Region Xor

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class ClipRegionXor
   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 pen As New Pen(Color.Red, 5)
        Dim brush As New SolidBrush(Color.Red)
        Dim rect1 As New Rectangle(50, 0, 50, 150)
        Dim rect2 As New Rectangle(0, 50, 150, 50)
        Dim [region] As New [Region](rect1)
        [region].Xor(rect2)
        g.FillRegion(brush, [region])
        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

CombineMode.Exclude

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class CombineModeExclude
   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 rect1 As New RectangleF(20.0F, 20.0F, 200.0F, 200.0F)
        Dim rgn1 As New [Region](rect1)
        g.SetClip(rgn1, CombineMode.Exclude)
        Dim h As Single = 20.0F
        Dim w As Single = 30.0F
        g.TranslateClip(h, w)
        g.FillRectangle(Brushes.Green, 20, 20, 300, 300)
  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

Rectangle clipped to Ellipse

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

Public Class ClippingRegionForm
    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 = "ClippingRegionForm"
    End Sub
#End Region

    Dim rnd As Random = New Random()

    Private Sub ClippingRegionForm_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        Dim g As Graphics = e.Graphics
        Dim path As GraphicsPath = New GraphicsPath()
        path.AddEllipse(Me.ClientRectangle)
        Dim region As Region = New Region(path)
        g.DrawPath(Pens.Red, path)
        g.Clip = region
        Dim rect As Rectangle = Me.ClientRectangle
        rect.Offset(10, 10)
        rect.Width = rect.Width - 20
        rect.Height = rect.Height - 20
        g.FillRectangle(Brushes.Black, rect)
    End Sub
End Class