Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7516177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:03:09+00:00 2026-05-30T01:03:09+00:00

I have an application where I draw polygons on inkCanvas. I would like to

  • 0

I have an application where I draw polygons on inkCanvas. I would like to add a function where after clicking one of drawn polygons it would be in editing mode and then I could change some of this proporties for example Fill.

I wrote this code but it selects all area from top left of inkcanvas to the end of my polygon but I need only polygon area.

Xaml:

<DockPanel>
    <ToolBarTray DockPanel.Dock="Left" Orientation="Vertical" IsLocked="True">
        <ToolBar Padding="2">
            <RadioButton x:Name="rbDraw" IsChecked="False"
                    ToolTip="Add Rectangle" Margin="3" Checked="rbDraw_Checked">
                <Rectangle Width="20" Height="12" Stroke="Blue"
                    Fill="LightBlue" />
            </RadioButton>
            <RadioButton x:Name="rbSelect" IsChecked="False"
                ToolTip="Select" Margin="3">
                <Path Stroke="Blue" Fill="LightBlue" Width="20" Height="20">
                    <Path.Data>
                        <PathGeometry Figures="M5,15L 10,0 15,15 12,15 12,20 8,20 8,15Z">
                            <PathGeometry.Transform>
                                <RotateTransform CenterX="10" CenterY="10" Angle="45"/>
                            </PathGeometry.Transform>
                        </PathGeometry>
                    </Path.Data>
                </Path>
            </RadioButton>
        </ToolBar>
    </ToolBarTray>
    <Border BorderThickness="1" BorderBrush="Black">
        <InkCanvas x:Name="canvas1" MouseMove="canvas1_MouseMove" PreviewMouseLeftButtonDown="canvas1_PreviewMouseLeftButtonDown" EditingMode="None">
        </InkCanvas>
    </Border>
</DockPanel>

Code behind

   private Polyline polyline;
    private PointCollection polylinePoints;
    private bool drawOnMove = false;
    private List<Polygon> polygons = new List<Polygon>();

    public MainWindow()
    {
        InitializeComponent();
    }

    private void canvas1_MouseMove(object sender, MouseEventArgs e)
    {
        if (drawOnMove && (bool)rbDraw.IsChecked)
        {
            polyline.Points = polylinePoints.Clone();
            polyline.Points.Add(e.GetPosition(canvas1));
        }
    }

    private void canvas1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        if (rbDraw.IsChecked ?? false)
        {
            if (e.OriginalSource is Ellipse)
            {
                canvas1.Children.Remove((Ellipse)e.OriginalSource);
                canvas1.Children.Remove(polyline);
                Polygon tmpPolygon = new Polygon();
                tmpPolygon.StrokeThickness = 2;
                tmpPolygon.Stroke = Brushes.Black;
                tmpPolygon.Points = polylinePoints.Clone();
                polylinePoints.Clear();

                polygons.Add(tmpPolygon);
                drawOnMove = false;
                rbDraw.IsChecked = false;
                tmpPolygon.Fill = Brushes.Gray;
                canvas1.Children.Add(tmpPolygon);
                rbSelect.IsChecked = true;

            }
            else
            {
                polylinePoints.Add(e.GetPosition(canvas1));
                polyline.Points = polylinePoints.Clone();

                if (polyline.Points.Count == 1)
                {
                    Ellipse el = new Ellipse();
                    el.Width = 10;
                    el.Height = 10;
                    el.Stroke = Brushes.Black;
                    el.StrokeThickness = 2;
                    el.Fill = new SolidColorBrush { Color = Colors.Yellow };
                    el.Margin =
                        new Thickness(left: polyline.Points[0].X - el.Width / 2, top: polyline.Points[0].Y - el.Height / 2, right: 0, bottom: 0);
                    canvas1.Children.Add(el);
                }

                drawOnMove = true;
            }
        }
        else if (rbSelect.IsChecked ?? false) 
        {
            if (e.OriginalSource is Polygon)
            {
                Polygon pol = (Polygon)e.OriginalSource;

                canvas1.Select(new UIElement[] { pol });
            }
        }
    }

    private void rbDraw_Checked(object sender, RoutedEventArgs e)
    {
        polyline = new Polyline();
        polylinePoints = new PointCollection();
        polyline.StrokeThickness = 2;
        polyline.Stroke = Brushes.Black;
        canvas1.Children.Add(polyline);
    }

Edit: I edited my code my first sample was a little too general. Selecting polygon looks like this but I want to select only polygon area.

Example

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T01:03:11+00:00Added an answer on May 30, 2026 at 1:03 am

    Ok I solved my problem I added a custom Dependency Property to my Window which holds selected polygon. To show that polygon is selected I change its opacity.

     public static readonly DependencyProperty SelectedShapeProperty =
           DependencyProperty.Register
           ("SelectedShape", typeof(Polygon), typeof(MainWindow));
    
        public Polygon Polygon 
        {
            set{SetValue(SelectedShapeProperty, value);}
            get{return (Polygon) GetValue(SelectedShapeProperty);}
        }
    
    
     private void canvas1_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (rbDraw.IsChecked ?? false)
            {
                if (e.OriginalSource is Ellipse)
                {
                    canvas1.Children.Remove((Ellipse)e.OriginalSource);
                    canvas1.Children.Remove(polyline);
                    Polygon tmpPolygon = new Polygon();
                    tmpPolygon.StrokeThickness = 2;
                    tmpPolygon.Stroke = Brushes.Black;
                    tmpPolygon.Points = polylinePoints.Clone();
                    polylinePoints.Clear();
    
                    polygons.Add(tmpPolygon);
                    drawOnMove = false;
                    rbDraw.IsChecked = false;
                    tmpPolygon.Fill = Brushes.Gray;
                    canvas1.Children.Add(tmpPolygon);
                    rbSelect.IsChecked = true;
    
                }
                else
                {
                    polylinePoints.Add(e.GetPosition(canvas1));
                    polyline.Points = polylinePoints.Clone();
    
                    if (polyline.Points.Count == 1)
                    {
                        Ellipse el = new Ellipse();
                        el.Width = 10;
                        el.Height = 10;
                        el.Stroke = Brushes.Black;
                        el.StrokeThickness = 2;
                        el.Fill = new SolidColorBrush { Color = Colors.Yellow };
                        InkCanvas.SetLeft(el, polyline.Points[0].X - el.Width / 2);
                        InkCanvas.SetTop(el, polyline.Points[0].Y - el.Height / 2);
    
                        el.Margin =
                            new Thickness(left: polyline.Points[0].X - el.Width / 2, top: polyline.Points[0].Y - el.Height / 2, right: 0, bottom: 0);
                        canvas1.Children.Add(el);
                    }
    
                    drawOnMove = true;
                }
            }
            else if (rbSelect.IsChecked ?? false)
            {
                if (e.OriginalSource is Polygon && Polygon == null)
                {
                    Shape s = (Shape)e.OriginalSource;
                    Polygon = (Polygon)s;
                    Polygon.Opacity = 0.75;
                }
                else if (e.OriginalSource is Polygon && Polygon != null)
                {
                    Polygon.Opacity = 1;
                    Polygon = null;
                    Shape s = (Shape)e.OriginalSource;
                    Polygon = (Polygon)s;
                    Polygon.Opacity = 0.75;
                }
                else if (Polygon != null)
                {
                    Polygon.Opacity = 1;
                    Polygon = null;
                }
            }
            else
            {
                if(Polygon != null)
                    Polygon = null;
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application wherein I would like a function to be executed in
I have an application that uses imagefilledellipse method (http://php.net/manual/en/function.imagefilledellipse.php) to draw plot points (circles)
I have a model called newbie. The routes.rb file looks like: Myapp::Application.routes.draw do resources
I have the following code in my Rails application's routes file: MyApp::Application.routes.draw do constraints
I have integrate draw line in my application i have not used OpenGL or
I have a Flex application in which I draw a rectangle using: <s:Rect height=20
I have a simple iPhone application which uses OpenGL ES (v1) to draw a
Hi I am working on an application where i have to draw something. When
I have an application which uses the canvas to draw the scribbling done by
I have an application that uses the mapview-overlay-manager code to draw map markers on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.