一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - C# - WPF InkCanvas基本操作方法詳解

WPF InkCanvas基本操作方法詳解

2022-03-06 13:25有個家伙喜歡代碼 C#

這篇文章主要為大家詳細介紹了WPF InkCanvas基本的操作方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

wpf的inkcanvas就是一個畫板,可以在上面隨意涂鴉,每寫上一筆,inkcanvas的strokes集合里就新增一個涂鴉對象,下面的代碼演示了基本的操作。

效果圖

WPF InkCanvas基本操作方法詳解

xaml代碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<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>

后臺代碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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代碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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

延伸 · 閱讀

精彩推薦
  • C#Unity3D實現虛擬按鈕控制人物移動效果

    Unity3D實現虛擬按鈕控制人物移動效果

    這篇文章主要為大家詳細介紹了Unity3D實現虛擬按鈕控制人物移動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一...

    shenqingyu060520232410972022-03-11
  • C#深入解析C#中的交錯數組與隱式類型的數組

    深入解析C#中的交錯數組與隱式類型的數組

    這篇文章主要介紹了深入解析C#中的交錯數組與隱式類型的數組,隱式類型的數組通常與匿名類型以及對象初始值設定項和集合初始值設定項一起使用,需要的...

    C#教程網6172021-11-09
  • C#C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題實例

    C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題實例

    這篇文章主要介紹了C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題,簡單描述了訪問者模式的定義并結合具體實例形式分析了C#使用訪問者模式解決長...

    GhostRider9502022-01-21
  • C#WPF 自定義雷達圖開發實例教程

    WPF 自定義雷達圖開發實例教程

    這篇文章主要介紹了WPF 自定義雷達圖開發實例教程,本文介紹的非常詳細,具有參考借鑒價值,需要的朋友可以參考下...

    WinterFish13112021-12-06
  • C#C#通過KD樹進行距離最近點的查找

    C#通過KD樹進行距離最近點的查找

    這篇文章主要為大家詳細介紹了C#通過KD樹進行距離最近點的查找,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    帆帆帆6112022-01-22
  • C#C#實現XML文件讀取

    C#實現XML文件讀取

    這篇文章主要為大家詳細介紹了C#實現XML文件讀取的相關代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    Just_for_Myself6702022-02-22
  • C#C#裁剪,縮放,清晰度,水印處理操作示例

    C#裁剪,縮放,清晰度,水印處理操作示例

    這篇文章主要為大家詳細介紹了C#裁剪,縮放,清晰度,水印處理操作示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    吳 劍8332021-12-08
  • C#C# 實現對PPT文檔加密、解密及重置密碼的操作方法

    C# 實現對PPT文檔加密、解密及重置密碼的操作方法

    這篇文章主要介紹了C# 實現對PPT文檔加密、解密及重置密碼的操作方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下...

    E-iceblue5012022-02-12
主站蜘蛛池模板: 亚洲色欲色欲综合网站 | 草草草在线 | 97精品国产自在现线免费 | 国产无限免费观看黄网站 | 兽皇日本 | 从后面撕开老师的丝袜动态图 | 网站在线观看 | 国产精品久久久久久久久久久搜索 | 蜜桃成熟时1997在线看免费看 | 黑人k8经典| 欧美日韩高清观看一区二区 | 莫莉瑞典1977k | 亚洲国产情侣一区二区三区 | 国产精品欧美在线观看 | 久久亚洲精品中文字幕60分钟 | 亚洲欧洲网站 | 狠狠色狠狠色综合日日小蛇 | 国产偷啪视频一区 | 114毛片免费观看网站 | 亚洲精品一线二线三线 | 亚洲精品丝袜在线一区波多野结衣 | 国产一级毛片潘金莲的奶头 | 高清不卡日本v在线二区 | 欧亚专线欧洲m码可遇不可求 | 四虎导航 | 国产亚洲欧美日韩俺去了 | 久久91精品国产91 | 四虎网站网址 | 99精品在线免费观看 | 免费国产高清视频 | 精品无人区一区二区三区 | 国语刺激对白勾搭视频在线观看 | 欧美一区二区三区综合色视频 | 欧美特黄视频在线观看 | 日韩在线 中文字幕 | 舔比小说 | 92福利网| 范冰冰a级一级特级毛片 | 男同gay作爰视频网站 | 恩不要好大好硬好爽3p | 男生同性啪视频在线观看 |