WPFInkCanvasは長方形と楕円を描画します



Wpf Inkcanvas Draw Rectangle



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 { /// /// Interactive logic of 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 = 'Test·····', InkStrokes = new StrokeCollection(), } DataContext = viewModel } private void OpenFile_Click(object sender, RoutedEventArgs e) { OpenFileDialog openDialog = new OpenFileDialog *.png 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) } } } } }
  • 背後にあるコード
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') } } } }
  • ViewModel.csコード

補足:brushプロパティ// FitToCurve = trueをコメントアウトする必要がある理由は、コメントしないという効果を体験できます。InkCanvasのストロークを変数にバインドすると、このオブジェクトを他のウィンドウで取得できます。これはviewModelにあるため、楕円を描画した後にviewModelパラメーターを渡すだけで、InkCanvasのEditingModeをSelectに設定して、サイズと形状を変更します。