VB.Net Tutorial/2D Graphics/Graphics — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 12:55, 26 мая 2010
Содержание
- 1 Create Graphics from Handle
- 2 Create Graphics object from System.Windows.Forms.PaintEventArgs
- 3 Graphics: clear background
- 4 Graphics.DpiX
- 5 Graphics.DrawArc
- 6 Graphics.DrawBeziers
- 7 Graphics.DrawClosedCurve
- 8 Graphics.DrawCurve
- 9 Graphics.DrawEllipse Method
- 10 Graphics.DrawIcon
- 11 Graphics.DrawIconUnstretched
- 12 Graphics.DrawImage
- 13 Graphics.DrawLines
- 14 Graphics.DrawPath
- 15 Graphics.DrawPie
- 16 Graphics.DrawPolygon
- 17 Graphics.DrawString
- 18 Graphics.ExcludeClip
- 19 Graphics.FillClosedCurve
- 20 Graphics.FillEllipse
- 21 Graphics.FillPath
- 22 Graphics.FillPie
- 23 Graphics.FillPolygon
- 24 Graphics.FillRectangle
- 25 Graphics.FillRectangles
- 26 Graphics.IntersectClip
- 27 Graphics.MeasureString
- 28 Graphics.ReleaseHdc
- 29 Graphics.ResetClip
- 30 Graphics.SetClip
- 31 Use Graphics.DrawEllipse to draw a circle
Create Graphics from Handle
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class GraphicsFromHwnd
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 System.Drawing.Graphics = Graphics.FromHwnd(Me.Handle)
Dim points(3) As System.Drawing.Point
points(0) = New Point(120, 60) "Top Left of Trapezoid
points(1) = New Point(180, 60) "Top Right of Trapezoid
points(2) = New Point(240, 120) "Bottom Right of Trapezoid
points(3) = New Point(60, 120) "Bottom Left of Trapezoid
G.DrawPolygon(New Pen(Color.Blue), points)
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
Create Graphics object from System.Windows.Forms.PaintEventArgs
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class GDIObjectsCreateGraphic
public Shared Sub Main
Application.Run(New Form1)
End Sub
End class
Public Class Form1
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim canvas As Graphics = e.Graphics
Dim colorBackground As Color = Color.Cornsilk
canvas.Clear(colorBackground)
canvas = Nothing
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(610, 328)
Me.Name = "Form1"
Me.Text = "Creating Graphics Objects (Color, Pen, Font, Brush)"
Me.ResumeLayout(False)
End Sub
End Class
Graphics: clear background
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class GDIObjectsCreateGraphic
public Shared Sub Main
Application.Run(New Form1)
End Sub
End class
Public Class Form1
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim canvas As Graphics = e.Graphics
Dim colorBackground As Color = Color.Cornsilk
canvas.Clear(colorBackground)
canvas = Nothing
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(610, 328)
Me.Name = "Form1"
Me.Text = "Creating Graphics Objects (Color, Pen, Font, Brush)"
Me.ResumeLayout(False)
End Sub
End Class
Graphics.DpiX
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class GraphicsDpiX
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
e.Graphics.PageUnit = GraphicsUnit.Millimeter
e.Graphics.DrawLine(New Pen(Color.Black, e.Graphics.DpiX), 0, 0, 200, 200)
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.MaximizeBox = False
Me.ResumeLayout(False)
End Sub
End Class
Graphics.DrawArc
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class DrawArc
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)
" Drawing an arc
Dim g As Graphics = e.Graphics
Dim pn As New Pen(Color.Blue)
Dim rect As New Rectangle(50, 50, 200, 100)
g.DrawArc(pn, rect, 12, 84)
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
Graphics.DrawBeziers
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class DrawBeziers
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)
" Drawing Beziers
Dim g As Graphics = e.Graphics
" Create a green pen.
Dim greenPen As New Pen(Color.Green, 2)
" Draw bezier
e.Graphics.DrawBezier(greenPen, 20.0F, 30.0F, 100.0F, 200.0F, 40.0F, 400.0F, 100.0F, 200.0F)
" Create pen.
Dim redPen As New Pen(Color.Red, 2)
" Create points for curve.
Dim p1 As New PointF(40.0F, 50.0F)
Dim p2 As New PointF(60.0F, 70.0F)
Dim p3 As New PointF(80.0F, 34.0F)
Dim p4 As New PointF(120.0F, 180.0F)
Dim p5 As New PointF(200.0F, 150.0F)
Dim p6 As New PointF(350.0F, 250.0F)
Dim p7 As New PointF(200.0F, 200.0F)
Dim ptsArray As PointF() = {p1, p2, p3, p4, p5, p6, p7}
e.Graphics.DrawBeziers(redPen, ptsArray)
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
Graphics.DrawClosedCurve
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class ClosedCurve
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 pts() As Point = { _
New Point(10, 50), _
New Point(200, 30), _
New Point(120, 200), _
New Point(230, 150), _
New Point(150, 50), _
New Point(250, 200), _
New Point(100, 250) _
}
e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
For tension As Single = 0 To 1 Step 0.25
Dim curve_pen As New Pen(Color.Black, tension * 4 + 1)
e.Graphics.DrawClosedCurve(curve_pen, pts, tension, Drawing2D.FillMode.Alternate)
Next tension
" Draw rectangles on the control points.
For i As Integer = 0 To pts.Length - 1
e.Graphics.FillRectangle(Brushes.White, pts(i).X - 2, pts(i).Y - 2, 5, 5)
e.Graphics.DrawRectangle(Pens.Black, pts(i).X - 2, pts(i).Y - 2, 5, 5)
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
Graphics.DrawCurve
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class DrawCurve
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)
" Create a pen
Dim bluePen As New Pen(Color.Blue, 1)
" Create an array of points
Dim pt1 As New PointF(40.0F, 50.0F)
Dim pt2 As New PointF(50.0F, 75.0F)
Dim pt3 As New PointF(100.0F, 115.0F)
Dim pt4 As New PointF(200.0F, 180.0F)
Dim pt5 As New PointF(200.0F, 90.0F)
Dim ptsArray As PointF() = {pt1, pt2, pt3, pt4, pt5}
" Draw curve
Dim offset As Integer = 1
Dim segments As Integer = 3
Dim tension As Single = 0.5F
e.Graphics.DrawCurve(bluePen, ptsArray, offset, segments, tension)
" Dispose
bluePen.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
Graphics.DrawEllipse Method
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class DrawShapes
public Shared Sub Main
Application.Run(New FrmComboBox)
End Sub
End class
Public Class FrmComboBox
Inherits 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
" contains shape list (circle, square, ellipse, pie)
Friend WithEvents cboImage As System.Windows.Forms.ruboBox
"Required by the Windows Form Designer
Private components As System.ruponentModel.Container
"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.cboImage = New System.Windows.Forms.ruboBox()
Me.SuspendLayout()
"
"cboImage
"
Me.cboImage.DropDownWidth = 121
Me.cboImage.Items.AddRange(New Object() {"Circle", "Square", "Ellipse", "Pie", "Filled Circle", "Filled Square", "Filled Ellipse", "Filled Pie"})
Me.cboImage.Location = New System.Drawing.Point(24, 16)
Me.cboImage.Name = "cboImage"
Me.cboImage.Size = New System.Drawing.Size(121, 21)
Me.cboImage.TabIndex = 0
"
"FrmComboBox
"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.cboImage})
Me.Name = "FrmComboBox"
Me.Text = "ComboBoxTest"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub cboImage_SelectedIndexChanged _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles cboImage.SelectedIndexChanged
Dim myGraphics As Graphics = MyBase.CreateGraphics()
Dim myPen As New Pen(Color.DarkRed)
Dim mySolidBrush As New SolidBrush(Color.DarkRed)
myGraphics.Clear(Color.White)
Select Case cboImage.SelectedIndex
Case 0 " case circle is selected
myGraphics.DrawEllipse(myPen, 50, 50, 150, 150)
Case 1
myGraphics.DrawRectangle(myPen, 50, 50, 150, 150)
Case 2
myGraphics.DrawEllipse(myPen, 50, 85, 150, 115)
Case 3
myGraphics.DrawPie(myPen, 50, 50, 150, 150, 0, 45)
Case 4
myGraphics.FillEllipse(mySolidBrush, 50, 50, 150, 150)
Case 5
myGraphics.FillRectangle(mySolidBrush, 50, 50, 150, 150)
Case 6
myGraphics.FillEllipse(mySolidBrush, 50, 85, 150, 115)
Case 7
myGraphics.FillPie(mySolidBrush, 50, 50, 150, 150, 0, 45)
End Select
End Sub
End Class
Graphics.DrawIcon
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class DrawIcon
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 icon1 As New Icon("1.ico")
Dim icon2 As New Icon("2.ico")
Dim x As Integer = 20
Dim y As Integer = 50
e.Graphics.DrawIcon(icon1, x, y)
Dim rect As New Rectangle(100, 200, 400, 400)
e.Graphics.DrawIconUnstretched(icon2, rect)
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
Graphics.DrawIconUnstretched
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class DrawIcon
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 icon1 As New Icon("1.ico")
Dim icon2 As New Icon("2.ico")
Dim x As Integer = 20
Dim y As Integer = 50
e.Graphics.DrawIcon(icon1, x, y)
Dim rect As New Rectangle(100, 200, 400, 400)
e.Graphics.DrawIconUnstretched(icon2, rect)
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
Graphics.DrawImage
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class DrawImage
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 imageFile As Image = Image.FromFile("yourfile.jpg")
Dim g As Graphics = Me.CreateGraphics() "
Dim newGraphics As Graphics = Graphics.FromImage(imageFile)
newGraphics.FillRectangle(New SolidBrush(Color.Black), 100, 50, 100, 100)
g.DrawImage(imageFile, New PointF(0.0F, 0.0F))
newGraphics.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
Graphics.DrawLines
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class DrawLines
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 = e.Graphics
Dim pn As New Pen(Color.Blue)
" Rectangle rect = new Rectangle(50, 50, 200, 100);
Dim pt1 As New Point(30, 30)
Dim pt2 As New Point(110, 100)
g.DrawLine(pn, pt1, pt2)
" Create a pen with red color and width 3
Dim redPen As New Pen(Color.Red, 3)
" Create an array of points
Dim ptsArray As PointF() = {New PointF(10.0F, 20.0F), New PointF(50.0F, 40.0F), New PointF(220.0F, 120.0F), New PointF(120.0F, 20.0F)}
g.DrawLines(redPen, ptsArray)
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
Graphics.DrawPath
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class DrawPath
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 greenPen As Pen = New Pen(Color.Green, 1)
Dim path As GraphicsPath = New GraphicsPath
path.AddLine(20, 20, 103, 80)
path.AddEllipse(100, 50, 100, 100)
path.AddLine(195, 80, 300, 80)
path.AddLine(200, 100, 300, 100)
path.AddLine(195, 120, 300, 120)
Dim rect As Rectangle = New Rectangle(50, 150, 300, 50)
path.AddRectangle(rect)
e.Graphics.DrawPath(greenPen, path)
greenPen.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
Graphics.DrawPie
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class DrawPie
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 startAngle As Single = 123
Dim sweepAngle As Single = 321
Dim bluePen As Pen = New Pen(Color.Blue, 1)
g.DrawPie(bluePen, 20, 20, 100, 100, startAngle, sweepAngle)
bluePen.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
Graphics.DrawPolygon
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class DrawPolygon
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 = e.Graphics
" Create pens
Dim greenPen As New Pen(Color.Green, 2)
Dim redPen As New Pen(Color.Red, 2)
" Create points for polygon.
Dim p1 As New PointF(40.0F, 50.0F)
Dim p2 As New PointF(60.0F, 70.0F)
Dim p3 As New PointF(80.0F, 34.0F)
Dim p4 As New PointF(120.0F, 180.0F)
Dim p5 As New PointF(200.0F, 150.0F)
Dim ptsArray As PointF() = {p1, p2, p3, p4, p5}
" Draw polygon
e.Graphics.DrawPolygon(greenPen, ptsArray)
" Dispose
greenPen.Dispose()
redPen.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
Graphics.DrawString
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class DrawFontString
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 blueBrush As New SolidBrush(Color.Blue)
Dim redBrush As New SolidBrush(Color.Red)
Dim greenBrush As New SolidBrush(Color.Green)
" Create a rectangle
Dim rect As New Rectangle(20, 20, 200, 100)
" The text to be drawn
Dim drawString As [String] = "Hello GDI+ World!"
" Create a Font
Dim drawFont As New Font("Verdana", 14)
Dim x As Single = 100.0F
Dim y As Single = 100.0F
" String format
Dim drawFormat As New StringFormat
" Set string format flag to direction vertical
" which draws text vertical
drawFormat.FormatFlags = StringFormatFlags.DirectionVertical
" Draw string
e.Graphics.DrawString("Drawing text", New Font("Tahoma", 14), greenBrush, rect.Location.X, rect.Location.Y)
e.Graphics.DrawString(drawString, New Font("Arial", 12), redBrush, 120, 140)
e.Graphics.DrawString(drawString, drawFont, blueBrush, x, y, drawFormat)
" Dispose
blueBrush.Dispose()
redBrush.Dispose()
greenBrush.Dispose()
drawFont.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
Graphics.ExcludeClip
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class ExcludeClip
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 = Graphics.FromHwnd(Me.Handle)
g.Clear(Me.BackColor)
Dim redBrush As New SolidBrush(Color.Red)
Dim exRect As New Rectangle(100, 100, 150, 100)
g.ExcludeClip(exRect)
g.FillRectangle(redBrush, 10, 10, 350, 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
Graphics.FillClosedCurve
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class FillClosedCurve
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)
" Create an array of points
Dim pt1 As New PointF(40.0F, 50.0F)
Dim pt2 As New PointF(50.0F, 75.0F)
Dim pt3 As New PointF(100.0F, 115.0F)
Dim pt4 As New PointF(200.0F, 180.0F)
Dim pt5 As New PointF(200.0F, 90.0F)
Dim ptsArray As PointF() = {pt1, pt2, pt3, pt4, pt5}
" Fill a closed curve
Dim tension As Single = 1.0F
Dim flMode As FillMode = FillMode.Alternate
Dim blueBrush As New SolidBrush(Color.Blue)
e.Graphics.FillClosedCurve(blueBrush, ptsArray, flMode, tension)
" Dispose
blueBrush.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
Graphics.FillEllipse
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class DrawShapes
public Shared Sub Main
Application.Run(New FrmComboBox)
End Sub
End class
Public Class FrmComboBox
Inherits 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
" contains shape list (circle, square, ellipse, pie)
Friend WithEvents cboImage As System.Windows.Forms.ruboBox
"Required by the Windows Form Designer
Private components As System.ruponentModel.Container
"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.cboImage = New System.Windows.Forms.ruboBox()
Me.SuspendLayout()
"
"cboImage
"
Me.cboImage.DropDownWidth = 121
Me.cboImage.Items.AddRange(New Object() {"Circle", "Square", "Ellipse", "Pie", "Filled Circle", "Filled Square", "Filled Ellipse", "Filled Pie"})
Me.cboImage.Location = New System.Drawing.Point(24, 16)
Me.cboImage.Name = "cboImage"
Me.cboImage.Size = New System.Drawing.Size(121, 21)
Me.cboImage.TabIndex = 0
"
"FrmComboBox
"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.cboImage})
Me.Name = "FrmComboBox"
Me.Text = "ComboBoxTest"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub cboImage_SelectedIndexChanged _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles cboImage.SelectedIndexChanged
Dim myGraphics As Graphics = MyBase.CreateGraphics()
Dim myPen As New Pen(Color.DarkRed)
Dim mySolidBrush As New SolidBrush(Color.DarkRed)
myGraphics.Clear(Color.White)
Select Case cboImage.SelectedIndex
Case 0 " case circle is selected
myGraphics.DrawEllipse(myPen, 50, 50, 150, 150)
Case 1
myGraphics.DrawRectangle(myPen, 50, 50, 150, 150)
Case 2
myGraphics.DrawEllipse(myPen, 50, 85, 150, 115)
Case 3
myGraphics.DrawPie(myPen, 50, 50, 150, 150, 0, 45)
Case 4
myGraphics.FillEllipse(mySolidBrush, 50, 50, 150, 150)
Case 5
myGraphics.FillRectangle(mySolidBrush, 50, 50, 150, 150)
Case 6
myGraphics.FillEllipse(mySolidBrush, 50, 85, 150, 115)
Case 7
myGraphics.FillPie(mySolidBrush, 50, 50, 150, 150, 0, 45)
End Select
End Sub
End Class
Graphics.FillPath
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class FillPath
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 = e.Graphics
Dim graphPath As New GraphicsPath
graphPath.AddEllipse(50, 50, 150, 200)
Dim blackBrush As New SolidBrush(Color.Black)
e.Graphics.FillPath(blackBrush, graphPath)
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
Graphics.FillPie
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class FillPie
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)
" Drawing a Pie
Dim g As Graphics = e.Graphics
g.FillPie(New SolidBrush(Color.Red), 0.0F, 0.0F, 100, 60, 0.0F, 90.0F)
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
Graphics.FillPolygon
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class FillPolygon
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 = e.Graphics
" Create a solid brush
Dim greenBrush As New SolidBrush(Color.Green)
" Create points for polygon.
Dim p1 As New PointF(40.0F, 50.0F)
Dim p2 As New PointF(60.0F, 70.0F)
Dim p3 As New PointF(80.0F, 34.0F)
Dim p4 As New PointF(120.0F, 180.0F)
Dim p5 As New PointF(200.0F, 150.0F)
Dim ptsArray As PointF() = {p1, p2, p3, p4, p5}
" Draw polygon
e.Graphics.FillPolygon(greenBrush, ptsArray)
" Dispose
greenBrush.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
Graphics.FillRectangle
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class FillRectangle
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 = e.Graphics
Dim blueBrush As New SolidBrush(Color.Green)
Dim redBrush As New SolidBrush(Color.Green)
Dim rect As New Rectangle(10, 20, 50, 50)
Dim rectArray As RectangleF() = {New RectangleF(0.0F, 0.0F, 60.0F, 60.0F), New RectangleF(100.0F, 100.0F, 150.0F, 60.0F), New RectangleF(200.0F, 80.0F, 230.0F, 100.0F), New RectangleF(40.0F, 32.0F, 20.0F, 200.0F)}
g.FillRectangle(New HatchBrush(HatchStyle.BackwardDiagonal, Color.Yellow, Color.Black), rect)
e.Graphics.FillRectangle(blueBrush, rect)
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
Graphics.FillRectangles
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class FillFillRectangles
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 = e.Graphics
Dim blueBrush As New SolidBrush(Color.Green)
Dim redBrush As New SolidBrush(Color.Green)
Dim rect As New Rectangle(10, 20, 50, 50)
Dim rectArray As RectangleF() = {New RectangleF(0.0F, 0.0F, 60.0F, 60.0F), New RectangleF(100.0F, 100.0F, 150.0F, 60.0F), New RectangleF(200.0F, 80.0F, 230.0F, 100.0F), New RectangleF(40.0F, 32.0F, 20.0F, 200.0F)}
g.FillRectangle(New HatchBrush(HatchStyle.BackwardDiagonal, Color.Yellow, Color.Black), rect)
e.Graphics.FillRectangles(redBrush, rectArray)
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
Graphics.IntersectClip
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class IntersectClip
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 = Graphics.FromHwnd(Me.Handle)
g.Clear(Me.BackColor)
Dim blackPen As New Pen(Color.Black, 2)
Dim redPen As New Pen(Color.Red, 2)
Dim greenPen As New Pen(Color.Green, 2)
Dim yellowPen As New Pen(Color.Yellow, 2)
Dim yelgreenPen As New Pen(Color.YellowGreen)
Dim rect1 As New Rectangle(0, 0, 50, 50)
Dim rect2 As New Rectangle(50, 50, 100, 100)
Dim region1 As New [Region](rect1)
Dim region2 As New [Region](rect2)
g.SetClip(region1, System.Drawing.Drawing2D.rubineMode.Replace)
Dim intRect1 As New Rectangle(25, 25, 75, 75)
Dim intRect2 As New Rectangle(100, 100, 150, 150)
Dim intReg1 As New [Region](intRect1)
Dim intReg2 As New [Region](intRect2)
g.IntersectClip(intReg1)
g.FillRectangle(New SolidBrush(Color.Blue), 0, 0, 125, 125)
g.FillRectangle(New SolidBrush(Color.Blue), 50, 50, 175, 175)
g.ResetClip()
g.DrawRectangle(yellowPen, rect1)
g.DrawRectangle(greenPen, intRect1)
g.DrawRectangle(blackPen, rect2)
g.DrawRectangle(redPen, intRect2)
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
Graphics.MeasureString
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Imports System.Math
public class MeasureString
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 the_font As New Font("Times New Roman", 40, FontStyle.Bold, GraphicsUnit.Pixel)
Dim the_string As String = "MeasureString"
Dim string_size As SizeF = e.Graphics.MeasureString("MeasureString", the_font)
Dim x As Integer = (Me.ClientSize.Width - CInt(string_size.Width)) \ 2
Dim y As Integer = (Me.ClientSize.Height - CInt(string_size.Height)) \ 2
e.Graphics.DrawString(the_string, the_font, Brushes.Black, x, y)
Dim string_rect As New Rectangle(x, y, CInt(string_size.Width), CInt(string_size.Height))
e.Graphics.DrawRectangle(Pens.Black, string_rect)
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
Graphics.ReleaseHdc
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class ReleaseHdc
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 hdc As IntPtr = e.Graphics.GetHdc()
Dim newGraphics As Graphics = Graphics.FromHdc(hdc)
newGraphics.DrawRectangle(New Pen(Color.Red, 3), 0, 0, 200, 100)
e.Graphics.ReleaseHdc(hdc)
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
Graphics.ResetClip
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class ResetClip
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 = Graphics.FromHwnd(Me.Handle)
g.Clear(Me.BackColor)
Dim clipRect As New Rectangle(0, 0, 200, 200)
g.SetClip(clipRect)
Dim intersectRectF As New RectangleF(100.0F, 100.0F, 200.0F, 200.0F)
g.IntersectClip(intersectRectF)
g.FillRectangle(New SolidBrush(Color.Blue), 0, 0, 500, 500)
g.ResetClip()
g.DrawRectangle(New Pen(Color.Black), clipRect)
g.DrawRectangle(New Pen(Color.Red), Rectangle.Round(intersectRectF))
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
Graphics.SetClip
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class SetClip
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 Rectangle(20, 20, 200, 200)
Dim rect2 As New Rectangle(100, 100, 200, 200)
Dim rgn1 As New [Region](rect1)
Dim rgn2 As New [Region](rect2)
g.SetClip(rgn1, CombineMode.Exclude)
g.IntersectClip(rgn2)
g.FillRectangle(Brushes.Red, 0, 0, 300, 300)
g.ResetClip()
g.DrawRectangle(Pens.Green, rect1)
g.DrawRectangle(Pens.Yellow, rect2)
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
Use Graphics.DrawEllipse to draw a circle
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class DrawEllipseCircle
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)
" Drawing a Circle
Dim g As Graphics = e.Graphics
Dim pn As New Pen(Color.Blue, 100)
Dim rect As New Rectangle(50, 50, 100, 100)
g.DrawEllipse(pn, rect)
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