Floating FB sharing byReview Results

Floating FB sharing byReview Results

  • WPF Tutorial

    • # Read in Your Language

    2D Graphics in WPF

    Tutorial Home | 2D Graphics in WPF

    2D Graphics in WPF

    2D Graphics:

    WPF provides two different objects for graphical drawing:

    1. Shape objects
    2. Drawing object

    Shape objects: This is derived from frameworkelement. This provides set of graphical shapes that can be embedded in panels or any content controls. Provided shape objects in WPF are Ellipse, Line, Path, Polygon, Polyline, and Rectangle.

    Namespace: System.Windows.Shapes
    Sample code for shape objects:

    Drawing ellipse in WPF:

                <Ellipse Width="300" Height="200" Name="sample">
                            <GradientStop Offset="0" Color="Orange"/>
                            <GradientStop Offset="1" Color="Blue"/>
                            <GradientStop Offset="2" Color="Yellow"/>

    From codebehind:

    ellipse1 = new Ellipse();
    ellipse1.Stroke = System.Windows.Media.Brushes.Red;
    ellipse1.Fill = System.Windows.Media.Brushes.Black;
    ellipse1.HorizontalAlignment = HorizontalAlignment.Left;
    ellipse1.VerticalAlignment = VerticalAlignment.Center;
    ellipse1.Width = 90;
    ellipse1.Height = 125;

    Drawing a line in WPF:

        <Line Name="sample" StrokeThickness="2" X1="1" X2="100" Y1="1" Y2="100">              

    From codebehind:

    // Add a Line 
    line1 = new Line();
    line1.Stroke = System.Windows.Media.Brushes.Blue;
    line1.X1 = 1;
    line1.X2 = 50;
    line1.Y1 = 1;
    line1.Y2 = 50;
    line1.HorizontalAlignment = HorizontalAlignment.Left;
    line1.VerticalAlignment = VerticalAlignment.Center;
    line1.StrokeThickness = 2;

    Drawing a Path in WPF:

    Drawing aline using path:
        <Path Stroke="Red" StrokeThickness="1" Data="M 10,70 L 200,70" />
    Using data object:
                <Path Stroke="Red" StrokeThickness="1">
                            <PathFigure StartPoint="10,70">
                                <LineSegment Point="200,70" />

    From codebehind:

    PathFigure Figure1 = new PathFigure();
    Figure1.StartPoint = new Point(10, 70);
    LineSegment LineSegment1 = new LineSegment();
    LineSegment1.Point = new Point(200, 70);
    PathSegmentCollection PathSegmentCollection1 = new PathSegmentCollection();
    myPathFigure.Segments = PathSegmentCollection1;
    PathFigureCollection FigureCollection1 = new PathFigureCollection();
    PathGeometry pathGeometry1 = new PathGeometry();
    pathGeometry1.Figures = FigureCollection1;
    Path myPath = new Path();
    myPath.Stroke = Brushes.Red;
    myPath.StrokeThickness = 1;
    myPath.Data = pathGeometry1;

    Drawing an arc using path :
      <Path Stroke="Black" StrokeThickness="1"  
      Data="M 20,120 A 100,56 42 1 0 200,140" />

    Drawing Polygons in WPF:

    Sample to draw triangle with border:
         <Polygon Points="10,110 60,10 110,110" Fill="Brown" Stroke="Black" 
            StrokeThickness="4" Height="300" Width="470" />         

    From codebehind:

    polygon1 = new Polygon();
    polygon1.Stroke = System.Windows.Media.Brushes.Red;
    polygon1.Fill = System.Windows.Media.Brushes.Green;
    polygon1.StrokeThickness = 1;
    polygon1.HorizontalAlignment = HorizontalAlignment.Left;
    polygon1.VerticalAlignment = VerticalAlignment.Center;
    System.Windows.Point Point1 = new System.Windows.Point(1, 80);
    System.Windows.Point Point2 = new System.Windows.Point(10,100);
    System.Windows.Point Point3 = new System.Windows.Point(80,80);
    PointCollection pCollection1 = new PointCollection();
    polygon1.Points = pCollection1;

    Drawing polyline in WPF:

        <Polyline Points="10,110 60,10 110,110,160 20" Stroke="Black" 
        StrokeThickness="4" /> 

    From codebehind:

    Polyline poly = new Polyline();
    poly.Points.Add(new Point(10, 110));       
    poly.Points.Add(new Point(60, 60));
    poly.Points.Add(new Point(110, 110)); 
    poly.Points.Add(new Point(160, 60));
    poly.Stroke = System.Windows.Media.Brushes.Blue;
    poly.StrokeThickness = 2;

    Drawing Rectangle in WPF:

    <DockPanel Name="dc1">           
        <Rectangle Width="100" Height="50" Fill="Brown" />
    Rounded rectangle:
    <DockPanel Name="dc1">           
         <Rectangle  Width="100" Height="50" Fill="Black" 
          Stroke="Brown" StrokeThickness="4" RadiusX="20" RadiusY="20"/>

    From codebehind:

    Rectangle rect1 = new System.Windows.Shapes.Rectangle();
    rect1.Stroke = System.Windows.Media.Brushes.Black;
    rect1.Fill = System.Windows.Media.Brushes.Red;
    rect1.VerticalAlignment = VerticalAlignment.Center;
    rect1.Height = 50;
    rect1.Width = 70;

    Drawing object: This is light weight object compared to shape object.This is not derived from frameworkelement.Used for drawing shapes , text and images.

    Four types of Drawing objects:
    • GeometryDrawing used for Drawing shape.
    • ImageDrawing used for Drawing an image.
    • GlyphRunDrawing used for Drawing text.
    • DrawingGroup used for combining other drawings into single drawing.

    Namespace: System.Windows.Media

    GeometryDrawing: Region of a control is defined using Geometry objects. Geometry objects are objects such as composite shapes, rectangles, ellipses and circles. CombinedGeometry, EllipseGeometry, and RectangleGeometry are derived from this object.

    <DockPanel Name="dc1">
        <Image Stretch="None" HorizontalAlignment="Left">
              <DrawingImage >
                     <!-- Create a composite shape. -->
              <RectangleGeometry Rect="50,80,50,80"  RadiusX="45" RadiusY="20" />
              <RectangleGeometry Rect="80,50,80,50"  RadiusX="20" RadiusY="45" />
                    <GradientStop Offset="0.0" Color="Brown" />
                    <GradientStop Offset="1.0" Color="#CCCCFF" />
                      <Pen Thickness="4" Brush="Black" />

    From codebehind:

    ImageDrawing: This is used to draw an image inside a region defined by rectangular area.

    <DockPanel Name="dc1">
            <ImageDrawing Rect="0,150,25,25" ImageSource=" Images\win2wpf.jpg"/>
            <ImageDrawing Rect="150,0,25,25" ImageSource=" Images\win2wpf.jpg"/>
            <ImageDrawing Rect="0,0,75,75" ImageSource="Images\win2wpf.jpg"/>

    GlyphRunDrawing : GlyphRun is low-level object used for fixed-format document presentation and also for print scenarios.

        <GlyphRunDrawing ForegroundBrush="White">
                     GlyphIndices="41 76 79 72" 
                     AdvanceWidths="5.859375 2.90625 2.90625 6.275390625">
                 <GlyphTypeface FontUri="C:\WINDOWS\Fonts\SEGOEUI.TTF"/>

    DrawingGroup :To have multiple drawings in to single - composite drawing.

        <ImageDrawing Rect="0,150,25,25" ImageSource=" Images\win2wpf.jpg"/>
        <ImageDrawing Rect="150,0,25,25" ImageSource=" Images\win2wpf.jpg"/>
        <ImageDrawing Rect="0,0,75,75" ImageSource="Images\win2wpf.jpg"/>

    << Previous Topic >> | << Next Topic >>