Blue Theme Orange Theme Green Theme Red Theme
 
Nevron Gauge for SharePoint
Home | Forums | Videos | Photos | Blogs | Beginners | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Nevron Gauge for SharePoint
Search :       Advanced Search »
Home » WPF » WPF Drawing Brush

WPF Drawing Brush

The Drawing object in WPF represents a 2-D drawing that include shapes, text, video, image and other drawings. A Drawing Brush represented by the DrawingBrush object paints a surface with a drawing.

Author Rank :
Page Views : 14739
Downloads : 133
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
DrawingBrushSample.zip
 
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


Drawing Brush

The Drawing object in WPF represents a 2-D drawing that include shapes, text, video, image and other drawings. A Drawing Brush represented by the DrawingBrush object paints a surface with a drawing. The DrawingGroup, GeometryDrawing, GlyphRunDrawing, ImageDrawing, and VideoDrawing classes are inherited from the Drawing class. That means we can create any of these objects to be painted by a DrawingBrush.

Creating a Drawing Brush

The DrawingBrush element in XAML creates a drawing brush.

The following code snippet creates a drawing brush and sets the Drawing property. The Drawing property can be an element inherited from the Drawing such as a GeometryDrawing.

<DrawingBrush >

<DrawingBrush.Drawing />

</DrawingBrush>

 
 

We can fill a shape with a drawing brush by setting a shape's Fill property to the image brush. The code snippet in Listing 23 creates a rectangle shape sets the Fill property to a DrawingBrush.

<Grid Name="LayoutRoot">

    <Rectangle Width="200" Height="200" Stroke="Black" StrokeThickness="0">

        <Rectangle.Fill>

            <DrawingBrush >

                <DrawingBrush.Drawing>

                    <GeometryDrawing Brush="Yellow">

                        <GeometryDrawing.Geometry>

                            <GeometryGroup>

                                <RectangleGeometry Rect="50,25,25,25" />

                                <RectangleGeometry Rect="25,50,25,25" />

                            </GeometryGroup>

                        </GeometryDrawing.Geometry>

                        <GeometryDrawing.Pen>

                            <Pen Thickness="5">

                                <Pen.Brush>

                                    <LinearGradientBrush>

                                        <GradientStop Offset="0.0" Color="Blue" />

                                        <GradientStop Offset="1.0" Color="Black" />

                                    </LinearGradientBrush>

                                </Pen.Brush>

                            </Pen>

                        </GeometryDrawing.Pen>

                    </GeometryDrawing>

                </DrawingBrush.Drawing>

            </DrawingBrush>

        </Rectangle.Fill>

    </Rectangle>

</Grid>

Listing 23

The output looks like Figure 27.

DBImg1.jpg

Figure 27. A shape filled with a Drawing brush

The Viewport property determines the size and position of the base tile when the TileMode of a DrawingBrush is not set to None, and the ViewportUnits property determines whether the Viewport is specified using absolute or relative coordinates. If the coordinates are relative, they are relative to the size of the output area. The point (0,0) represents the top left corner of the output area, and (1,1) represents the bottom right corner of the output area. To specify that the Viewport property uses absolute coordinates, set the ViewportUnits property to Absolute.

The TileMode property of DrawingBrush represents the tile mode that is a type if a TileMode enumeration. The TileMode enumeration has Tile, FlipX, FlipY, FlipXY, and None values.

The following code snippet sets the Viewport and TileMode properties of a DrawingBrush.

<DrawingBrush Viewport="0,0,0.25, 0.25" TileMode="Tile">

Figure 28, 29, and 30 show the tile modes values Tile, FilpXY, and FlipX.

  DBImg2.jpg

Figure 28. A shape filled with a Drawing brush in Tile mode

  DBImg3.jpg

Figure 29. A shape filled with a Drawing brush with TileMode as FlipXY

 

DBImg4.jpg


Figure 30. A shape filled with a Drawing brush with TileMode as FlipX

One of the key examples of tile mode is create a chess board like image that has a repeating rectangle with black and white background colors. The CreateARectangleWithDrawingBrush method listed in Listing 24 draws a chess board like rectangle with a drawing brush dynamically.

private void CreateARectangleWithDrawingBrush()

{

    // Create a background recntangle

    Rectangle chessBoard = new Rectangle();

    chessBoard.Width = 300;

    chessBoard.Height = 300;

 

    // Create a DrawingBrush

    DrawingBrush blackBrush = new DrawingBrush();

    // Create a Geometry with white background

    GeometryDrawing backgroundSquare =

        new GeometryDrawing(

            Brushes.White,

            null,

            new RectangleGeometry(new Rect(0, 0, 400, 400)));

    // Create a GeometryGroup that will be added to Geometry

    GeometryGroup gGroup = new GeometryGroup();

    gGroup.Children.Add(new RectangleGeometry(new Rect(0, 0, 200, 200)));

    gGroup.Children.Add(new RectangleGeometry(new Rect(200, 200, 200, 200)));

    // Create a GeomertyDrawing

    GeometryDrawing checkers = new GeometryDrawing(new SolidColorBrush(Colors.Black), null, gGroup);

   

    DrawingGroup checkersDrawingGroup = new DrawingGroup();

    checkersDrawingGroup.Children.Add(backgroundSquare);

    checkersDrawingGroup.Children.Add(checkers);

 

    blackBrush.Drawing = checkersDrawingGroup;

 

    // Set Viewport and TimeMode

    blackBrush.Viewport = new Rect(0, 0, 0.25, 0.25);

    blackBrush.TileMode = TileMode.Tile;

 

    // Fill rectangle with a DrawingBrush

    chessBoard.Fill = blackBrush;

 

    LayoutRoot.Children.Add(chessBoard);

}

Listing 24

The output of Listing 24 looks like Figure 31.

DBImg5.jpg

Figure 31. A chess board

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Author
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Nevron Gauge for SharePoint
Become a Sponsor
 Comments
Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.