這篇文章將為大家詳細(xì)講解有關(guān)WPF中InkCanvas基本操作方法的示例分析,小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
創(chuàng)新互聯(lián)技術(shù)團(tuán)隊(duì)十載來(lái)致力于為客戶提供成都網(wǎng)站建設(shè)、成都做網(wǎng)站、品牌網(wǎng)站設(shè)計(jì)、成都全網(wǎng)營(yíng)銷推廣、搜索引擎SEO優(yōu)化等服務(wù)。經(jīng)過(guò)多年發(fā)展,公司擁有經(jīng)驗(yàn)豐富的技術(shù)團(tuán)隊(duì),先后服務(wù)、推廣了上千多家網(wǎng)站,包括各類中小企業(yè)、企事單位、高校等機(jī)構(gòu)單位。WPF的InkCanvas就是一個(gè)畫板,可以在上面隨意涂鴉,每寫上一筆,InkCanvas的Strokes集合里就新增一個(gè)涂鴉對(duì)象,下面的代碼演示了基本的操作。
效果圖
xaml代碼
后臺(tái)代碼
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 = "測(cè)試······", }; 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() == "按點(diǎn)擦除") { 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"); } } } }
補(bǔ)充說(shuō)明:將Image和InkCanvas放到一個(gè)Grid里,并且將InkCanvas的長(zhǎng)寬綁定到Image,這樣Image和InkCanvas的位置就是對(duì)應(yīng)的,方便我后續(xù)在InkCanvas上提取Image的感興趣區(qū)域;InkCanvas里加了一個(gè)Label可以實(shí)現(xiàn)類似圖片上添加文字說(shuō)明的功能,要設(shè)置Label的IsHitTestVisible="False",不然點(diǎn)擊事件就沒(méi)辦法觸發(fā)了。
關(guān)于“WPF中InkCanvas基本操作方法的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。