VB.Net Tutorial/2D Graphics/TextureBrush — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 12:54, 26 мая 2010
Содержание
Create TextureBrush from Bitmap
Imports System
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Collections
Imports System.ruponentModel
Imports System.Windows.Forms
Imports System.Data
public class TextureBrushCreateFromBitmap
public Shared Sub Main
Application.Run(New BrushesForm)
End Sub
End class
Public Class BrushesForm
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()
"
"BrushesForm
"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Name = "BrushesForm"
Me.Text = "Form1"
End Sub
#End Region
Enum LinearGradientMode
BackwardDiagonal
ForwardDiagonal
Horizontal
Vertical
End Enum
Private Sub BrushesForm_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint
Dim g As Graphics = e.Graphics
Dim width As Integer = Me.ClientRectangle.Width
Dim height As Integer = Me.ClientRectangle.Height / 5
Dim whiteBrush As Brush = System.Drawing.Brushes.White
Dim blackBrush As Brush = System.Drawing.Brushes.Black
Dim b As Brush
Dim file As String = "YourFile.jpg"
b = New TextureBrush(New Bitmap(file))
g.FillRectangle(b, 0, 0, width, height)
g.DrawString(b.ToString(), Me.Font, whiteBrush, 0, 0)
End Sub
End Class
Create textured brush and display textured rectangle
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class TexturePaintDemo
public Shared Sub Main
Application.Run(New FrmDrawShapes)
End Sub
End class
Public Class FrmDrawShapes
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.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()
"
"frmDrawShapes
"
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(520, 197)
Me.Name = "frmDrawShapes"
Me.Text = "Drawing Shapes"
End Sub
#End Region
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Dim graphicsObject As Graphics = e.Graphics
Dim textureBitmap As Bitmap = New Bitmap(10, 10)
Dim graphicsObject2 As Graphics = Graphics.FromImage(textureBitmap) " get bitmap graphics
" brush and pen used throughout program
Dim solidColorBrush As SolidBrush = New SolidBrush(Color.Red)
Dim coloredPen As Pen = New Pen(solidColorBrush)
solidColorBrush.Color = Color.Yellow
graphicsObject2.FillRectangle(solidColorBrush, 0, 0, 10, 10)
coloredPen.Color = Color.Black
graphicsObject2.DrawRectangle(coloredPen, 1, 1, 6, 6)
solidColorBrush.Color = Color.Blue
graphicsObject2.FillRectangle(solidColorBrush, 1, 1, 3, 3)
solidColorBrush.Color = Color.Red
graphicsObject2.FillRectangle(solidColorBrush, 4, 4, 3, 3)
Dim texturedBrush As TextureBrush = _
New TextureBrush(textureBitmap)
graphicsObject.FillRectangle(texturedBrush, 155, 30, 75, 100)
End Sub
End Class
Generate points to draw a star shape
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Imports System.Math
public class DrawAStarShape
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 cx As Integer = Me.ClientSize.Width \ 2
Dim cy As Integer = Me.ClientSize.Height \ 2
Dim r1 As Integer = Min(cx, cy) - 10
Dim r2 As Integer = Min(cx, cy) \ 2
Dim pts(9) As Point
For i As Integer = 0 To 9 Step 2
pts(i).X = cx + CInt(r1 * Cos(i * PI / 5 - PI / 2))
pts(i).Y = cy + CInt(r1 * Sin(i * PI / 5 - PI / 2))
pts(i + 1).X = cx + CInt(r2 * Cos((i + 1) * PI / 5 - PI / 2))
pts(i + 1).Y = cy + CInt(r2 * Sin((i + 1) * PI / 5 - PI / 2))
Next i
Dim texture_brush As New TextureBrush(new Bitmap("yourfile.jpg"))
e.Graphics.FillPolygon(texture_brush, pts)
e.Graphics.DrawPolygon(Pens.Black, pts)
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
Transform and Rotate the TextureBrush
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Imports System.Math
public class TextureBrushTransformAndRotate
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 cx As Integer = Me.ClientSize.Width \ 2
Dim cy As Integer = Me.ClientSize.Height \ 2
Dim r1 As Integer = Min(cx, cy) - 10
Dim r2 As Integer = Min(cx, cy) \ 2
Dim pts(9) As Point
For i As Integer = 0 To 9 Step 2
pts(i).X = cx + CInt(r1 * Cos(i * PI / 5 - PI / 2))
pts(i).Y = cy + CInt(r1 * Sin(i * PI / 5 - PI / 2))
pts(i + 1).X = cx + CInt(r2 * Cos((i + 1) * PI / 5 - PI / 2))
pts(i + 1).Y = cy + CInt(r2 * Sin((i + 1) * PI / 5 - PI / 2))
Next i
Dim texture_brush As New TextureBrush(new Bitmap("yourfile.jpg"))
texture_brush.ScaleTransform(20, 10, MatrixOrder.Append)
texture_brush.RotateTransform(30, MatrixOrder.Append)
e.Graphics.FillPolygon(texture_brush, pts)
e.Graphics.DrawPolygon(Pens.Black, pts)
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
Transform TextureBrush
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class TransformTextureBrush
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)
" Create a TextureBrush object
Dim txtrBrush As New TextureBrush(New Bitmap("yourfile.jpg"))
" Create a transformation matrix.
Dim M As New Matrix
" Rotate the texture image by 90 degrees.
txtrBrush.RotateTransform(90, MatrixOrder.Prepend)
" Translate
M.Translate(50, 0)
" Multiply the transformation matrix
" of tBrush by translateMatrix.
txtrBrush.MultiplyTransform(M)
" Scale operation
txtrBrush.ScaleTransform(2, 1, MatrixOrder.Prepend)
" Fill a rectangle with texture brush
g.FillRectangle(txtrBrush, 240, 0, 200, 200)
" Reset transformation
txtrBrush.ResetTransform()
" Fill rectangle after reseting transformation
g.FillRectangle(txtrBrush, 0, 0, 200, 200)
" Dispose
txtrBrush.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