WPF InkCanvas绘制矩形和椭圆
前面说到了InkCanvas的基本操作,这里用一个实例来说明具体应用:绘制矩形和椭圆。
效果图
xaml代码
<Window x:Class="WPF_InkCanvas.ROI_InkCanvas" 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-compatibility/2006" xmlns:local="clr-namespace:WPF_InkCanvas" mc:Ignorable="d" Title="ROI_InkCanvas" Height="450" Width="800"> <Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition Height="auto"/> </Grid.RowDefinitions> <Image Name="imgMeasure" HorizontalAlignment="Center" Stretch="Uniform"/> <InkCanvas Name="inkCanvasMeasure" EditingMode="None" Background="Transparent" Strokes="{Binding InkStrokes, Mode=TwoWay}" HorizontalAlignment="Center" Width="{Binding ElementName=imgMeasure, Path=ActualWidth}" Height="{Binding ElementName=imgMeasure, Path=ActualHeight}" MouseDown="InkCanvasMeasure_MouseDown" MouseMove="InkCanvasMeasure_MouseMove"> <Label Content="{Binding MeaInfo}" Background="Transparent" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10" FontSize="18" Foreground="Red" IsHitTestVisible="False"/> </InkCanvas> <StackPanel Grid.Row="1" Orientation="Horizontal"> <Button Content="OpenFile" Margin="5" HorizontalAlignment="Left" FontSize="20" Click="OpenFile_Click"/> <ToggleButton Name="btnSquare" Content="Draw Square" Margin="5" HorizontalAlignment="Left" FontSize="20" Click="DrawSquare_Click"/> <ToggleButton Name="btnEllipse" Content="Draw Ellipse" Margin="5" HorizontalAlignment="Left" FontSize="20" Click="DrawEllipse_Click"/> </StackPanel> </Grid> </Window>
后台代码
using Microsoft.Win32; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Ink; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; namespace WPF_InkCanvas { /// <summary> /// ROI_InkCanvas.xaml 的交互逻辑 /// </summary> public partial class ROI_InkCanvas : Window { private ViewModel viewModel; private System.Windows.Point iniP; public ROI_InkCanvas() { InitializeComponent(); DrawingAttributes drawingAttributes = new DrawingAttributes { Color = Colors.Red, Width = 2, Height = 2, StylusTip = StylusTip.Rectangle, //FitToCurve = true, IsHighlighter = false, IgnorePressure = true, }; inkCanvasMeasure.DefaultDrawingAttributes = drawingAttributes; viewModel = new ViewModel { MeaInfo = "测试······", InkStrokes = new StrokeCollection(), }; DataContext = viewModel; } private void OpenFile_Click(object sender, RoutedEventArgs e) { OpenFileDialog openDialog = new OpenFileDialog { Filter = "Image Files (*.jpg)|*.jpg|Image Files (*.png)|*.png|Image Files (*.bmp)|*.bmp", Title = "Open Image File" }; if (openDialog.ShowDialog() == true) { BitmapImage image = new BitmapImage(); image.BeginInit(); image.UriSource = new Uri(openDialog.FileName, UriKind.RelativeOrAbsolute); image.EndInit(); imgMeasure.Source = image; } } private void DrawSquare_Click(object sender, RoutedEventArgs e) { if (btnSquare.IsChecked == true) { btnEllipse.IsChecked = false; } } private void DrawEllipse_Click(object sender, RoutedEventArgs e) { if (btnEllipse.IsChecked == true) { btnSquare.IsChecked = false; } } private List<System.Windows.Point> GenerateEclipseGeometry(System.Windows.Point st, System.Windows.Point ed) { double a = 0.5 * (ed.X - st.X); double b = 0.5 * (ed.Y - st.Y); List<System.Windows.Point> pointList = new List<System.Windows.Point>(); for (double r = 0; r <= 2 * Math.PI; r = r + 0.01) { pointList.Add(new System.Windows.Point(0.5 * (st.X + ed.X) + a * Math.Cos(r), 0.5 * (st.Y + ed.Y) + b * Math.Sin(r))); } return pointList; } private void InkCanvasMeasure_MouseDown(object sender, MouseButtonEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed) { iniP = e.GetPosition(inkCanvasMeasure); } } private void InkCanvasMeasure_MouseMove(object sender, MouseEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed) { // Draw square if (btnSquare.IsChecked == true) { System.Windows.Point endP = e.GetPosition(inkCanvasMeasure); List<System.Windows.Point> pointList = new List<System.Windows.Point> { new System.Windows.Point(iniP.X, iniP.Y), new System.Windows.Point(iniP.X, endP.Y), new System.Windows.Point(endP.X, endP.Y), new System.Windows.Point(endP.X, iniP.Y), new System.Windows.Point(iniP.X, iniP.Y), }; StylusPointCollection point = new StylusPointCollection(pointList); Stroke stroke = new Stroke(point) { DrawingAttributes = inkCanvasMeasure.DefaultDrawingAttributes.Clone() }; viewModel.InkStrokes.Clear(); viewModel.InkStrokes.Add(stroke); } // Draw Eclipse else if (btnEllipse.IsChecked == true) { System.Windows.Point endP = e.GetPosition(inkCanvasMeasure); List<System.Windows.Point> pointList = GenerateEclipseGeometry(iniP, endP); StylusPointCollection point = new StylusPointCollection(pointList); Stroke stroke = new Stroke(point) { DrawingAttributes = inkCanvasMeasure.DefaultDrawingAttributes.Clone() }; viewModel.InkStrokes.Clear(); viewModel.InkStrokes.Add(stroke); } } } } }
ViewModel.cs代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Ink; namespace WPF_InkCanvas { class ViewModel : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName = null) { if (PropertyChanged != null) PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName)); } private string meaInfo; public string MeaInfo { get => meaInfo; set { meaInfo = value; OnPropertyChanged("MeaInfo"); } } private StrokeCollection inkStrokes; public StrokeCollection InkStrokes { get { return inkStrokes; } set { inkStrokes = value; OnPropertyChanged("InkStrokes"); } } } }
补充说明:为什么要注释掉画笔属性//FitToCurve = true,可以自行体会下不注释会是个什么效果;将InkCanvas的Strokes绑定到变量有好处,在别的窗口也能获取到这个对象的哦,因为它是在viewModel里的,传viewModel参数就可以了;椭圆绘制完成后设置InkCanvas的EdittingMode为Select就可以修改大小和形状。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
上一篇:C#中的协变与逆变深入讲解
栏 目:C#教程
本文标题:WPF InkCanvas绘制矩形和椭圆
本文地址:https://www.xiuzhanwang.com/a1/C_jiaocheng/4993.html
您可能感兴趣的文章
- 01-10WinForm绘制圆角的方法
- 01-10C#及WPF获取本机所有字体和颜色的方法
- 01-10WPF实现类似360安全卫士界面的程序源码分享
- 01-10C#绘制曲线图的方法
- 01-10WPF实现时钟特效
- 01-10超炫酷的WPF实现Loading控件效果
- 01-10C#利用GDI绘制常见图形和文字
- 01-10VS中模仿WPF模板创建最简单的WPF程序
- 01-10在WPF中动态加载XAML中的控件实例代码
- 01-10WPF的ListView控件自定义布局用法实例


阅读排行
本栏相关
- 01-10C#通过反射获取当前工程中所有窗体并
- 01-10关于ASP网页无法打开的解决方案
- 01-10WinForm限制窗体不能移到屏幕外的方法
- 01-10WinForm绘制圆角的方法
- 01-10C#实现txt定位指定行完整实例
- 01-10WinForm实现仿视频 器左下角滚动新
- 01-10C#停止线程的方法
- 01-10C#实现清空回收站的方法
- 01-10C#通过重写Panel改变边框颜色与宽度的
- 01-10C#实现读取注册表监控当前操作系统已
随机阅读
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 01-10C#中split用法实例总结
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 01-10SublimeText编译C开发环境设置
- 01-11ajax实现页面的局部加载
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 04-02jquery与jsp,用jquery
- 01-10delphi制作wav文件的方法