WPF InkCanvas繪制矩形和橢圓
前面說(shuō)到了InkCanvas的基本操作,這里用一個(gè)實(shí)例來(lái)說(shuō)明具體應(yīng)用:繪制矩形和橢圓。
效果圖
xaml代碼
<Window x:Class="WPF_InkCanvas.ROI_InkCanvas" 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="ROI_InkCanvas" Height="450" Width="800"> <Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition Height="auto"/> </Grid.RowDefinitions> <Image Name="imgMeasure" HorizontalAlignment="Center" Stretch="Uniform"/> <InkCanvas Name="inkCanvasMeasure" EditingMode="None" Background="Transparent" Strokes="{Binding InkStrokes, Mode=TwoWay}" 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> <StackPanel Grid.Row="1" Orientation="Horizontal"> <Button Content="OpenFile" Margin="5" HorizontalAlignment="Left" FontSize="20" Click="OpenFile_Click"/> <ToggleButton Name="btnSquare" Content="Draw Square" Margin="5" HorizontalAlignment="Left" FontSize="20" Click="DrawSquare_Click"/> <ToggleButton Name="btnEllipse" Content="Draw Ellipse" Margin="5" HorizontalAlignment="Left" FontSize="20" Click="DrawEllipse_Click"/> </StackPanel> </Grid> </Window>
后臺(tái)代碼
using Microsoft.Win32; using System; using System.Collections.Generic; 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.Shapes; namespace WPF_InkCanvas { /// <summary> /// ROI_InkCanvas.xaml 的交互邏輯 /// </summary> public partial class ROI_InkCanvas : Window { private ViewModel viewModel; private System.Windows.Point iniP; public ROI_InkCanvas() { 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 = "測(cè)試······", InkStrokes = new StrokeCollection(), }; DataContext = viewModel; } 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 DrawSquare_Click(object sender, RoutedEventArgs e) { if (btnSquare.IsChecked == true) { btnEllipse.IsChecked = false; } } private void DrawEllipse_Click(object sender, RoutedEventArgs e) { if (btnEllipse.IsChecked == true) { btnSquare.IsChecked = false; } } private List<System.Windows.Point> GenerateEclipseGeometry(System.Windows.Point st, System.Windows.Point ed) { double a = 0.5 * (ed.X - st.X); double b = 0.5 * (ed.Y - st.Y); List<System.Windows.Point> pointList = new List<System.Windows.Point>(); for (double r = 0; r <= 2 * Math.PI; r = r + 0.01) { pointList.Add(new System.Windows.Point(0.5 * (st.X + ed.X) + a * Math.Cos(r), 0.5 * (st.Y + ed.Y) + b * Math.Sin(r))); } return pointList; } private void InkCanvasMeasure_MouseDown(object sender, MouseButtonEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed) { iniP = e.GetPosition(inkCanvasMeasure); } } private void InkCanvasMeasure_MouseMove(object sender, MouseEventArgs e) { if (e.LeftButton == MouseButtonState.Pressed) { // Draw square if (btnSquare.IsChecked == true) { System.Windows.Point endP = e.GetPosition(inkCanvasMeasure); List<System.Windows.Point> pointList = new List<System.Windows.Point> { new System.Windows.Point(iniP.X, iniP.Y), new System.Windows.Point(iniP.X, endP.Y), new System.Windows.Point(endP.X, endP.Y), new System.Windows.Point(endP.X, iniP.Y), new System.Windows.Point(iniP.X, iniP.Y), }; StylusPointCollection point = new StylusPointCollection(pointList); Stroke stroke = new Stroke(point) { DrawingAttributes = inkCanvasMeasure.DefaultDrawingAttributes.Clone() }; viewModel.InkStrokes.Clear(); viewModel.InkStrokes.Add(stroke); } // Draw Eclipse else if (btnEllipse.IsChecked == true) { System.Windows.Point endP = e.GetPosition(inkCanvasMeasure); List<System.Windows.Point> pointList = GenerateEclipseGeometry(iniP, endP); StylusPointCollection point = new StylusPointCollection(pointList); Stroke stroke = new Stroke(point) { DrawingAttributes = inkCanvasMeasure.DefaultDrawingAttributes.Clone() }; viewModel.InkStrokes.Clear(); viewModel.InkStrokes.Add(stroke); } } } } }
ViewModel.cs代碼
using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Ink; 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"); } } private StrokeCollection inkStrokes; public StrokeCollection InkStrokes { get { return inkStrokes; } set { inkStrokes = value; OnPropertyChanged("InkStrokes"); } } } }
補(bǔ)充說(shuō)明:為什么要注釋掉畫(huà)筆屬性//FitToCurve = true,可以自行體會(huì)下不注釋會(huì)是個(gè)什么效果;將InkCanvas的Strokes綁定到變量有好處,在別的窗口也能獲取到這個(gè)對(duì)象的哦,因?yàn)樗窃趘iewModel里的,傳viewModel參數(shù)就可以了;橢圓繪制完成后設(shè)置InkCanvas的EdittingMode為Select就可以修改大小和形狀。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
欄 目:C#教程
下一篇:利用lambda表達(dá)式樹(shù)優(yōu)化反射詳解
本文標(biāo)題:WPF InkCanvas繪制矩形和橢圓
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/4993.html
您可能感興趣的文章
- 01-10WinForm繪制圓角的方法
- 01-10C#及WPF獲取本機(jī)所有字體和顏色的方法
- 01-10WPF實(shí)現(xiàn)類似360安全衛(wèi)士界面的程序源碼分享
- 01-10C#繪制曲線圖的方法
- 01-10WPF實(shí)現(xiàn)時(shí)鐘特效
- 01-10超炫酷的WPF實(shí)現(xiàn)Loading控件效果
- 01-10C#利用GDI繪制常見(jiàn)圖形和文字
- 01-10VS中模仿WPF模板創(chuàng)建最簡(jiǎn)單的WPF程序
- 01-10在WPF中動(dòng)態(tài)加載XAML中的控件實(shí)例代碼
- 01-10WPF的ListView控件自定義布局用法實(shí)例


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過(guò)反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁(yè)無(wú)法打開(kāi)的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過(guò)重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-11Mac OSX 打開(kāi)原生自帶讀寫NTFS功能(圖文
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法