大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
这篇文章将为大家详细讲解有关WPF中InkCanvas基本操作方法的示例分析,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
创新互联技术团队十载来致力于为客户提供成都网站建设、成都做网站、品牌网站设计、成都全网营销推广、搜索引擎SEO优化等服务。经过多年发展,公司拥有经验丰富的技术团队,先后服务、推广了上千多家网站,包括各类中小企业、企事单位、高校等机构单位。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的位置就是对应的,方便我后续在InkCanvas上提取Image的感兴趣区域;InkCanvas里加了一个Label可以实现类似图片上添加文字说明的功能,要设置Label的IsHitTestVisible="False",不然点击事件就没办法触发了。
关于“WPF中InkCanvas基本操作方法的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。