WPF使用DrawingContext實(shí)現(xiàn)簡(jiǎn)單繪圖
WPF 使用DrawingContext繪圖,指定忽略一個(gè)區(qū)域,比如繪制同心圓扣除內(nèi)圓范圍
實(shí)現(xiàn)代碼
using System; using System.Windows; using System.Windows.Media; public class MyDrawingVisual : DrawingVisual { public void DrawEllipseWithHole() { using (DrawingContext drawingContext = RenderOpen()) { // 創(chuàng)建外部大圓的EllipseGeometry EllipseGeometry outerEllipse = new EllipseGeometry(new Point(150, 150), 200, 200); // 創(chuàng)建內(nèi)部小圓的EllipseGeometry EllipseGeometry innerEllipse = new EllipseGeometry(new Point(150, 150), 100, 100); // 創(chuàng)建一個(gè)GeometryGroup,并將外部大圓和內(nèi)部小圓添加到其中 GeometryGroup geometryGroup = new GeometryGroup(); geometryGroup.FillRule = FillRule.EvenOdd; // 設(shè)置FillRule為EvenOdd以處理排除區(qū)域 geometryGroup.Children.Add(outerEllipse); geometryGroup.Children.Add(innerEllipse); // 使用Geometry.Combine方法將內(nèi)部小圓的Geometry設(shè)置為Exclude模式,從而在最終的Geometry中去除中心的圓形區(qū)域 geometryGroup = new GeometryGroup(); geometryGroup.FillRule = FillRule.EvenOdd; // 設(shè)置FillRule為EvenOdd以處理排除區(qū)域 geometryGroup.Children.Add(outerEllipse); geometryGroup.Children.Add(innerEllipse); geometryGroup = Geometry.Combine(geometryGroup, innerEllipse, GeometryCombineMode.Exclude, null); // 使用GeometryGroup繪制圓環(huán) drawingContext.DrawGeometry(Brushes.Blue, null, geometryGroup); } } } public class MainWindow : Window { public MainWindow() { // 創(chuàng)建一個(gè)畫(huà)布 DrawingVisual drawingVisual = new MyDrawingVisual(); // 創(chuàng)建主窗口內(nèi)容 ((MyDrawingVisual)drawingVisual).DrawEllipseWithHole(); // 創(chuàng)建一個(gè)Image對(duì)象,用于顯示繪制的圖像 Image imageControl = new Image(); // 使用DrawingVisual對(duì)象創(chuàng)建一個(gè)DrawingImage DrawingImage drawingImage = new DrawingImage(drawingVisual.Drawing); // 將DrawingImage設(shè)置為Image控件的源 imageControl.Source = drawingImage; // 將Image添加到主窗口中 Content = imageControl; } } // 在這里調(diào)用主窗口 public class Program { [STAThread] public static void Main(string[] args) { MainWindow mainWindow = new MainWindow(); mainWindow.ShowDialog(); } }
到此這篇關(guān)于WPF使用DrawingContext實(shí)現(xiàn)簡(jiǎn)單繪圖的文章就介紹到這了,更多相關(guān)WPF DrawingContext繪圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C# 使用鼠標(biāo)點(diǎn)擊對(duì)Chart控件實(shí)現(xiàn)數(shù)據(jù)提示效果
這篇文章主要介紹了C# 使用鼠標(biāo)點(diǎn)擊對(duì)Chart控件實(shí)現(xiàn)數(shù)據(jù)提示效果,文章給予上一篇的詳細(xì)內(nèi)容做延伸介紹,需要的小伙伴可任意參考一下2022-08-08c# 實(shí)現(xiàn)康威生命游戲(細(xì)胞自動(dòng)機(jī))的示例
這篇文章主要介紹了c# 實(shí)現(xiàn)康威生命游戲(細(xì)胞自動(dòng)機(jī))的示例,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-02-02Unity使用攝像機(jī)實(shí)現(xiàn)望遠(yuǎn)鏡效果
這篇文章主要為大家詳細(xì)介紹了Unity攝使用像機(jī)實(shí)現(xiàn)望遠(yuǎn)鏡效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11c#實(shí)現(xiàn)數(shù)據(jù)同步的方法(使用文件監(jiān)控對(duì)象filesystemwatcher)
這篇文章主要介紹了C#使用文件監(jiān)控對(duì)象FileSystemWatcher實(shí)現(xiàn)數(shù)據(jù)同步,大家參考使用吧2013-12-12