Option Strict On Public Class Form1 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 Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents bttnBoxedText As System.Windows.Forms.Button Friend WithEvents bttnClippedText As System.Windows.Forms.Button Friend WithEvents bttnClippedImage As System.Windows.Forms.Button Friend WithEvents bttnClear As System.Windows.Forms.Button 'Required by the Windows Form Designer Private components As System.ComponentModel.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. Private Sub InitializeComponent() Me.bttnClippedImage = New System.Windows.Forms.Button() Me.bttnClippedText = New System.Windows.Forms.Button() Me.bttnClear = New System.Windows.Forms.Button() Me.bttnBoxedText = New System.Windows.Forms.Button() Me.PictureBox1 = New System.Windows.Forms.PictureBox() Me.Button1 = New System.Windows.Forms.Button() Me.SuspendLayout() ' 'bttnClippedImage ' Me.bttnClippedImage.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.bttnClippedImage.Location = New System.Drawing.Point(456, 98) Me.bttnClippedImage.Name = "bttnClippedImage" Me.bttnClippedImage.Size = New System.Drawing.Size(112, 32) Me.bttnClippedImage.TabIndex = 4 Me.bttnClippedImage.Text = "Clipped Image" ' 'bttnClippedText ' Me.bttnClippedText.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.bttnClippedText.Location = New System.Drawing.Point(456, 53) Me.bttnClippedText.Name = "bttnClippedText" Me.bttnClippedText.Size = New System.Drawing.Size(112, 32) Me.bttnClippedText.TabIndex = 3 Me.bttnClippedText.Text = "Clipped Text" ' 'bttnClear ' Me.bttnClear.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.bttnClear.Location = New System.Drawing.Point(456, 143) Me.bttnClear.Name = "bttnClear" Me.bttnClear.Size = New System.Drawing.Size(112, 32) Me.bttnClear.TabIndex = 4 Me.bttnClear.Text = "Clear Control" ' 'bttnBoxedText ' Me.bttnBoxedText.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.bttnBoxedText.Location = New System.Drawing.Point(456, 8) Me.bttnBoxedText.Name = "bttnBoxedText" Me.bttnBoxedText.Size = New System.Drawing.Size(112, 32) Me.bttnBoxedText.TabIndex = 2 Me.bttnBoxedText.Text = "Boxed Text" ' 'PictureBox1 ' 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(440, 408) Me.PictureBox1.TabIndex = 0 Me.PictureBox1.TabStop = False ' 'Button1 ' Me.Button1.Font = New System.Drawing.Font("Verdana", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Button1.Location = New System.Drawing.Point(456, 384) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(112, 32) Me.Button1.TabIndex = 4 Me.Button1.Text = "E X I T" ' 'Form1 ' Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(576, 421) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.bttnClear, Me.Button1, Me.bttnClippedImage, Me.bttnClippedText, Me.bttnBoxedText, Me.PictureBox1}) Me.Name = "Form1" Me.Text = "Clipping Text and Images" Me.ResumeLayout(False) End Sub #End Region Dim txt As String = "This string will be printed " & _ "in orange color and it will be clipped " & _ "within an ellipse that extents 120 pixels " & _ "horizontally and 120 pixels vertically. " Private Sub bttnBoxedText_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnBoxedText.Click Dim G As Graphics G = GetGraphicsObject() Dim Rect As New RectangleF(30, 30, 250, 150) G.ResetTransform() G.ResetClip() Dim format As StringFormat = New StringFormat() format.Alignment = StringAlignment.Center G.DrawString(txt & txt, New Font("Verdana", 11, FontStyle.Regular), Brushes.Coral, Rect, format) PictureBox1.Invalidate() End Sub Private Sub bttnClippedText_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnClippedText.Click Dim G As Graphics G = GetGraphicsObject() Dim P As New System.Drawing.Drawing2D.GraphicsPath() Dim clipRect As New RectangleF(30, 30, 250, 150) P.AddEllipse(clipRect) G.ResetTransform() G.DrawEllipse(Pens.Red, clipRect) G.SetClip(P) Dim format As StringFormat = New StringFormat() format.Alignment = StringAlignment.Center G.DrawString(txt & txt, New Font("Verdana", 11, FontStyle.Regular), Brushes.Coral, clipRect, format) PictureBox1.Invalidate() End Sub Private Sub bttnClippedImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnClippedImage.Click Dim G As Graphics G = GetGraphicsObject() G.ResetTransform() G.ResetClip() G.TranslateTransform(200, 200) Dim P As New System.Drawing.Drawing2D.GraphicsPath() Dim clipRect As New RectangleF(-150, -150, 320, 260) P.AddEllipse(clipRect) G.SetClip(P) Dim path As String ' The StartupPath is the Bin folder under the project's folder ' the following statement removes the "\bin" part of the path ' the rest of the string is the path to the application's folder, ' where the image "SEATTLE.JPG" resides path = Application.StartupPath.Remove(Application.StartupPath.Length - 4, 4) G.DrawImage(Image.FromFile(path & "\seattle.jpg"), -150, -150) PictureBox1.Invalidate() End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click End End Sub Private Sub bttnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bttnClear.Click Dim G As Graphics G = GetGraphicsObject() G.ResetClip() G.Clear(PictureBox1.BackColor) PictureBox1.Invalidate() End Sub Function GetGraphicsObject() As Graphics Dim G As Graphics Dim bmp As System.Drawing.Bitmap bmp = New System.Drawing.Bitmap(PictureBox1.Width, PictureBox1.Height) PictureBox1.Image = bmp Return Graphics.FromImage(bmp) End Function End Class