您好,登錄后才能下訂單哦!
這篇文章將為大家詳細講解有關WPF中InkCanvas基本操作方法的示例分析,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
WPF的InkCanvas就是一個畫板,可以在上面隨意涂鴉,每寫上一筆,InkCanvas的Strokes集合里就新增一個涂鴉對象,下面的代碼演示了基本的操作。
效果圖
xaml代碼
<Window x:Class="WPF_InkCanvas.MainWindow" 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="MainWindow" Height="450" Width="800"> <Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition Height="auto"/> <RowDefinition Height="auto"/> </Grid.RowDefinitions> <Image Name="imgMeasure" HorizontalAlignment="Center" Stretch="Uniform"/> <InkCanvas Name="inkCanvasMeasure" EditingMode="None" Background="Transparent" 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> <Grid Grid.Row="1"> <Grid.ColumnDefinitions> <ColumnDefinition/> <ColumnDefinition/> <ColumnDefinition/> <ColumnDefinition/> <ColumnDefinition/> </Grid.ColumnDefinitions> <RadioButton Grid.Column="0" Content="繪制墨跡" Click="RadioButton_Click"/> <RadioButton Grid.Column="1" Content="按點擦除" Click="RadioButton_Click"/> <RadioButton Grid.Column="2" Content="按線擦除" Click="RadioButton_Click"/> <RadioButton Grid.Column="3" Content="選中墨跡" Click="RadioButton_Click"/> <RadioButton Grid.Column="4" Content="停止操作" Click="RadioButton_Click"/> </Grid> <StackPanel Grid.Row="2" Orientation="Horizontal"> <Button Content="OpenFile" Margin="5" HorizontalAlignment="Left" FontSize="20" Click="OpenFile_Click"/> <Button Content="SaveInkCanvas" Margin="5" HorizontalAlignment="Left" FontSize="20" Click="SaveInkCanvas_Click"/> <Button Content="LoadInkCanvas" Margin="5" HorizontalAlignment="Left" FontSize="20" Click="LoadInkCanvas_Click"/> <Button Content="CopyInkCanvas" Margin="5" HorizontalAlignment="Left" FontSize="20" Click="CopyInkCanvas_Click"/> <Button Content="PasteInkCanvas" Margin="5" HorizontalAlignment="Left" FontSize="20" Click="PasteInkCanvas_Click"/> </StackPanel> </Grid> </Window>
后臺代碼
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 { /// <summary> /// MainWindow.xaml 的交互邏輯 /// </summary> 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的位置就是對應的,方便我后續在InkCanvas上提取Image的感興趣區域;InkCanvas里加了一個Label可以實現類似圖片上添加文字說明的功能,要設置Label的IsHitTestVisible="False",不然點擊事件就沒辦法觸發了。
關于“WPF中InkCanvas基本操作方法的示例分析”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。