none
使用DrawingGroup的DrawingContext绘图后,如何清除指定区域中的图形 RRS feed

  • 问题

  • 我使用DrawingGroup的DrawingContext绘图后,包括直线,几何图形,图片,比如这个容器有1000*1000像素,我现在要清除指定区域中的图形,这个区域比如说有起点为(100,100)大小为100*100的区域,里面可能有各种已经画好的几何图形(直线,图片)的一部分,改怎么清除啊?

    var CurrentDrawingGroup = new DrawingGroup();
    using (DrawingContext dc = CurrentDrawingGroup.Open())
                {
     dc.DrawLine(CurrentPen, new Point(10, 110), new Point(530, 112));
     dc.DrawLine(CurrentPen, new Point(10, 120), new Point(530, 132));
     dc.DrawLine(CurrentPen, new Point(10, 130), new Point(530, 122));
    }
    
    上面画完了,都会经过要清除的区域,改怎么实现?

    2013年9月5日 7:17

答案

  • 您好!

    通过您的描述,我了解到您想清除掉指定区域中绘制的图像。受Tina的想法启发,我创建了一个类似的项目在我的电脑上,思路和Tina的一样。鼠标按下后获取第一个Point,当释放鼠标后获取另一个Point,这样通过这两个点,我们可以在Image上绘制出一个Rectangle,将这个Rectangle的画笔和Brush都设置为White,这样就可以达到清除指定区域的效果了。

    下面是一些代码。请参考:

    XAML Code:

    <Window x:Class="WpfDrawing.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525" MouseDown="Window_MouseDown" MouseUp="Window_MouseUp">
        <Grid>
            <Image x:Name="Container"></Image>
        </Grid>
    </Window>

    Code Behind:

    public partial class MainWindow : Window
        {
            Point px;
            Point py;
            public MainWindow()
            {
                InitializeComponent();
                DrawingLines();
            }
    
            public void DrawingLines()
            {
                Pen CurrentPen = new Pen(Brushes.Black, 2);
                
                DrawingGroup dGroup = new DrawingGroup();
    
                using (DrawingContext dc = dGroup.Open())
                {
                    dc.DrawLine(CurrentPen, new Point(10, 110), new Point(530, 112));
                    dc.DrawLine(CurrentPen, new Point(10, 120), new Point(530, 132));
                    dc.DrawLine(CurrentPen, new Point(10, 130), new Point(530, 122));
                }
    
                DrawingImage dImageSource = new DrawingImage(dGroup);
                Container.Source = dImageSource;
            }
    
            public void DrawingRect()
            {
                if (px != py)
                {
                    //double width = Math.Abs(Math.Abs(px.X) - Math.Abs(px.X));
                    //double height = Math.Abs(Math.Abs(px.Y) - Math.Abs(px.Y));
                    Rect rect = new Rect(px, py);
                    Pen currentPen = new Pen(Brushes.White, 3);
    
                    DrawingGroup dGroup = new DrawingGroup();
                    using (DrawingContext dc = dGroup.Open())
                    {
                        dc.DrawRectangle(Brushes.White, currentPen, rect);
                    }
    
                    DrawingImage dImageSource = new DrawingImage(dGroup);
                    Container.Source = dImageSource;
                }
            }
    
            private void Window_MouseDown(object sender, MouseButtonEventArgs e)
            {
                px = Mouse.GetPosition(Container);
            }
    
            private void Window_MouseUp(object sender, MouseButtonEventArgs e)
            {
                py = Mouse.GetPosition(Container);
                DrawingRect();
            }
        }

    我将这个工程上传到我的SkyDrive上了,下载地址如下,

    https://skydrive.live.com/redir?resid=F6BFF8BD172F0399!152

    谢谢!


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2013年9月9日 12:13
    版主

全部回复

  • 我有一个想法不知道是否可行,我没有尝试过。

    你可以再画一个这个范围的矩形,然后将画笔设置为无色的,矩形区域内填充色也是和窗体颜色一样的,这样就是清除经过区域了呀。你觉得呢?

    2013年9月8日 12:57
  • 您好!

    通过您的描述,我了解到您想清除掉指定区域中绘制的图像。受Tina的想法启发,我创建了一个类似的项目在我的电脑上,思路和Tina的一样。鼠标按下后获取第一个Point,当释放鼠标后获取另一个Point,这样通过这两个点,我们可以在Image上绘制出一个Rectangle,将这个Rectangle的画笔和Brush都设置为White,这样就可以达到清除指定区域的效果了。

    下面是一些代码。请参考:

    XAML Code:

    <Window x:Class="WpfDrawing.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525" MouseDown="Window_MouseDown" MouseUp="Window_MouseUp">
        <Grid>
            <Image x:Name="Container"></Image>
        </Grid>
    </Window>

    Code Behind:

    public partial class MainWindow : Window
        {
            Point px;
            Point py;
            public MainWindow()
            {
                InitializeComponent();
                DrawingLines();
            }
    
            public void DrawingLines()
            {
                Pen CurrentPen = new Pen(Brushes.Black, 2);
                
                DrawingGroup dGroup = new DrawingGroup();
    
                using (DrawingContext dc = dGroup.Open())
                {
                    dc.DrawLine(CurrentPen, new Point(10, 110), new Point(530, 112));
                    dc.DrawLine(CurrentPen, new Point(10, 120), new Point(530, 132));
                    dc.DrawLine(CurrentPen, new Point(10, 130), new Point(530, 122));
                }
    
                DrawingImage dImageSource = new DrawingImage(dGroup);
                Container.Source = dImageSource;
            }
    
            public void DrawingRect()
            {
                if (px != py)
                {
                    //double width = Math.Abs(Math.Abs(px.X) - Math.Abs(px.X));
                    //double height = Math.Abs(Math.Abs(px.Y) - Math.Abs(px.Y));
                    Rect rect = new Rect(px, py);
                    Pen currentPen = new Pen(Brushes.White, 3);
    
                    DrawingGroup dGroup = new DrawingGroup();
                    using (DrawingContext dc = dGroup.Open())
                    {
                        dc.DrawRectangle(Brushes.White, currentPen, rect);
                    }
    
                    DrawingImage dImageSource = new DrawingImage(dGroup);
                    Container.Source = dImageSource;
                }
            }
    
            private void Window_MouseDown(object sender, MouseButtonEventArgs e)
            {
                px = Mouse.GetPosition(Container);
            }
    
            private void Window_MouseUp(object sender, MouseButtonEventArgs e)
            {
                py = Mouse.GetPosition(Container);
                DrawingRect();
            }
        }

    我将这个工程上传到我的SkyDrive上了,下载地址如下,

    https://skydrive.live.com/redir?resid=F6BFF8BD172F0399!152

    谢谢!


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    2013年9月9日 12:13
    版主