Horizontal linear gradient


   
  

<Window x:Class="WpfApplication1.LinearGradientBrushExample"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="LinearGradientBrush" Height="375" Width="300">
    <StackPanel Grid.Column="1" Grid.Row="0">
      <TextBlock Margin="5" Text="Horizontal linear gradient:" />
      <Rectangle Width="100" Height="75" Stroke="Blue">
        <Rectangle.Fill>
          <LinearGradientBrush StartPoint="0,0"
            EndPoint="0,1">
            <GradientStop Color="Red" Offset="0" />
            <GradientStop Color="White" Offset="1" />
          </LinearGradientBrush>
        </Rectangle.Fill>
      </Rectangle>
    </StackPanel>
</Window>

   
    
     


Diagonal linear gradient with 0.5 Offset for White


   
  

<Window x:Class="WpfApplication1.LinearGradientBrushExample"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="LinearGradientBrush" Height="375" Width="300">
    <StackPanel>
      <TextBlock Margin="5,10,5,0"
        Text="Diagonal linear gradient" />
      <TextBlock Margin="5,0,5,5"
        Text="- with 0.5 Offset for White" />
      <Rectangle Width="100" Height="75" Stroke="Blue">
        <Rectangle.Fill>
          <LinearGradientBrush StartPoint="0,0"
            EndPoint="1,1">
            <GradientStop Color="Green" Offset="0" />
            <GradientStop Color="White" Offset="0.5" />
          </LinearGradientBrush>
        </Rectangle.Fill>
      </Rectangle>
    </StackPanel>
</Window>

   
    
     


Diagonal linear gradient multiple colors 2


   
  

<Window x:Class="WpfApplication1.LinearGradientBrushExample"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="LinearGradientBrush" Height="375" Width="300">
    <StackPanel Grid.Column="1" Grid.Row="2">
      <TextBlock Margin="5,10,5,0" Text="Diagonal linear gradient" />
      <TextBlock Margin="5,0,5,5" Text="- multiple colors" />
      <Rectangle Width="100" Height="75" Stroke="Blue">
        <Rectangle.Fill>
          <LinearGradientBrush StartPoint="0,0"
            EndPoint="1,1">
            <GradientStop Color="Red" Offset="0.2" />
            <GradientStop Color="Yellow" Offset="0.3" />
            <GradientStop Color="Coral" Offset="0.4" />
            <GradientStop Color="Blue" Offset="0.5" />
            <GradientStop Color="White" Offset="0.6" />
            <GradientStop Color="Green" Offset="0.7" />
            <GradientStop Color="Purple" Offset="0.8" />
          </LinearGradientBrush>
        </Rectangle.Fill>
      </Rectangle>
    </StackPanel>
</Window>

   
    
     


Diagonal linear gradient – multiple colors


   
  

<Window x:Class="WpfApplication1.LinearGradientBrushExample"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="LinearGradientBrush" Height="375" Width="300">
    <StackPanel Grid.Column="0" Grid.Row="2">
      <TextBlock Margin="5,10,5,0"
        Text="Vertical linear gradient" />
      <TextBlock Margin="5,0,5,5" Text="- multiple colors" />
      <Rectangle Width="100" Height="75" Stroke="Blue">
        <Rectangle.Fill>
          <LinearGradientBrush StartPoint="0,0"
            EndPoint="1,0">
            <GradientStop Color="Red" Offset="0.3" />
            <GradientStop Color="Green" Offset="0.5" />
            <GradientStop Color="Blue" Offset="0.8" />
          </LinearGradientBrush>
        </Rectangle.Fill>
      </Rectangle>
    </StackPanel>
</Window>

   
    
     


Creating Lines


   
  


<Window x:Class="WpfApplication1.PerpendicularLine"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  Title="Perpendicular Line" Height="300" Width="400">
  <Viewbox Stretch="Uniform">
    <Canvas Name="canvas1" Grid.Column="1" Margin="10"
      ClipToBounds="True" Width="270" Height="280">
        <TextBlock Name="tbPoint1" Canvas.Top="10">
          Point1
        </TextBlock>
        <TextBlock Name="tbPoint2" Canvas.Top="25">
          Point2
        </TextBlock>
        <TextBlock Name="tbPoint3" Canvas.Top="40">
          Point3
        </TextBlock>
        <TextBlock Name="tbPoint4" Canvas.Top="55">
          Point4
        </TextBlock>
      </Canvas>
  </Viewbox>
</Window>

//File:Window.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;


namespace WpfApplication1
{
    public partial class PerpendicularLine : Window
    {
        private Line line1;
        private Line line2;
        public PerpendicularLine()
        {
            InitializeComponent();
            Rectangle rect = new Rectangle();
            rect.Stroke = Brushes.Black;
            rect.Width = canvas1.Width;
            rect.Height = canvas1.Height;
            canvas1.Children.Add(rect);
            line1 = new Line();
            line2 = new Line();
            AddLines();
        }
        private void AddLines()
        {
            Point pt1 = new Point();
            Point pt2 = new Point();

            pt1.X = 30;
            pt1.Y = 300;
            pt2.X = 100;
            pt2.Y = 500;
            double length = 400;
            line1 = new Line();
            line1.X1 = pt1.X;
            line1.Y1 = pt1.Y;
            line1.X2 = pt2.X;
            line1.Y2 = pt2.Y;
            line1.Stroke = Brushes.Gray;
            line1.StrokeThickness = 4;
            canvas1.Children.Add(line1);
            Canvas.SetLeft(tbPoint1, pt1.X);
            Canvas.SetTop(tbPoint1, pt1.Y);
            Canvas.SetLeft(tbPoint2, pt2.X);
            Canvas.SetTop(tbPoint2, pt2.Y);
            tbPoint1.Text = "Pt1(" + pt1.ToString() + ")";
            tbPoint2.Text = "Pt2(" + pt2.ToString() + ")";

            Vector v1 = pt1 - pt2;
            Matrix m1 = new Matrix();
            Point pt3 = new Point();
            Point pt4 = new Point();
            m1.Rotate(-90);
            v1.Normalize();
            v1 *= length;
            line2 = new Line();
            line2.Stroke = Brushes.Gray;
            line2.StrokeThickness = 4;
            line2.StrokeDashArray =

            DoubleCollection.Parse("3, 1");
            pt3 = pt2 + v1 * m1;
            m1 = new Matrix();
            m1.Rotate(90);
            pt4 = pt2 + v1 * m1;
            line2.X1 = pt3.X;
            line2.Y1 = pt3.Y;
            line2.X2 = pt4.X;
            line2.Y2 = pt4.Y;
            canvas1.Children.Add(line2);
            Canvas.SetLeft(tbPoint3, pt3.X);
            Canvas.SetTop(tbPoint3, pt3.Y);
            Canvas.SetLeft(tbPoint4, pt4.X);
            Canvas.SetTop(tbPoint4, pt4.Y);
            pt3.X = Math.Round(pt3.X, 0);
            pt3.Y = Math.Round(pt3.Y, 0);
            pt4.X = Math.Round(pt4.X, 0);
            pt4.Y = Math.Round(pt4.Y, 0);
            tbPoint3.Text = "Pt3(" + pt3.ToString() + ")";
            tbPoint4.Text = "Pt4(" + pt4.ToString() + ")";
        }

    }
}

   
    
     


Draw a Grid


   
    

<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Canvas.Resources>
        <Style x:Key="base" TargetType="Line">
            <Setter Property="Stroke" Value="Blue" />
        </Style>

        <Style x:Key="horz" TargetType="Line"
               BasedOn="{StaticResource base}">
            <Setter Property="X1" Value="100" />
            <Setter Property="X2" Value="300" />
            <Setter Property="Y2" Value="{Binding RelativeSource={RelativeSource self}, Path=Y1}" />
        </Style>

        <Style x:Key="vert" TargetType="Line" BasedOn="{StaticResource base}">
            <Setter Property="Y1" Value="100" />
            <Setter Property="Y2" Value="300" />
            <Setter Property="X2" Value="{Binding RelativeSource={RelativeSource self}, Path=X1}" />
        </Style>
    </Canvas.Resources>

    <Line Style="{StaticResource horz}" Y1="100" />
    <Line Style="{StaticResource horz}" Y1="125" />
    <Line Style="{StaticResource horz}" Y1="150" />
    <Line Style="{StaticResource horz}" Y1="175" />
    <Line Style="{StaticResource horz}" Y1="200" />
    <Line Style="{StaticResource horz}" Y1="225" />
    <Line Style="{StaticResource horz}" Y1="250" />
    <Line Style="{StaticResource horz}" Y1="275" />
    <Line Style="{StaticResource horz}" Y1="300" />

    <Line Style="{StaticResource vert}" X1="100" />
    <Line Style="{StaticResource vert}" X1="125" />
    <Line Style="{StaticResource vert}" X1="150" />
    <Line Style="{StaticResource vert}" X1="175" />
    <Line Style="{StaticResource vert}" X1="200" />
    <Line Style="{StaticResource vert}" X1="225" />
    <Line Style="{StaticResource vert}" X1="250" />
    <Line Style="{StaticResource vert}" X1="275" />
    <Line Style="{StaticResource vert}" X1="300" />
</Canvas>

   
    
    
    
     


Draws a diagonal line from (10,10) to (50,50) and moves it 100 pixels to the right


   
    
         
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="Microsoft.Samples.Graphics.RectangleExample"
    WindowTitle="Example">
  <Canvas>
    <Line
      X1="10" Y1="10"
      X2="50" Y2="50"
      StrokeThickness="4"
      Canvas.Left="100">
      <Line.Stroke>
        <RadialGradientBrush GradientOrigin="0.5,0.5" Center="0.5,0.5" RadiusX="0.5" RadiusY="0.5">
          <RadialGradientBrush.GradientStops>
            <GradientStop Color="Red" Offset="0" />
            <GradientStop Color="Blue" Offset="0.25" />
          </RadialGradientBrush.GradientStops>
        </RadialGradientBrush>
      </Line.Stroke>
    </Line>

  </Canvas>
</Page>