Chapter 16 Graphics and Multimedia - PowerPoint PPT Presentation

1 / 57
About This Presentation
Title:

Chapter 16 Graphics and Multimedia

Description:

amount of red in the color, the second defines the amount of green and the third ... Used to color interiors of shapes. In most Fill methods, Brushes fill a ... – PowerPoint PPT presentation

Number of Views:72
Avg rating:3.0/5.0
Slides: 58
Provided by: reha6
Category:

less

Transcript and Presenter's Notes

Title: Chapter 16 Graphics and Multimedia


1
Chapter 16 Graphics and Multimedia
2
Outline
16.1 Introduction16.2   Graphics Contexts and
Graphics Objects16.3   Color Control16.4   Font
Control16.5   Drawing Lines, Rectangles and
Ovals16.6   Drawing Arcs16.7   Drawing Polygons
and Polylines16.8   Advanced Graphics
Capabilities16.9   Introduction to
Multimedia16.10  Loading, Displaying and Scaling
Images16.11   Animating a Series of
Images16.12   Windows Media Player16.13   Micros
oft Agent
3
16.1 Introduction
  • 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.

4
Fig. 16.1 System.Drawing namespaces Classes and
Structures.
5
16.1 Introduction
  • 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

6
16.1 Introduction
  • 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.

7
16.1 Introduction
  • To begin drawing in Visual Basic, we first must
    understand GDIs 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.1 Introduction
  • 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
16.2   Graphics Contexts and Graphics Objects
  • 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 Controls Paint
    event.

10
16.2   Graphics Contexts and Graphics Objects
  • When displaying graphical information on a
    control, we need to
  • Access Graphics object of the control.
  • Then, use Graphics object to draw shapes and
    strings on the control.
  • Graphics object Can be accessed in 2 ways
  • By overriding OnPaint() method to retrieve a
    Graphics object from argument PaintEventArgs
  • Create a new Graphics object associated with the
    appropriate surface.

11
16.2   Graphics Contexts and Graphics Objects
  • 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

12
16.2   Graphics Contexts and Graphics Objects
  • 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 controls client area and
    repaints all graphical components.

13
16.2   Graphics Contexts and Graphics Objects
  • 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()

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
16.3   Color Control
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 Pens.
  • 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

When the application begins its execution, it
calls class ShowColors OnPaint method to paint
the window.
Gets a reference to PaintEventArgs es Graphics
object and assigns it to Graphics object
graphicsObject
Creates a black and white SolidBrush for drawing
on the form
Graphics method FillRectangle draws a solid white
rectangle with the Brush supplied as a parameter.
Assigns the ColormBehindColor value to the
Brushs Color property and displays a rectangle
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(txtAlphaBo
    x.Text, _
  • 77 txtRedBox.Text, txtGreenBox.Text,
    txtBlueBox.Text)

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(txtColorN
    ame.Text)
  • 88
  • 89 Invalidate() ' refresh Form
  • 90 End Sub ' cmdColorName_Click
  • 91
  • End Class ' FrmColorForm

22
(No Transcript)
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

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

25
(No Transcript)
26
16.4 Font Control
  • Fonts
  • 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 fontthe font in which the text
    displaysto its Font argument.

27
16.4 Font Control
  • DrawString constructors
  • takes a String to display,
  • the display Font,
  • a Brush
  • and the x- and y-coordinates of the location for
    the Strings first character.

28
16.4 Font Control
GraphicsUnit argumentan 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)

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.Na
    me _
  • 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 fonts 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 Integers are
    used
  • Drawing solid shapes
  • Versions that take a Brush and four Integers
  • Integer arguments
  • First two represent the coordinates of the
    upper-left corner of the shape or its enclosing
    area
  • Last two indicate the shapes width and height

34
16.5 Drawing Lines, Rectangles and Ovals
35
16.5 Drawing Lines, Rectangles and Ovals
Fig. 16.15 Ellipse bounded by a rectangle.
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)

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.16 Positive 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)

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

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 gt 1 Then
  • 49
  • 50 ' get array for use in drawing
    functions
  • 51 Dim pointArray() As Point _
  • 52 mPoints.ToArray(mPoints(0).GetTyp
    e())
  • 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

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(By
    Val 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_CheckedChan
    ged(ByVal sender _

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

Event method cmlNewColor_Click allows the user to
select a new drawing color with a ColorDialog
49
(No Transcript)
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
16.8 Advanced Graphics Capabilities
  • 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.FillR
ectangle(BrushVar, X, Y, width, height)
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
    _

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(thickRedPe
    n, drawArea2)
  • 42
  • 43 ' fill textureBitmap with yellow
  • 44 solidColorBrush.Color Color.Yellow
  • 45 graphicsObject2.FillRectangle(solidColo
    rBrush, 0, 0, 10, 10)
  • 46
  • 47 ' draw small black rectangle in
    textureBitmap
  • 48 coloredPen.Color Color.Black
  • 49 graphicsObject2.DrawRectangle(coloredPe
    n, 1, 1, 6, 6)
  • 50
  • 51 ' draw small blue rectangle in
    textureBitmap
  • 52 solidColorBrush.Color Color.Blue
  • 53 graphicsObject2.FillRectangle(solidColo
    rBrush, 1, 1, 3, 3)
  • 54

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
16.8 Advanced Graphics Capabilities
  • 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)
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

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
Write a Comment
User Comments (0)
About PowerShow.com