WPF DrawingContext DrawingVisual OnRender()

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.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Windows.Media.Effects;

namespace WpfApp117
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            TestDrawingVisual visual = new TestDrawingVisual();
            ((TestDrawingVisual)visual).DrawEllipseDemo();
            Image img = new Image();
            DrawingImage dImg = new DrawingImage(visual.Drawing);
            img.Source = dImg;
            this.Content = img;
        }

        public class TestDrawingVisual : DrawingVisual
        {
            public void DrawEllipseDemo()
            {
                using (DrawingContext dc = RenderOpen())
                {
                    EllipseGeometry outterEllipse = new EllipseGeometry(new Point(150, 150), 200, 200);
                    EllipseGeometry interEllipse = new EllipseGeometry(new Point(150, 150), 100, 100);
                    GeometryGroup group = new GeometryGroup();
                    group.FillRule = FillRule.EvenOdd;
                    group.Children.Add(outterEllipse);
                    group.Children.Add(interEllipse);
                    dc.DrawGeometry(Brushes.Blue, null, group);
                }
            }
        }
    }
}

 

posted @ 2024-05-28 21:22  FredGrit  阅读(17)  评论(0编辑  收藏  举报