WPF InkCanvas基本操作方法详解
作者:有个家伙喜欢代码 发布时间:2023-07-29 06:26:59
标签: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",不然点击事件就没办法触发了。
来源:https://blog.csdn.net/u012366767/article/details/81265922


猜你喜欢
- 在Android原生的TextView的基础上,可收缩/扩展的TextView:PhilExpandableTextView。实现原理:核心
- 本文介绍如何在springboot中使用默认的spring cache,声明式缓存Spring 定义 CacheManager 和 Cach
- 下面先看一下效果图using UnityEngine;using System.Collections;public class textM
- 问题的起源在项目里,有时候需要实现一个图片轮播的效果,用来展示Banner。同时,图片能循环播放,下面还有一排小圆点来指示当前轮播到哪一页了
- 具体代码如下所示:import java.util.ArrayList;import java.util.List;import java.
- 引言使用微信时我们会发现,首次进入微信的好友列表时,会加载好友头像,但是再次进入时,就不用重新加载了,而且其他页面都不用重新加载,说明微信的
- 本文实例讲述了Android编程使用android-support-design实现MD风格对话框功能。分享给大家供大家参考,具体如下:首先
- using System;using System.Collections.Generic;using System.Web.Script.
- 使用fileupload组件的原因: Request对象提供了一个getInputStream()方法,通过这个方法可以读取到客户端提交过来
- 使用要点如下:1.利用ListAdapter(一般使用ArrayAdapter)为AutoCompleteTextView提供数据,若有需要
- 1. 数据构造索引2个文档到 hotel 索引中:PUT /hotel/_doc/1{ "title": &
- 前言在日常的测试工作过程中,app可能会出现闪退崩溃的情况,这个时候就需要测试同学快速抓取到崩溃日志,来有效的辅助开发定位问题,快速的去解决
- 1.图的遍历从图中某一顶点出发访问图中其余顶点,且每个顶点仅被访问一次图的遍历有两种深度优先遍历DFS、广度优先遍历BFS2.深度优先遍历深
- #line #line 使您可以修改编译器的行号以及(可选)错误和警告的文件名输出。下面的示例说明如何报告与行号关联的两个警告。#line
- 我想每个写项目的人,都肯定会遇到控制权限这个问题.例如这个这个链接只能管理员访问,那个链接丫只能超级管理员访问等等,实现方式也有多种多样,控
- AndroidMaifest.xml中声明权限<!-- 声明所有需要的权限(包括普通权限和危险权限) --><uses-p
- Unity打开Unity Ctrl+9,打开Unity商店,下载TexturePacker Importer插件这个插件是用来解析图集文件的
- 最近这款“跳一跳”很火,在段子里面看到有人才放了张画着坐标的纸在手机上,说根据
- 一、题目描述题目:使用ThreadLocal管理一号和二号线程,分别存入100元,在三号线程中使用利用一号和二号的计算结果来算出账户的实际金
- 本文实例为大家分享了Java实现聊天机器人完善版的具体代码,供大家参考,具体内容如下Client代码:package GUISocket.c