這篇文章將為大家詳細講解有關(guān)WPF中InkCanvas基本操作方法的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
從策劃到設(shè)計制作,每一步都追求做到細膩,制作可持續(xù)發(fā)展的企業(yè)網(wǎng)站。為客戶提供成都網(wǎng)站制作、成都網(wǎng)站設(shè)計、網(wǎng)站策劃、網(wǎng)頁設(shè)計、域名注冊、雅安服務(wù)器托管、網(wǎng)絡(luò)營銷、VI設(shè)計、 網(wǎng)站改版、漏洞修補等服務(wù)。為客戶提供更好的一站式互聯(lián)網(wǎng)解決方案,以客戶的口碑塑造優(yōu)易品牌,攜手廣大客戶,共同發(fā)展進步。
WPF的InkCanvas就是一個畫板,可以在上面隨意涂鴉,每寫上一筆,InkCanvas的Strokes集合里就新增一個涂鴉對象,下面的代碼演示了基本的操作。
效果圖
xaml代碼
后臺代碼
using Microsoft.Win32; using System; using System.Collections.Generic; using System.IO; 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.Navigation; using System.Windows.Shapes; namespace WPF_InkCanvas { ////// MainWindow.xaml 的交互邏輯 /// public partial class MainWindow : Window { ViewModel viewModel; public MainWindow() { 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 = "測試······", }; DataContext = viewModel; } private void InkCanvasMeasure_MouseDown(object sender, MouseButtonEventArgs e) { } private void InkCanvasMeasure_MouseMove(object sender, MouseEventArgs e) { } 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 RadioButton_Click(object sender, RoutedEventArgs e) { if ((sender as RadioButton).Content.ToString() == "繪制墨跡") { inkCanvasMeasure.EditingMode = InkCanvasEditingMode.Ink; } else if ((sender as RadioButton).Content.ToString() == "按點擦除") { inkCanvasMeasure.EditingMode = InkCanvasEditingMode.EraseByPoint; } else if ((sender as RadioButton).Content.ToString() == "按線擦除") { inkCanvasMeasure.EditingMode = InkCanvasEditingMode.EraseByStroke; } else if ((sender as RadioButton).Content.ToString() == "選中墨跡") { inkCanvasMeasure.EditingMode = InkCanvasEditingMode.Select; } else if ((sender as RadioButton).Content.ToString() == "停止操作") { inkCanvasMeasure.EditingMode = InkCanvasEditingMode.None; } } private void SaveInkCanvas_Click(object sender, RoutedEventArgs e) { FileStream fileStream = new FileStream("inkCanvas.isf", FileMode.Create, FileAccess.ReadWrite); inkCanvasMeasure.Strokes.Save(fileStream); fileStream.Close(); } private void LoadInkCanvas_Click(object sender, RoutedEventArgs e) { FileStream fileStream = new FileStream("inkCanvas.isf", FileMode.Open, FileAccess.Read); inkCanvasMeasure.Strokes = new StrokeCollection(fileStream); fileStream.Close(); } private void CopyInkCanvas_Click(object sender, RoutedEventArgs e) { inkCanvasMeasure.CopySelection(); } private void PasteInkCanvas_Click(object sender, RoutedEventArgs e) { inkCanvasMeasure.Paste(); } } }
ViewModel.cs代碼
using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; 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"); } } } }
補充說明:將Image和InkCanvas放到一個Grid里,并且將InkCanvas的長寬綁定到Image,這樣Image和InkCanvas的位置就是對應(yīng)的,方便我后續(xù)在InkCanvas上提取Image的感興趣區(qū)域;InkCanvas里加了一個Label可以實現(xiàn)類似圖片上添加文字說明的功能,要設(shè)置Label的IsHitTestVisible="False",不然點擊事件就沒辦法觸發(fā)了。
關(guān)于“WPF中InkCanvas基本操作方法的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。