2D and 3D Graphics and Animations
2D and 3D Graphics and Animations
Animations
2D and 3D Graphics and Animations
c ad e my.t e le r ik .com/
http://a
Doncho Minkov
Technical Trainer
http://www.minkov.it
Telerik Software Academy
academy.telerik.com
Table of Contents (2)
1. Introduction to WPF Graphics
2. WPF Drawing Model
3. Resolution Independence
4. Basic Graphics Objects
5. Basic Shapes
6. Bitmaps, Images and Effects
7. Brushes and Pens
8. Transformations
9. Animation
2
Introduction to
WPF Graphics
Introduction to WPF
Graphics
Graphical elements can be
integrated into any part of user
interface
Free to mix them with any other
kind of element
Use graphical elements inside
controls
E.g. put an ellipse inside a button
4
WPF Graphics
Example
<DockPanel>
<StackPanel DockPanel.Dock="Top"
Orientation="Horizontal">
<TextBlock Text="Mix text, " />
<Ellipse Fill="red" Width="40" />
<TextBlock Text=" and " />
<Button>Controls</Button>
</StackPanel>
<Ellipse DockPanel.Dock="Left" Fill="Yellow"
Width="100" />
<Button DockPanel.Dock="Left">z</Button>
<TextBlock FontSize="24" TextWrapping="Wrap">
And of course you can put graphics into
your text:
<Ellipse Fill="Cyan" Width="50" Height="20" />
</TextBlock>
</DockPanel>
5
Introduction to WPF
Graphics
Live Demo
WPF Drawing Model
WPF Drawing Model
In WPF we don't need to write a C#
code to respond to redraw
requests\
WPF can keep the screen repainted
for us
This is because WPF lets us
represent drawings as objects that
can be represented as XAML
Objects are representing graphical
shapes in the tree of user interface
elements 8
WPF Drawing Model
Example
<Canvas x:Name="MainCanvas" MouseLeftButtonDown=
"mainCanvas_MouseLeftButtonDown">
<Rectangle Canvas.Left="10" Canvas.Top="30"
Fill="Indigo" Width="40" Height="20" />
<Rectangle Canvas.Left="20" Canvas.Top="40"
Fill="Yellow" Width="40" Height="20" />
<Rectangle Canvas.Left="30" Canvas.Top="50"
Fill="Orange" Width="40" Height="20" />
</Canvas>
21
Rectangle
It can be drawn either filled in
shape, as an outline, or both filled
in and outlined
Rectangle doesnt provide any
properties for setting its location
The location is determined by the
container (e.g. Canvas, StackPanel,
<Canvas>
FlowPanel, )
<Rectangle Fill="Yellow" Stroke="Black"
Canvas.Left="30" Canvas.Top="10"
Width="100" Height="20" />
</Canvas>
22
Rectangle (2)
A Rectangle will usually be aligned
with the coordinate system of its
parent panel
If the parent panel has been
rotated, Rectangle will of course be
also rotated
RadiusX and RadiusY properties
Draw a rectangle with rounded
corners
RenderTransform property
Transforms a Rectangle relative to
23
Ellipse
Ellipse is similar to Rectangle
Size, location, rotation, fill, and
stroke of an Ellipse are controlled
in exactly the same way as for a
Rectangle
<Ellipse Width="100" Height="50" Fill="Yellow"
Stroke="Black" />
24
Line
Draws a straight line between two
points
Controlling the location
25
Ellipses and Lines
Live Demo
Polyline
Draw a connected series of line
segments
Points property
27
Polygon
Polygon is very similar to Polyline
It has a Points property that works
in exactly the same way as
Polylines
Polygon always draws a closed
<Polyline Fill="Orange" Stroke="Blue"
shape
StrokeThickness="2" Points="40,10 70,50
10,50" />
<Polygon Fill="Orange" Stroke="Blue"
StrokeThickness="2" Points="40,10 70,50
10,50" />
<!--The result is-->
28
Polygon (2)
FillRule property
If this number is odd, the point was
inside the shape
If it is even, the point is outside the
shape
The default rule is EvenOdd
<Polygon Fill="Orange" Stroke="Blue"
StrokeThickness="2"
FillRule="Nonzero" Points="10,10 60,10 60,25
20,25
20,40 40,40 40,18 50,18 50,50 10,50" />
<!--The result is-->
29
Path
Path draws more complex shapes
Data property specifies the
Geometry
Three geometry types
RectangleGeometry
Correspond to the Rectangle
EllipseGeometry
Correspond to the Ellipse
LineGeometry
Correspond to the Line
30
Path (2)
GeometryGroup object
Create a shape with multiple
geometries
<Canvas>
<Path Fill="Cyan" Stroke="Black">
<Path.Data>
<GeometryGroup>
<EllipseGeometry Center="20, 40"
RadiusX="20"
RadiusY="40" />
<EllipseGeometry Center="20, 40"
RadiusX="10"
RadiusY="30" />
</GeometryGroup>
</Path.Data> <!--The result is-->
</Path>
</Canvas>
31
Path
Live Demo
Path (3)
The ArcSegment
Add elliptical curves to the edge of
a shape
Provides two flags
IsLargeArc determines whether you
get the larger or smaller slice size
SweepDirection chooses on which
side of the line the slice is drawn
Draw Bzier curves and combining
shapes
33
Arc Segment Example
<Path Fill="Cyan" Stroke="Black">
<Path.Data>
<PathGeometry>
<PathFigure StartPoint="0,11"
IsClosed="True">
<ArcSegment Point="50,61" Size="70,30"
SweepDirection="Counterclockwise" />
</PathFigure>
<PathFigure StartPoint="30,11"
IsClosed="True">
<ArcSegment Point="80,61" Size="70,30"
SweepDirection="Clockwise"
IsLargeArc="True" />
</PathFigure>
</PathGeometry> <!--The result is-->
</Path.Data>
</Path>
34
ArcSegment
Live Demo
Bitmaps,
Images and
Effects
Image
Image simply displays a picture
Place image anywhere in the visual
tree
Source property
40
Creating Bitmaps (2)
You can choose any resolution you
like for the output
RenderTargetBitmap lets you build a
bitmap out of any combination of
WPF visuals
It is great if you want to build or
modify a bitmap using WPF
elements
41
Bitmap Effects
BitmapEffects property
Apply a visual effect to the element
and all of its children
All of these effects use bitmap
processing algorithms
<StackPanel Orientation="Horizontal">
<StackPanel Orientation="Vertical">
<TextBlock Text="Normal Text"
TextAlignment="Center"
FontWeight="Bold" />
<RadioButton Content="Better in position 1?"
GroupName="r" />
</StackPanel>
<!--The example continues-->
42
Bitmap Effects (2)
<StackPanel Orientation="Vertical" Margin="10,0">
<StackPanel.BitmapEffect>
<BlurBitmapEffect Radius="3" />
</StackPanel.BitmapEffect>
<TextBlock Text="Blurred Text"
TextAlignment="Center"
FontWeight="Bold" />
<RadioButton Content="Or position 2?"
GroupName="r" />
</StackPanel>
</StackPanel>
The built-in effects
BevelBitmapEffect
BitmapEffectGroup
BlurBitmapEffect
43
Bitmap Effects
Live Demo
Brushes and Pens
SolidColorBrush
SolidColorBrush uses one color
across the whole area being
painted
It has just one property Color
<Rectangle Fill="Yellow" Width="100"
Height="20" />
47
LinearGradientBrush (2)
<Rectangle Width="80" Height="60">
<Rectangle.Fill>
<LinearGradientBrush StartPoint="0,0"
EndPoint="1,1">
<GradientStop Color="Blue" Offset="0" />
<GradientStop Color="White" Offset="1" />
</LinearGradientBrush> <!--The result is-->
</Rectangle.Fill>
</Rectangle>
49
ImageBrush (2)
AlignmentX and AlignmentY
properties
Horizontal and vertical alignment of
content in the TileBrush base tile
Viewbox, Viewport, ViewboxUnits,
and ViewportUnits properties
Allow you to focus on any part of
the image or choose specific scale
factors
TileMode property
50
ImageBrush
Live Demo
Pen
A Pen is always based on a brush
Accessed through Stroke property
Describes how a shape is outlined
Important properties
52
Transformations
Transform Classes
TranslateTransform displaces the
coordinate system by some amount
RotateTransform rotates
coordinate system
Angle, CenterX, CenterY properties
Storyboard.TargetProperty="(Canvas.Left)"
From="-50" To="300"
Duration="00:00:0.88"
AutoReverse="True"
RepeatBehavior="Forever" />
</Storyboard></BeginStoryboard>
</EventTrigger>
</Ellipse.Triggers>
</Ellipse>
59
</Canvas>
Animatio
n Live Demo
WPF Graphics and
? Animations
?
?
?
?
?
?
? ?
Questions?
?
?
?
?
?
?
? ?
?
?
http://academy.teler
ASP.NET - , , C#, .NET, ASP.NET
ASP.NET MVC HTML, SQL, C#, .NET, ASP.NET MVC
SEO - ,
, iPhone, Android, W P7, PhoneGap
, HTML, CSS, JavaScript, Photoshop -
free C# book, C#, Java, C# -
" "
" cloud " C# , ,
Exercises
1. Write a program that visualize this
figure. Use only rectangles
and RenderTransform.
2. Draw the rectangles from the previous
exercise with rounded corners.
3. Write a WPF program that visualize the
figure below. Use Polyline and Polygon
and FillRule property.
4. In the demo "Arc Segment" add rotation
of 45 degrees (rotating the ellipses
before slicing them).
5. Draw few national flags (e.g. Bulgarian,
German, ). Make an animation that
Exercises (2)
6. Write a WPF program that visualize a
LinearGradientBrush with multiple
colors (use Offset property).
20. Create
a WPF application that shows
the table below:
Free Trainings @ Telerik
Academy
Desktop Applications with C# and
WPF
academy.telerik.com/
Telerik Software Academy
academy.telerik.com
Telerik Academy @ Facebook
facebook.com/TelerikAcademy
Telerik Software Academy Forums
forums.academy.telerik.com