Download presentation
Presentation is loading. Please wait.
Published byKatrina Brown Modified over 8 years ago
1
Chapter 16 – Graphics and Multimedia
2
Outline 16.1Introduction 16.2 Graphics Contexts and Graphics Objects 16.3 Color Control 16.4 Font Control 16.5 Drawing Lines, Rectangles and Ovals 16.6 Drawing Arcs 16.7 Drawing Polygons and Polylines 16.8 Advanced Graphics Capabilities 16.9 Introduction to Multimedia 16.10 Loading, Displaying and Scaling Images 16.11 Animating a Series of Images 16.12 Windows Media Player 16.13 Microsoft Agent
3
The language contains many sophisticated drawing capabilities as part of namespace System.Drawing and the other namespaces that make up the.NET resource GDI+. GDI+, an extension of the Graphical Device Interface, is an application programming interface (API) that provides classes for 2D drawing. The most commonly used components of GDI+ reside in the System.Drawing and System.Drawing.Drawing2D namespaces. 16.1Introduction
4
Fig. 16.1 System.Drawing namespace’s Classes and Structures.
5
Class Graphics contains methods used for drawing Strings, lines, rectangles and other shapes on a Control. The drawing methods of class Graphics usually require a Pen or Brush object to render a specified shape. The Pen draws shape outlines; the Brush draws solid objects 16.1Introduction
6
Structure Color has: – properties to set color of various graphical components. – Methods to create new colors. Class Font has: – Properties to define unique fonts Class FontFamily has – Methods for obtaining font information. 16.1Introduction
7
To begin drawing in Visual Basic, we first must understand GDI+’s coordinate system: – Upper-left corner of component has coordinates (0, 0) – Coordinate pairs: Allow positioning of text and shapes Horizontal coordinate (x-coordinate) – Distance to the right from upper-left corner Vertical coordinate (y-coordinate) – Distance down from upper-left corner – Coordinate units measured in pixels – Used with structures Rectangle and Point that are provided by System.Drawing namespace
8
16.1Introduction x-axis y-axis (x, y) +x+x +y+y (0, 0) Rectangle structure defines rectangular shapes with ( width & height ) dimension. Point structure represents the x-y coordinates of a point on a two-dimensional plane.
9
Graphics objects contain methods for drawing, font manipulation, color manipulation and other graphics-related actions. Every Windows application that derives from class System.Windows.Forms.Form inherits an Overridable OnPaint method where most graphics operations are performed. The arguments to the OnPaint method include a PaintEventArgs object from which we can obtain a Graphics object for the control. The OnPaint method triggers the Control’s Paint event. 16.2 Graphics Contexts and Graphics Objects
10
When displaying graphical information on a control, we need to: 1.Access Graphics object of the control. 2.Then, use Graphics object to draw shapes and strings on the control. Graphics object Can be accessed in 2 ways: 1.By overriding OnPaint() method to retrieve a Graphics object from argument PaintEventArgs 2.Create a new Graphics object associated with the appropriate surface. 16.2 Graphics Contexts and Graphics Objects
11
1-Overriding OnPaint() : Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) ‘ extract the Graphics object from the PaintEventArgs argument: Dim graphicsObject As Graphics = e.Graphics Calling the OnPaint method raises the Paint event. Instead of overriding the OnPaint method, programmers can add an event handler for the Paint event because OnPaint method is rarely called directly. Public Sub MyEventHandler_Paint( _ ByVal sender As Object, ByVal e As PaintEventArgs) _ Handles Me.Paint 16.2 Graphics Contexts and Graphics Objects
12
OnPaint is called automatically by system when events occur such as moving or resizing of windows. Similarly, when controls( such as Label or Button) are displayed the program calls that controls paint method. Programmers can invoke OnPaint explicitly by calling Invalidate method. This method refreshes a control’s client area and repaints all graphical components. 16.2 Graphics Contexts and Graphics Objects
13
2-Creating a new Graphics: By invoking CreateGraphics method. Dim graphicsObject As Graphics = label1.CreaeGraphics() ‘ Then, you can use the methods provided in class Graphics to draw on the control for example we can draw a circle on label1 graphicsObjects.DrawCircle(……) 16.2 Graphics Contexts and Graphics Objects
14
16.3 Color Control Structure Color – ARGB values Alpha, red, green and blue values, respectively Each value represented as a Byte Alpha value determines intensity – 0 = transparent, 255 = opaque color. The first number in the RGB value defines the amount of red in the color, the second defines the amount of green and the third defines the amount of blue. The larger the value, the greater the amount of that particular color.
15
16.3 Color Control
16
The overloaded version takes four arguments and allows the user to specify alpha; the three-argument version defaults the alpha to 255.
17
16.3 Color Control Programmers draw shapes and Strings using Brushes and Pens. Pen objects – functions similarly to an ordinary pen, is used to draw lines. – constructors allow programmers to specify the colors and widths of the lines that they wish to draw. – Pens collection ( System.Drawing ) contains predefined Pen s. Brush objects – Used to color interiors of shapes – In most Fill methods, Brushes fill a space with a color, pattern or image. – Upcoming example: Color value and alpha demonstration
18
16.3 Color Control
19
ShowColors.vb 1 ' Fig. 16.6: ShowColors.vb 2 ' Using different colors in Visual Basic. 3 4 Public Class FrmColorForm 5 Inherits System.Windows.Forms.Form 6 30 ' color for back rectangle 31 Private mBehindColor As Color = Color.Wheat 32 33 ' color for front rectangle 34 Private mFrontColor As Color = Color.FromArgb(100, 0, 0, 255) 35 ' overrides Form OnPaint method 37 Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) 38 Dim graphicsObject As Graphics = e.Graphics ' get graphics 39 40 Dim textBrush As SolidBrush = New SolidBrush(Color.Black) ' create text brush 42 43 Dim brush As SolidBrush = New SolidBrush(Color.White) ' create solid brush 45 46 ' draw white background 47 graphicsObject.FillRectangle(brush, 4, 4, 275, 180) 48 49 ' display name of behindColor 50 graphicsObject.DrawString(mBehindColor.Name, Me.Font, textBrush, 40, 5) 52 53 ' set brush color and display back rectangle 54 brush.Color = mBehindColor 55 56 graphicsObject.FillRectangle(brush, 45, 20, 150, 120) 57 Graphics method FillRectangle draws a solid white rectangle with the Brush supplied as a parameter. Gets a reference to PaintEventArgs e ’s Graphics object and assigns it to Graphics object graphicsObject Creates a black and white SolidBrush for drawing on the form Assigns the ColormBehindColor value to the Brush ’s Color property and displays a rectangle When the application begins its execution, it calls class ShowColors’ OnPaint method to paint the window.
20
58 ' display Argb values of front color 59 graphicsObject.DrawString("Alpha: " & mFrontColor.A & _ 60 " Red: " & mFrontColor.R & " Green: " & mFrontColor.G _ 61 & " Blue: " & mFrontColor.B, Me.Font, textBrush, _ 62 55, 165) 63 64 ' set brush color and display front rectangle 65 brush.Color = mFrontColor 66 67 graphicsObject.FillRectangle(brush, 65, 35, 170, 130) 68 End Sub ' OnPaint 69 70 ' handle cmdColorValue click event 71 Private Sub cmdColorValue_Click(ByVal sender As _ 72 System.Object, ByVal e As System.EventArgs) _ 73 Handles cmdColorValue.Click 74 75 ' obtain new front color from text boxes 76 mFrontColor = Color.FromArgb(txtAlphaBox.Text, _ 77 txtRedBox.Text, txtGreenBox.Text, txtBlueBox.Text) 78 79 Invalidate() ' refresh Form 80 End Sub ' cmdColorValue_Click
21
81 82 Private Sub cmdColorName_Click(ByVal sender As _ 83 System.Object, ByVal e As System.EventArgs) _ 84 Handles cmdColorName.Click 85 86 ' set behindColor to color specified in text box 87 mBehindColor = Color.FromName(txtColorName.Text) 88 89 Invalidate() ' refresh Form 90 End Sub ' cmdColorName_Click 91 92End Class ' FrmColorForm
23
1 ' Fig. 16.7: ShowColorsComplex.vb 2 ' Change the background and text colors of a form. 3 4 Imports System.Windows.Forms 5 6 Public Class FrmColorDialogTest 7 Inherits System.Windows.Forms.Form 13 14 ' change text color 15 Private Sub cmdTextButton_Click (ByVal sender As System.Object, _ 16 ByVal e As System.EventArgs) Handles cmdTextButton.Click 17 18 ' create ColorDialog object 19 Dim colorBox As ColorDialog = New ColorDialog() 20 Dim result As DialogResult 21 22 ' get chosen color 23 result = colorBox.ShowDialog() 24 25 If result = DialogResult.Cancel Then 26 Return 27 End If 28 29 ' assign forecolor to result of dialog 30 cmdBackgroundButton.ForeColor = colorBox.Color 31 cmdTextButton.ForeColor = colorBox.Color 32 End Sub ' cmdTextButton_Click 33 Creates new ColorDialog named colorBox and invokes ShowDialog method to display the window Sets the text color of both buttons to the selected color
24
34 ' change background color 35 Private Sub cmdBackgroundButton_Click( _ 36 ByVal sender As System.Object, _ 37 ByVal e As System.EventArgs) _ 38 Handles cmdBackgroundButton.Click 39 40 ' create ColorDialog object 41 Dim colorBox As ColorDialog = New ColorDialog() 42 Dim result As DialogResult 43 44 ' show ColorDialog and get result 45 colorBox.FullOpen = True 46 result = colorBox.ShowDialog() 47 48 If result = DialogResult.Cancel Then 49 Return 50 End If 51 52 ' set background color 53 Me.BackColor = colorBox.Color 54 End Sub ' cmdBackgroundButton_Click 55 56 End Class ' FrmColorDialogTest Sets the dialog’s FullOpen property to True
26
16.4 Font Control Font s – After a Font is created, its properties cannot be modified – Programmers must create a new Font object to be different Font constructors – Must require a font name as an argument – They usually require the font size as an argument – And usually require the font style which is a member of the FontStyle enumeration: Bold, Italic, Regular, Strikeout, Underline. Graphics method DrawString sets the current drawing font— the font in which the text displays—to its Font argument.
27
DrawString constructors: – takes a String to display, – the display Font, – a Brush – and the x- and y-coordinates of the location for the String’s first character. 16.4 Font Control
28
GraphicsUnit argument—an enumeration that allows users to specify the unit of measurement employed to describe the font size. Members of the GraphicsUnit enumeration include Point (1/72 inch), Display (1/75 inch), Document (1/300 inch), Millimeter, Inch and Pixel.
29
1 ' Fig. 16.9: UsingFonts.vb 2 ' Demonstrating various font settings. 3 4 Public Class FrmFonts 5 Inherits System.Windows.Forms.Form 6 9 ' demonstrate various font and style settings 10 Protected Overrides Sub OnPaint( ByVal paintEvent As PaintEventArgs) 12 13 Dim graphicsObject As Graphics = paintEvent.Graphics 14 Dim brush As SolidBrush = New SolidBrush(Color.DarkBlue) 15 16 ' arial, 12 pt bold 17 Dim style As FontStyle = FontStyle.Bold 18 Dim arial As Font = New Font( New FontFamily("Arial"), 12, style) 20 21 ' times new roman, 12 pt regular 22 style = FontStyle.Regular 23 Dim timesNewRoman As Font = New Font( "Times New Roman", 12, style) 25 26 ' courier new, 16 pt bold and italic 27 style = FontStyle.Bold Or FontStyle.Italic 28 Dim courierNew As Font = New Font("Courier New", 16, style) 30 Creates a DarkBlue SolidBrush object ( brush ), causing all strings drawn with that brush to appear in DarkBlue
30
31 ' tahoma, 18 pt strikeout 32 style = FontStyle.Strikeout 33 Dim tahoma As Font = New Font("Tahoma", 18, style) 34 35 graphicsObject.DrawString(arial.Name & " 12 point bold.", arial, brush, 10, 10) 37 38 graphicsObject.DrawString(timesNewRoman.Name & _ 39 " 12 point plain.", timesNewRoman, brush, 10, 30) 40 41 graphicsObject.DrawString(courierNew.Name & _ 42 " 16 point bold and italic.", courierNew, brush, 10, 54 ) 43 44 graphicsObject.DrawString(tahoma.Name & _ 45 " 18 point strikeout.", tahoma, brush, 10, 75) 46 End Sub ' OnPaint 47 48 End Class ' FrmFonts
31
16.4 Font Control Programmers can define precise information about a font’s metrics (or properties), such as: height descent (the amount that characters dip below the baseline), ascent (the amount that characters rise above the baseline) leading (the difference between the ascent of one line and the decent of the previous line).
32
16.4 Font Control Class FontFamily provides several methods used to determine the font metrics that are shared by members of a particular family.
33
16.5 Drawing Lines, Rectangles and Ovals Drawing shape outlines – Versions that take a Pen and four Integer s are used Drawing solid shapes – Versions that take a Brush and four Integer s Integer arguments – First two represent the coordinates of the upper-left corner of the shape or its enclosing area – Last two indicate the shape’s width and height
34
16.5 Drawing Lines, Rectangles and Ovals
35
Fig. 16.15Ellipse bounded by a rectangle. height width (x, y)
36
1 ' Fig. 16.14: LinesRectanglesOvals.vb 2 ' Demonstrating lines, rectangles, and ovals. 3 4 Public Class FrmDrawing 5 Inherits System.Windows.Forms.Form 6 7 ' Visual Studio.NET generated code 8 9 ' display ovals lines, and rectangles 10 Protected Overrides Sub OnPaint( _ 11 ByVal paintEvent As PaintEventArgs) 12 13 ' get graphics object 14 Dim g As Graphics = paintEvent.Graphics 15 Dim brush As SolidBrush = New SolidBrush(Color.Blue) 16 Dim pen As Pen = New Pen(Color.AliceBlue) 17 18 ' create filled rectangle 19 g.FillRectangle(brush, 90, 30, 150, 90) 20 21 ' draw lines to connect rectangles 22 g.DrawLine(pen, 90, 30, 110, 40) 23 g.DrawLine(pen, 90, 120, 110, 130) 24 g.DrawLine(pen, 240, 30, 260, 40) 25 g.DrawLine(pen, 240, 120, 260, 130) 26 27 ' draw top rectangle 28 g.DrawRectangle(pen, 110, 40, 150, 90) 29 30 ' set brush to red 31 brush.Color = Color.Red 32 33 ' draw base Ellipse 34 g.FillEllipse(brush, 280, 75, 100, 50) 35
37
36 ' draw connecting lines 37 g.DrawLine(pen, 380, 55, 380, 100) 38 g.DrawLine(pen, 280, 55, 280, 100) 39 40 ' draw Ellipse outline 41 g.DrawEllipse(pen, 280, 30, 100, 50) 42 End Sub ' OnPaint 43 44 End Class ' FrmDrawing
38
16.6 Drawing Arcs Arcs – Arcs are portions of ellipses. – Measured in degrees, beginning at the starting angle and continuing for a specified number of degrees, the arc angle. – An arc is said to sweep (traverse) its arc angle, beginning from its starting angle. – Measuring Sweep in a clockwise direction, measured in positive degrees Sweep in a counterclockwise direction, measured in negative degrees – Graphics methods used to draw arcs DrawArc, DrawPie, and FillPie
39
16.6 Drawing Arcs Fig. 16.16Positive and negative arc angles.
40
16.6 Drawing Arcs
41
1 ' Fig. 16.18: DrawArcs.vb 2 ' Drawing various arcs on a form. 3 4 Public Class FrmArcTest 5 Inherits System.Windows.Forms.Form 8 9 Protected Overrides Sub OnPaint( _ 10 ByVal paintEvent As PaintEventArgs) 11 12 ' get graphics object 13 Dim graphicsObject As Graphics = paintEvent.Graphics 14 Dim rectangle1 As Rectangle = New Rectangle(15, 35, 80, 80) 15 Dim brush1 As SolidBrush = New SolidBrush(Color.FireBrick) 16 Dim pen1 As Pen = New Pen(brush1, 1) 17 Dim brush2 As SolidBrush = New SolidBrush(Color.DarkBlue) 18 Dim pen2 As Pen = New Pen(brush2, 1) 19 20 ' start at 0 and sweep 360 degrees 21 graphicsObject.DrawRectangle(pen1, rectangle1) 22 graphicsObject.DrawArc(pen2, rectangle1, 0, 360) 23 24 ' start at 0 and sweep 110 degrees 25 rectangle1.Location = New Point(100, 35) 26 graphicsObject.DrawRectangle(pen1, rectangle1) 27 graphicsObject.DrawArc(pen2, rectangle1, 0, 110) 28 29 ' start at 0 and sweep -270 degrees 30 rectangle1.Location = New Point(185, 35) 31 graphicsObject.DrawRectangle(pen1, rectangle1) 32 graphicsObject.DrawArc(pen2, rectangle1, 0, -270) 33 Draws a rectangle and then an arc inside the rectangle Changes the location of the Rectangle by setting its Location property to a new Point
42
34 ' start at 0 and sweep 360 degrees 35 rectangle1.Location = New Point(15, 120) 36 rectangle1.Size = New Size(80, 40) 37 graphicsObject.DrawRectangle(pen1, rectangle1) 38 graphicsObject.FillPie(brush2, rectangle1, 0, 360) 39 40 ' start at 270 and sweep -90 degrees 41 rectangle1.Location = New Point(100, 120) 42 graphicsObject.DrawRectangle(pen1, rectangle1) 43 graphicsObject.FillPie(brush2, rectangle1, 270, -90) 44 45 ' start at 0 and sweep -270 degrees 46 rectangle1.Location = New Point(185, 120) 47 graphicsObject.DrawRectangle(pen1, rectangle1) 48 graphicsObject.FillPie(brush2, rectangle1, 0, -270) 49 End Sub ' OnPaint 50 51 End Class ' FrmArcTest Sets the Size property to a new Size object
43
16.7 Drawing Polygons and Polylines Polygons – Multisided shapes – Graphics methods used to draw polygons DrawLines, DrawPolygon, and FillPolygon
44
16.7 Drawing Polygons and Polylines
45
1 ' Fig. 16.20: DrawPolygons.vb 2 ' Demonstrating polygons. 3 4 Public Class FrmPolygon 5 Inherits System.Windows.Forms.Form 6 21 ' contains list of polygon points 22 Private mPoints As ArrayList = New ArrayList() 23 24 ' initialize default pen and brush 25 Dim mPen As Pen = New Pen(Color.DarkBlue) 26 Dim mBrush As SolidBrush = New SolidBrush(Color.DarkBlue) 27 28 ' draw panel mouse down event handler 29 Private Sub drawWindow_MouseDown(ByVal sender _ 30 As Object, ByVal e As _ 31 System.Windows.Forms.MouseEventArgs) _ 32 Handles drawWindow.MouseDown 33 34 ' Add mouse position to vertex list 35 mPoints.Add(New Point(e.X, e.Y)) 36 drawWindow.Invalidate() ' refresh panel 37 End Sub ' drawWindow_MouseDown 38 39 ' draw panel paint event handler 40 Private Sub drawWindow_Paint(ByVal sender As Object, _ 41 ByVal e As System.Windows.Forms.PaintEventArgs) _ 42 Handles drawWindow.Paint 43 44 ' get graphics object for panel 45 Dim graphicsObject As Graphics = e.Graphics Declaring ArrayList mPoints as a container for our Point objects allows the user to specify a variable number of points The MouseDown event handler for Panel drawWindow stores mouse-click locations in the mPoints ArrayList. Calls method Invalidate of drawWindow to ensure that the panel refreshes to accommodate the new point. if the ArrayList mPoints contains two or more Points, displays the polygon using the method that the user selected via the GUI radio buttons
46
46 47 ' if arraylist has 2 or more points, display shape 48 If mPoints.Count > 1 Then 49 50 ' get array for use in drawing functions 51 Dim pointArray() As Point = _ 52 mPoints.ToArray(mPoints(0).GetType()) 53 54 If polygonRadio.Checked Then ' draw polygon 55 graphicsObject.DrawPolygon(mPen, pointArray) 56 57 ElseIf lineRadio.Checked Then ' draw lines 58 graphicsObject.DrawLines(mPen, pointArray) 59 60 ElseIf filledPolygonRadio.Checked Then ' draw filled 61 graphicsObject.FillPolygon(mBrush, pointArray) 62 End If 63 64 End If 65 66 End Sub ' drawWindow_Paint 67 68 ' handle cmdClear click event 69 Private Sub cmdClear_Click(ByVal sender As System.Object, _ 70 ByVal e As System.EventArgs) Handles cmdClear.Click 71 72 mPoints = New ArrayList() ' remove points 73 74 drawWindow.Invalidate() ' refresh panel 75 End Sub ' cmdClear_Click Extracts an Array from the ArrayList via method ToArray Method ToArray can take a single argument to determine the type of the returned array; we obtain the type from the first element in the ArrayList. Creates an empty ArrayList (causing the old list to be erased) and refreshes the display.
47
76 77 ' handle polygon radio button CheckedChange event 78 Private Sub polygonRadio_CheckedChanged(ByVal sender As _ 79 System.Object, ByVal e As System.EventArgs) _ 80 Handles polygonRadio.CheckedChanged 81 82 drawWindow.Invalidate() ' refresh panel 83 End Sub ' polygonRadio_CheckedChanged 84 85 ' handle line radio button CheckChanged event 86 Private Sub lineRadio_CheckedChanged(ByVal sender As _ 87 System.Object, ByVal e As System.EventArgs) _ 88 Handles lineRadio.CheckedChanged 89 90 drawWindow.Invalidate() ' refresh panel 91 End Sub ' lineRadio_CheckedChanged 92 93 ' handle filled polygon radio button CheckChanged event 94 Private Sub filledPolygonRadio_CheckedChanged(ByVal sender _ 95 As System.Object, ByVal e As System.EventArgs) _ 96 Handles filledPolygonRadio.CheckedChanged 97 98 drawWindow.Invalidate() ' refresh panel 99 End Sub ' filledPolygonRadio_CheckedChanged 100 Lines 78–++ define the event handlers for the radio buttons’ CheckedChanged event. Each method refreshes Panel drawWindow to ensure that the panel display reflects the selected drawing type.
48
101 ' handle cmdNewColor click event 102 Private Sub cmdNewColor_Click(ByVal sender As _ 103 System.Object, ByVal e As System.EventArgs) _ 104 Handles cmdNewColor.Click 105 106 ' create new color dialog 107 Dim colorBox As ColorDialog = New ColorDialog() 108 109 ' show dialog and obtain result 110 Dim result As DialogResult = colorBox.ShowDialog() 111 112 ' return if user cancels 113 If result = DialogResult.Cancel Then 114 Return 115 End If 116 117 mPen.Color = colorBox.Color ' set pen to new color 118 mBrush.Color = colorBox.Color ' set brush 119 drawWindow.Invalidate() ' refresh panel 120 End Sub ' cmdNewColor_Click 121 122 End Class ' FrmPolygon Event method cmlNewColor_Click allows the user to select a new drawing color with a ColorDialog
50
16.8 Advanced Graphics Capabilities Visual Basic offers many additional graphics capabilities Examples – Brush hierarchy also includes: HatchBrush, LinearGradientBrush, PathGradientBrush and TextureBrush – Additional graphics features Dashed lines, thick lines, filling shapes with patterns, etc
51
Bitmap Class Produce images in color and gray scale with a particular width and height. Used to work with images defined by pixel data. How to use… Dim graphicsObject As Graphics = e.Graphics Dim BitmapVar As Bitmap = New Bitmap(width, height). Dim BrushVar As TextureBrush = New TextureBrush(BitmapVar) graphicsObject.FillRectangle(BrushVar, X, Y, width, height) 16.8 Advanced Graphics Capabilities
52
DrawShapes.vb 1 ' Fig. 16.21: DrawShapes.vb 2 ' Drawing various shapes on a form. 3 4 Imports System.Drawing.Drawing2D 5 6 Public Class FrmDrawShapes 7 Inherits System.Windows.Forms.Form 8 9 ' Visual Studio.NET generated code 10 11 ' draw various shapes on form 12 Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) 13 14 ' references to object we will use 15 Dim graphicsObject As Graphics = e.Graphics 16 17 ' ellipse rectangle and gradient brush 18 Dim drawArea1 As Rectangle = New Rectangle(5, 35, 30, 100) 19 Dim linearBrush As LinearGradientBrush = _ 20 New LinearGradientBrush(drawArea1, Color.Blue, _ 21 Color.Yellow, LinearGradientMode.ForwardDiagonal) 22 23 ' pen and location for red outline rectangle 24 Dim thickRedPen As Pen = New Pen(Color.Red, 10) 25 Dim drawArea2 As Rectangle = New Rectangle(80, 30, 65, 100) 26 27 ' bitmap texture 28 Dim textureBitmap As Bitmap = New Bitmap(10, 10) 29 Dim graphicsObject2 As Graphics = _ 30 Graphics.FromImage(textureBitmap) ' get bitmap graphics 31 32 ' brush and pen used throughout program 33 Dim solidColorBrush As SolidBrush = _ 34 New SolidBrush(Color.Red) 35 Dim coloredPen As Pen = New Pen(solidColorBrush) Creates a Pen object pen, and passes arguments Color.Red and Integer 10, indicating that we want pen to draw red lines that are 10 pixels wide Creates a new Bitmap image, which is initially empty
53
DrawShapes.vb 36 37 ' draw ellipse filled with a blue-yellow gradient 38 graphicsObject.FillEllipse(linearBrush, 5, 30, 65, 100) 39 40 ' draw thick rectangle outline in red 41 graphicsObject.DrawRectangle(thickRedPen, drawArea2) 42 43 ' fill textureBitmap with yellow 44 solidColorBrush.Color = Color.Yellow 45 graphicsObject2.FillRectangle(solidColorBrush, 0, 0, 10, 10) 46 47 ' draw small black rectangle in textureBitmap 48 coloredPen.Color = Color.Black 49 graphicsObject2.DrawRectangle(coloredPen, 1, 1, 6, 6) 50 51 ' draw small blue rectangle in textureBitmap 52 solidColorBrush.Color = Color.Blue 53 graphicsObject2.FillRectangle(solidColorBrush, 1, 1, 3, 3) 54 55 ' draw small red square in textureBitmap 56 solidColorBrush.Color = Color.Red 57 graphicsObject2.FillRectangle(solidColorBrush, 4, 4, 3, 3) 58 59 ' create textured brush and display textured rectangle 60 Dim texturedBrush As TextureBrush = _ 61 New TextureBrush(textureBitmap) 62 63 graphicsObject.FillRectangle( _ 64 texturedBrush, 155, 30, 75, 100) 65 66 ' draw pie-shaped arc in white 67 coloredPen.Color = Color.White 68 coloredPen.Width = 6 69 graphicsObject.DrawPie( _ 70 coloredPen, 240, 30, 75, 100, 0, 270) A TextureBrush is a brush that fills the interior of a shape with an image, rather than a solid color.
54
71 72 ' draw lines in green and yellow 73 coloredPen.Color = Color.Green 74 coloredPen.Width = 5 75 graphicsObject.DrawLine(coloredPen, 395, 30, 320, 150) 76 77 ' draw a rounded, dashed yellow line 78 coloredPen.Color = Color.Yellow 79 coloredPen.DashCap = LineCap.Round 80 coloredPen.DashStyle = DashStyle.Dash 81 graphicsObject.DrawLine(coloredPen, 320, 30, 395, 150) 82 End Sub ' OnPaint 83 84 End Class ' FrmDrawShapes The DashCap enumeration specifies the styles for the start and end of a dashed line.
55
General path is a shape constructed from straight lines and complex curves. TranslateTransform is a graphics method to indicate that the origin of an object should be translated to the coordinates (X,Y). RotateTransform is a graphics method to move an object to the next position with a particular rotation angle in degree. How to use… Dim graphicsObject As Graphics = e.Graphics graphicsObject.TranslateTransform(X,Y) graphicsObject.RotateTransform(angle) 16.8 Advanced Graphics Capabilities
56
DrawStars.vb 1 ' Fig. 16.22: DrawStars.vb 2 ' Using paths to draw stars on a form. 3 4 Imports System.Drawing.Drawing2D 5 6 Public Class FrmDrawStars 7 Inherits System.Windows.Forms.Form 8 9 ' Visual Studio.NET generated code 10 11 ' create path and draw stars along it 12 Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) 13 Dim graphicsObject As Graphics = e.Graphics 14 Dim i As Integer 15 Dim random As Random = New Random() 16 Dim brush As SolidBrush = _ 17 New SolidBrush(Color.DarkMagenta) 18 19 ' x and y points of path 20 Dim xPoints As Integer() = _ 21 {55, 67, 109, 73, 83, 55, 27, 37, 1, 43} 22 Dim yPoints As Integer() = _ 23 {0, 36, 36, 54, 96, 72, 96, 54, 36, 36} 24 25 ' create graphics path for star 26 Dim star As GraphicsPath = New GraphicsPath() 27 28 ' translate origin to (150, 150) 29 graphicsObject.TranslateTransform(150, 150) 30 31 ' create star from series of points 32 For i = 0 To 8 Step 2 33 star.AddLine(xPoints(i), yPoints(i), _ 34 xPoints(i + 1), yPoints(i + 1)) 35 Next Defines two Integer arrays, representing the x- and y- coordinates of the points in the star
57
36 37 ' close shape 38 star.CloseFigure() 39 40 ' rotate origin and draw stars in random colors 41 For i = 1 To 18 42 graphicsObject.RotateTransform(20) 43 44 brush.Color = Color.FromArgb(random.Next(200, 255), _ 45 random.Next(255), random.Next(255), random.Next(255)) 46 47 graphicsObject.FillPath(brush, star) 48 Next 49 50 End Sub ' OnPaint 51 52 End Class ' FrmDrawStars Uses GraphicsPath method CloseFigure to complete the shape Draws the star 18 times, rotating it around the origin Uses Graphics method RotateTransform to move to the next position on the form Graphics method FillPath draws a filled version of the star with the Brush created earlier
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.