栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

WPF InkCanvas绘制矩形和椭圆

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

WPF InkCanvas绘制矩形和椭圆

前面说到了InkCanvas的基本操作,这里用一个实例来说明具体应用:绘制矩形和椭圆。

效果图

xaml代码


 
  
   
   
  
  
  
   
  
   

后台代码

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
{
 /// 
 /// ROI_InkCanvas.xaml 的交互逻辑
 /// 
 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 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 pointList = new List();
   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 pointList = new List
     {
      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 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就可以修改大小和形状。

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/122761.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号