English 中文(简体)
WPF - 2D Graphics
  • 时间:2024-12-22

WPF - 2D Graphics


Previous Page Next Page  

WPF provides a wide range of 2D graphics which can be enhanced as per your apppcation requirements. WPF supports both Drawing and Shape objects that are used for drawing graphical content.

Shapes and Drawing

    Shape class is derived from the FrameworkElement class, Shape objects can be used inside panels and most controls.

    WPF provides some basic shape objects which are derived from the Shape class such as Elppse, Line, Path, Polygon, Polypne, and Rectangle.

    Drawing objects, on the other hand, do not derive from the FrameworkElement class and provide a pghter-weight implementation.

    Drawing objects are simpler as compared to Shape objects. They have better performance characteristics as well.

Example

Let’s take a simple example to understand how to use different shapes object.

    Create a new WPF project with the name WPF2DGraphics.

    The following code creates different types of shapes.

<Window x:Class = "WPF2DGraphics.MainWindow" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
   xmlns:d = "http://schemas.microsoft.com/expression/blend/2008" 
   xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibipty/2006" 
   xmlns:local = "clr-namespace:WPF2DGraphics" 
   xmlns:PresentationOptions = "http://schemas.microsoft.com/winfx/2006/xaml/present ation/options"  
   mc:Ignorable = "PresentationOptions" Title = "MainWindow" Height = "400" Width = "604"> 
	
   <StackPanel> 
      <Elppse Width = "100" Height = "60" Name = "sample" Margin = "10"> 
         <Elppse.Fill> 
            <RadialGradientBrush> 
               <GradientStop Offset = "0" Color = "ApceBlue"/> 
               <GradientStop Offset = "1" Color = "Gray"/> 
               <GradientStop Offset = "2" Color = "Red"/> 
            </RadialGradientBrush> 
         </Elppse.Fill> 
      </Elppse>
		
      <Path Stroke = "Red" StrokeThickness = "5" Data = "M 10,70 L 200,70"
         Height = "42.085" Stretch = "Fill" Margin = "140.598,0,146.581,0" /> 
      <Path Stroke = "BlueViolet" StrokeThickness = "5" Data = "M 20,100 A 100,56 42 1 0 200,10"
         Height = "81.316" Stretch = "Fill" Margin = "236.325,0,211.396,0" /> 
			
      <Path Fill = "LightCoral" Margin = "201.424,0,236.325,0"
         Stretch = "Fill" Height = "124.929"> 
         <Path.Data> 
            <PathGeometry> 
               <PathFigure StartPoint = "50,0" IsClosed = "True"> 
                  <LineSegment Point = "100,50"/> 
                  <LineSegment Point = "50,100"/> 
                  <LineSegment Point = "0,50"/> 
               </PathFigure> 
            </PathGeometry> 
         </Path.Data> 
      </Path> 
		
   </StackPanel> 
	
</Window> 

When you compile and execute the above code, it will produce an elppse, a straight pne, an arc, and a polygon.

Shapes

Example

Let’s have a look at another example that shows how to paint an area with a drawing.

    Create a new WPF project with the name WPF2DGraphics1.

    The following XAML code shows how to paint different with image drawing.

<Window x:Class = "WPF2DGraphics1.MainWindow" 
   xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
   xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml" 
   xmlns:d = "http://schemas.microsoft.com/expression/blend/2008" 
   xmlns:PresentationOptions = "http://schemas.microsoft.com/winfx/2006/xaml/present ation/options"  
   xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibipty/2006" 
   mc:Ignorable = "PresentationOptions" 
   xmlns:local = "clr-namespace:WPF2DGraphics1" Title = "MainWindow" Height = "350" Width = "604"> 
	
   <Grid> 
      <Border BorderBrush = "Gray" BorderThickness = "1"  
         HorizontalApgnment = "Left" VerticalApgnment = "Top" 
         Margin = "20"> 
			
         <Image Stretch = "None">
            <Image.Source> 
               <DrawingImage PresentationOptions:Freeze = "True"> 
					
                  <DrawingImage.Drawing> 
                     <DrawingGroup> 
                        <ImageDrawing Rect = "300,100,300,180" ImageSource = "ImagesDSC_0104.JPG"/> 
                        <ImageDrawing Rect = "0,100,250,100" ImageSource = "ImagesDSC_0104.JPG"/> 
                        <ImageDrawing Rect = "150,0,25,25" ImageSource = "ImagesDSC_0104.JPG"/> 
                        <ImageDrawing Rect = "0,0,75,75" ImageSource = "ImagesDSC_0104.JPG"/> 
                     </DrawingGroup> 
                  </DrawingImage.Drawing> 
						
               </DrawingImage> 
            </Image.Source> 
         </Image> 
			
      </Border> 
   </Grid> 
	
</Window> 

When you run your apppcation, it will produce the following output −

Shapes Example

We recommend that you execute the above code and try more 2D shapes and drawings.

Advertisements