中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

WPF InkCanvas繪制矩形和橢圓

發布時間:2020-10-04 10:37:51 來源:腳本之家 閱讀:316 作者:有個家伙喜歡代碼 欄目:編程語言

前面說到了InkCanvas的基本操作,這里用一個實例來說明具體應用:繪制矩形和橢圓。

效果圖

WPF 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就可以修改大小和形狀。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

晋中市| 武安市| 大埔县| 靖州| 江华| 大英县| 余干县| 阜新| 枣阳市| 高阳县| 忻城县| 白山市| 昌乐县| 高清| 高安市| 潼南县| 渭源县| 绥棱县| 垦利县| 神农架林区| 宣威市| 沙湾县| 渭南市| 清河县| 岳普湖县| 翼城县| 灯塔市| 双辽市| 墨玉县| 比如县| 安阳市| 海宁市| 聂拉木县| 神农架林区| 青州市| 凉山| 江川县| 正宁县| 南丰县| 紫金县| 囊谦县|