欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

WPF InkCanvas基本操作方法詳解

 更新時間:2018年11月30日 11:37:42   作者:有個家伙喜歡代碼  
這篇文章主要為大家詳細介紹了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的位置就是對應的,方便我后續(xù)在InkCanvas上提取Image的感興趣區(qū)域;InkCanvas里加了一個Label可以實現類似圖片上添加文字說明的功能,要設置Label的IsHitTestVisible="False",不然點擊事件就沒辦法觸發(fā)了。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • C#編寫發(fā)送郵件組件

    C#編寫發(fā)送郵件組件

    本文給大家分享的是使用C#編寫的發(fā)送郵件的組件,非常的簡單實用,有需要的小伙伴可以參考下。
    2015-06-06
  • C# 遍歷枚舉類型的所有元素

    C# 遍歷枚舉類型的所有元素

    寫個小東西,剛好用到枚舉類型,需要顯示在DropDownList控件中。嘗試了下,用如下方法可以實現
    2013-03-03
  • C#多線程之線程鎖

    C#多線程之線程鎖

    這篇文章介紹了C#多線程中的線程鎖,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-05-05
  • Unity 修改FBX模型動畫的操作

    Unity 修改FBX模型動畫的操作

    這篇文章主要介紹了Unity 修改FBX模型動畫的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • C# 線程相關知識總結

    C# 線程相關知識總結

    這篇文章主要介紹了C# 線程相關知識,文中講解非常細致,代碼幫助大家更好的理解和學習,感興趣的朋友可以了解下
    2020-06-06
  • C# 漢字轉化拼音的簡單實例代碼

    C# 漢字轉化拼音的簡單實例代碼

    C# 漢字轉化拼音的簡單實例代碼,需要的朋友可以參考一下
    2013-04-04
  • WPF實現列表分頁控件的示例代碼

    WPF實現列表分頁控件的示例代碼

    這篇文章主要為大家詳細介紹了如何利用WPF實現列表分頁控件,文中的示例代碼講解詳細,對我們學習或工作有一定幫助,感興趣的小伙伴可以了解一下
    2022-10-10
  • c#求兩個數中最大值的方法

    c#求兩個數中最大值的方法

    這篇文章主要介紹了c#求兩個數中最大值的方法,需要的朋友可以參考下
    2014-02-02
  • 史上最簡潔C# 生成條形碼圖片思路及示例分享

    史上最簡潔C# 生成條形碼圖片思路及示例分享

    這篇文章主要介紹了史上最簡潔C# 生成條形碼圖片思路及示例分享,需要的朋友可以參考下
    2015-01-01
  • C#中調用SAPI實現語音識別的2種方法

    C#中調用SAPI實現語音識別的2種方法

    這篇文章主要介紹了C#中調用SAPI實現語音識別的2種方法,本文直接給出實現代碼,需要的朋友可以參考下
    2015-06-06

最新評論