A simple way to draw in wpf.
<Window x:Class="DrawingGroupDemo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow"
Width="525"
Height="350">
<Grid>
<Image Width="200" Height="200">
<Image.Source>
<DrawingImage>
<DrawingImage.Drawing>
<DrawingGroup x:Name="DrawingGroup" />
</DrawingImage.Drawing>
</DrawingImage>
</Image.Source>
</Image>
</Grid>
</Window>
Draw it in code.
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Animation;
namespace DrawingGroupDemo
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DrawingContext dc = DrawingGroup.Open();
// Draw a rectangle.
dc.DrawRectangle(new SolidColorBrush(Colors.BurlyWood), new Pen(new SolidColorBrush(Colors.Coral), 10.0),
new Rect(new Size(100, 100)));
// Draw a line.
dc.DrawLine(new Pen(new SolidColorBrush(Colors.Chartreuse), 10.0), new Point(0.0, 50.0),
new Point(100.0, 50.0));
// Draw text.
dc.DrawText(
new FormattedText("Hello", CultureInfo.CurrentCulture, FlowDirection.LeftToRight,
new Typeface(new FontFamily("Times New Roman"), FontStyles.Normal, FontWeights.Black,
FontStretches.Normal), 30.0, new SolidColorBrush(Colors.Purple)),
new Point(15.0, 50.0));
// Apply animation to ellipse center.
AnimationClock animationClock =
new PointAnimation(new Point(1.0, 1.0), new Point(100.0, 100.0), new Duration(new TimeSpan(0, 0, 3)))
.CreateClock();
dc.DrawEllipse(new SolidColorBrush(Colors.Aqua), new Pen(new SolidColorBrush(Colors.Orange), 2.0),
new Point(5.0, 5.0), animationClock, 4.0, null, 4.0, null);
// Close to activate. Or use "using DrawingGroup.Open()".
dc.Close();
}
}
}
0 件のコメント:
コメントを投稿