VB.Net Tutorial/2D Graphics/Draw string

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

Draw string with Brush and Font settings

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class GDIObjectsCreateBrushFont

  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 brush1 As New SolidBrush(Color.DarkBlue)
       Dim font1 As New Font("Arial", 24, FontStyle.Bold Or FontStyle.Italic)
       canvas.DrawString("www.vbex.ru",font1, brush1, 120, 70)
       
       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</source>

Print string in 3D

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class TextureTransform

  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 str As String = "www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru www.vbex.ru "
       g.ScaleTransform(2, 1)
       g.RotateTransform(45.0F, System.Drawing.Drawing2D.MatrixOrder.Prepend)
       g.TranslateTransform(-20, -70)
       g.DrawString(str, New Font("Verdana", 10), New SolidBrush(Color.Blue), New RectangleF(50, 20, 200, 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</source>

Rotate and Transoform in Text painting

<source lang="vbnet">Imports System.Drawing.Text Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class TransformRotateTextPaint

  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)
       Me.Font = New Font("Times New Roman", 20, FontStyle.Bold, GraphicsUnit.Pixel)
       e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliasGridFit
       DrawArrow(e.Graphics, "")
       e.Graphics.TranslateTransform(150, 50, MatrixOrder.Append)
       DrawArrow(e.Graphics, "Translate 150 horizontally and 50 vertically")
       e.Graphics.RotateTransform(45, MatrixOrder.Append)
       DrawArrow(e.Graphics, "Rotate 45 degrees")
       " Reset the transformation.
       e.Graphics.ResetTransform()
       e.Graphics.RotateTransform(45, MatrixOrder.Append)
       DrawArrow(e.Graphics, "Rotate 45 degrees after reset")
       e.Graphics.TranslateTransform(150, 50, MatrixOrder.Append)
       DrawArrow(e.Graphics, "Translate 150 horizontally and 50 vertically after reset")
   End Sub
   " Draw an arrow outline containing some text.
   Private Sub DrawArrow(ByVal gr As Graphics, ByVal txt As String)
       " Draw the text.
       Dim layout_rectangle As New RectangleF(10, 20, 600, 20)
       Dim string_format As New StringFormat
       string_format.LineAlignment = StringAlignment.Center
       string_format.Alignment = StringAlignment.Center
       gr.DrawString(txt, Me.Font, Brushes.Black, _
           layout_rectangle, string_format)
   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</source>

Text Contrast

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class TextContrast

  public Shared Sub Main
       Application.Run(New TextContrastForm)
  End Sub

End class Public Class TextContrastForm

   Inherits System.Windows.Forms.Form
  1. 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()
       "
       "TextContrastForm
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.BackColor = System.Drawing.Color.White
       Me.ClientSize = New System.Drawing.Size(292, 266)
       Me.Name = "TextContrastForm"
       Me.Text = "TextContrastForm"
   End Sub
  1. End Region
   Private Sub TextContrastForm_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
       Dim g As Graphics = e.Graphics
       
       Dim i As Integer
       Dim y As Integer
       For i = 0 To 12 Step 1
           g.TextContrast = i
           Dim line As String = "Contrast = " & i.ToString()
           g.DrawString(line, Me.Font, Brushes.Black, 0, y, New StringFormat())
           y = y + 30
       Next
   End Sub

End Class</source>

Text outline

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class StringOutLine

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

End class Public Class Form1

   Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
       Dim outlinePath As New Drawing2D.GraphicsPath
       Dim useFont As Font = New Font("Times New Roman", 96, FontStyle.Regular)
       e.Graphics.TextRenderingHint = Drawing.Text.TextRenderingHint.AntiAlias
       e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
       outlinePath.AddString("www.vbex.ru", useFont.FontFamily, FontStyle.Regular, 96, New Point(0, 0), StringFormat.GenericTypographic)
       useFont.Dispose()
       e.Graphics.FillPath(Brushes.LightGray, outlinePath)
       e.Graphics.TranslateTransform(-5, -5)
       e.Graphics.FillPath(Brushes.White, outlinePath)
       e.Graphics.DrawPath(Pens.Black, outlinePath)
       outlinePath.Dispose()
   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.PictureBox1 = New System.Windows.Forms.PictureBox
       CType(Me.PictureBox1, System.ruponentModel.ISupportInitialize).BeginInit()
       Me.SuspendLayout()
       "
       "PictureBox1
       "
       Me.PictureBox1.BackColor = System.Drawing.Color.White
       Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
       Me.PictureBox1.Location = New System.Drawing.Point(8, 8)
       Me.PictureBox1.Name = "PictureBox1"
       Me.PictureBox1.Size = New System.Drawing.Size(780, 152)
       Me.PictureBox1.TabIndex = 0
       Me.PictureBox1.TabStop = False
       "
       "Form1
       "
       Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
       Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
       Me.ClientSize = New System.Drawing.Size(800, 166)
       Me.Controls.Add(Me.PictureBox1)
       Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
       Me.MaximizeBox = False
       Me.Name = "Form1"
       Me.Text = "Outline Text"
       CType(Me.PictureBox1, System.ruponentModel.ISupportInitialize).EndInit()
       Me.ResumeLayout(False)
   End Sub
   Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox

End Class</source>

Text Shadow

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class TextShadow

  public Shared Sub Main
       Application.Run(New ShadowFontsForm)
  End Sub

End class Public Class ShadowFontsForm

   Inherits System.Windows.Forms.Form
  1. 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()
       "
       "ShadowFontsForm
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(46, 109)
       Me.ClientSize = New System.Drawing.Size(800, 266)
       Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 72.0!)
   End Sub
  1. End Region
   Private Function GetStringPath(ByVal s As String, ByVal dpi As Single, ByVal rect As RectangleF, ByVal font As Font, ByVal format As StringFormat) As GraphicsPath
       Dim path As GraphicsPath = New GraphicsPath()
       Dim emSize As Single = dpi * font.SizeInPoints / 72
       path.AddString(s, font.FontFamily, CInt(font.Style), emSize, rect, format)
       Return path
   End Function
   Private Sub ShadowFontsForm_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
       Dim g As Graphics = e.Graphics
       Dim s As String = "www.vbex.ru"
       
       Dim size As SizeF = New SizeF(Me.ClientRectangle.Width - 4, Me.ClientRectangle.Height - 4)
       
       Dim rect As RectangleF = New RectangleF(0, 0, size.Width, size.Height)
       Dim pathShadow As GraphicsPath = GetStringPath(s, g.DpiY, New RectangleF(4, 4, size.Width, size.Height), Me.Font, StringFormat.GenericTypographic)
       Dim path As GraphicsPath = GetStringPath(s, g.DpiY, rect, Me.Font, StringFormat.GenericTypographic)
       g.FillPath(Brushes.Black, pathShadow)
       g.FillPath(Brushes.Red, path)
   End Sub

End Class</source>

Transform Text

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms Imports System.Math public class TransformedText

  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 = "The quick brown fox jumps over the lazy dog."
       Dim the_font As New Font("Times New Roman", 30, FontStyle.Bold, GraphicsUnit.Pixel)
       Dim layout_rect As New RectangleF( 10, 10, 200, 200)
       Dim string_format As New StringFormat
       string_format.Alignment = StringAlignment.Center
       string_format.LineAlignment = StringAlignment.Center
       e.Graphics.TranslateTransform(-100, -100, MatrixOrder.Append)
       e.Graphics.RotateTransform(60, MatrixOrder.Append)
       e.Graphics.TranslateTransform(100,100, MatrixOrder.Append)
       e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit
       e.Graphics.DrawString(txt, the_font, Brushes.Black, layout_rect, string_format)
       e.Graphics.DrawRectangle(Pens.Black, Rectangle.Round(layout_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</source>

Transparent string

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class AlphaBlending

  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 curImage As Image = Image.FromFile("yourfile.jpg")
       g.DrawImage(curImage, 0, 0, curImage.Width, curImage.Height) "
       Dim opqPen As New Pen(Color.FromArgb(255, 0, 255, 0), 10)
       Dim transPen As New Pen(Color.FromArgb(128, 0, 255, 0), 10)
       Dim totTransPen As New Pen(Color.FromArgb(40, 0, 255, 0), 10)
       g.DrawLine(opqPen, 10, 10, 200, 10)
       g.DrawLine(transPen, 10, 30, 200, 30)
       g.DrawLine(totTransPen, 10, 50, 200, 50)
       Dim semiTransBrush As New SolidBrush(Color.FromArgb(90, 255, 255, 0))
       g.DrawString("Photo " + ControlChars.Lf + "Date: 04/09/2001", New Font("Verdana", 14), semiTransBrush, New RectangleF(20, 100, 300, 100))
       " 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</source>

Use LinearGradientBrush to draw string

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class ColorBlending

  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 rgBrush As New LinearGradientBrush(New RectangleF(20, 20, 100, 100), Color.Red, Color.Green, LinearGradientMode.Horizontal)
       Dim yrBrush As New LinearGradientBrush(New RectangleF(140, 20, 150, 300), Color.Yellow, Color.Red, LinearGradientMode.ForwardDiagonal)
       Dim rbBrush As New LinearGradientBrush(New RectangleF(10, 10, 50, 50), Color.Red, Color.Blue, LinearGradientMode.ForwardDiagonal)
       g.FillRectangle(rgBrush, 20, 20, 100, 100)
       g.FillEllipse(yrBrush, 140, 20, 200, 200)
       g.DrawString("Color Blending", New Font("Tahoma", 30), rbBrush, New RectangleF(100, 240, 300, 300))
       " 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</source>