C#,WPF,Visual Studio 4:不规则形状的颜色填充

时间:2010-07-06 04:11:08

标签: c# wpf windows visual-studio paint

我正在使用C#,WPF和Visual Studio 4创建一个Paint应用程序。就像MS Paint一样,用户可以绘制线条和形状。

如何用彩色填充用户绘制的不规则形状?做这样的事情有没有图书馆?检测由线创建的闭环似乎是不切实际的方法。我可以想象由于一个像素间隙造成的各种“泄漏”。

感谢。

2 个答案:

答案 0 :(得分:1)

这是一个简单的SSCCE。

编辑:这可能不是您想要的,现在重新阅读问题,但也许它可以让您了解如何构建您的填充工具。至于像素完美,即使MS Paint也需要一个完整的封闭图,其他库可能会使用一定程度的抗锯齿匹配来避免“泄漏”。

XAML:

<Window x:Class="FreeformDrawing.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        MouseLeftButtonDown="Window_MouseLeftButtonDown"
        MouseLeftButtonUp="Window_MouseLeftButtonUp"
        MouseMove="Window_MouseMove"
        Title="MainWindow" Height="400" Width="400">
    <Grid>
        <Canvas x:Name="DrawingCanvas" />
    </Grid>
</Window>

C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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;

namespace FreeformDrawing
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private Polygon polygon;
        private bool isDrawing = false;

        public MainWindow()
        {
            InitializeComponent();
        }

        public void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (!isDrawing)
            {
                isDrawing = true;

                polygon = new Polygon()
                {
                    Stroke = Brushes.Black,
                    StrokeThickness = 1,
                    StrokeMiterLimit = 1,
                    StrokeLineJoin = PenLineJoin.Round,
                    StrokeStartLineCap = PenLineCap.Round,
                    StrokeEndLineCap = PenLineCap.Round
                };

                AddPoint(e.GetPosition(DrawingCanvas));
                DrawingCanvas.Children.Add(polygon);
            }
        }

        public void Window_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            isDrawing = false;

            if (polygon != null)
            {
                polygon.Points.Add(polygon.Points.First());
                polygon.Fill = Brushes.Yellow;
            }
        }

        public void Window_MouseMove(object sender, MouseEventArgs e)
        {
            if (isDrawing)
            {
                AddPoint(e.GetPosition(DrawingCanvas));
            }
        }

        private void AddPoint(Point value)
        {
            if (value.X < (DrawingCanvas.ActualWidth - 1)
            && value.Y < (DrawingCanvas.ActualHeight - 1))
            {
                polygon.Points.Add(value);
            }
        }
    }
}

答案 1 :(得分:1)

洪水填充算法看起来像是要走的路 -

http://en.wikipedia.org/wiki/Flood_fill