WPF InkCanvas绘制矩形和椭圆

 更新时间:2020年6月25日 11:15  点击:2494

前面说到了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就可以修改大小和形状。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。

[!--infotagslink--]

相关文章

  • c# WPF中通过双击编辑DataGrid中Cell的示例(附源码)

    这篇文章主要介绍了c# WPF中通过双击编辑DataGrid中Cell的示例(附源码),帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下...2021-03-03
  • WPF实现类似360安全卫士界面的程序源码分享

    最近在网上看到了新版的360安全卫士,感觉界面还不错,于是用WPF制作了一个,时间有限,一些具体的控件没有制作,用图片代替了。感兴趣的朋友一起跟着小编学习WPF实现类似360安全卫士界面的程序源码分享...2020-06-25
  • WPF仿三星手机充电界面实现代码

    这篇文章主要为大家详细介绍了WPF仿三星手机充电界面实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
  • C# WPF 通过委托实现多窗口间的传值的方法

    这篇文章主要介绍了C# WPF 通过委托实现多窗口间的传值的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • C#中WPF使用多线程调用窗体组件的方法

    这篇文章主要介绍了C#中WPF使用多线程调用窗体组件的方法,涉及C#中多线程的使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • WPF TextBox实现按字节长度限制输入功能

    这篇文章主要为大家详细介绍了WPF TextBox实现按字节长度限制输入功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
  • WPF InkCanvas绘制矩形和椭圆

    这篇文章主要为大家详细介绍了WPF InkCanvas绘制矩形和椭圆,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
  • OpenCV实现图像轮廓检测以及外接矩形

    这篇文章主要为大家详细介绍了OpenCV实现图像轮廓检测以及外接矩形,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-04-25
  • WPF基础教程之形状画刷与变换详解

    这篇文章主要给大家介绍了关于WPF基础教程之形状画刷与变换的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
  • WPF如何自定义TabControl控件样式示例详解

    这篇文章主要给大家介绍了关于WPF如何自定义TabControl控件样式的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。...2020-06-25
  • OpenCV实现最小外接正矩形

    这篇文章主要为大家详细介绍了OpenCV实现最小外接正矩形,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-07-21
  • python3+openCV 获取图片中文本区域的最小外接矩形实例

    这篇文章主要介绍了python3+openCV 获取图片中文本区域的最小外接矩形实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-06-03
  • Python实现图片查找轮廓、多边形拟合、最小外接矩形代码

    这篇文章主要介绍了Python实现图片查找轮廓、多边形拟合、最小外接矩形代码,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-07-14
  • WPF水珠效果按钮组的实现教程

    下面小编就为大家分享一篇WPF水珠效果按钮组的实现教程,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-09-22
  • 解析WPF实现音频文件循环顺序播放的解决方法

    本篇文章是对WPF实现音频文件循环顺序播放的方法进行了详细的分析介绍,需要的朋友参考下...2021-09-22
  • C#画圆角矩形的方法

    这篇文章主要介绍了C#画圆角矩形的方法,涉及C#绘图的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • WPF实现转圈进度条效果

    这篇文章主要为大家详细介绍了WPF实现转圈进度条效果,如何设计自定义的绕圈进度条,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-22
  • 在Winform和WPF中注册全局快捷键实现思路及代码

    如果注册快捷键,RegisterHotKey中的fsModifiers参数为0,即None选项,一些安全软件会警报,可能因为这样就可以全局监听键盘而造成安全问题,感兴趣的你可以参考下本文...2020-06-25
  • WPF/Silverlight实现图片局部放大的方法分析

    这篇文章主要介绍了WPF/Silverlight实现图片局部放大的方法,结合实例形式分析了WPF/Silverlight针对图片属性操作相关实现技巧,需要的朋友可以参考下...2020-06-25
  • WPF中自定义GridLengthAnimation

    这篇文章主要为大家详细介绍了WPF中自定义GridLengthAnimation的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-22