Exemple #1
0
 public bool FillContains(Point pt)
 {
     if (this.Visibility == System.Windows.Visibility.Visible && _polygon.Points.Count > 0)
     {
         PathFigure pthFigure = new PathFigure();
         pthFigure.StartPoint = _polygon.Points[0];
         pthFigure.Segments.Add(new PolyLineSegment((IEnumerable <Point>)_polygon.Points, false));
         PathGeometry pthGeometry = new PathGeometry();
         pthGeometry.Figures.Add(pthFigure);
         if (pthGeometry.FillContains(pt))
         {
             return(true);
         }
     }
     return(false);
 }
Exemple #2
0
        private bool HitTestDrawing(GeometryDrawing drawing, Point pt)
        {
            Pen   pen   = drawing.Pen;
            Brush brush = drawing.Brush;

            if (pen != null && brush == null)
            {
                if (drawing.Geometry.StrokeContains(pen, pt))
                {
                    return(true);
                }
                else
                {
                    Geometry geometry = drawing.Geometry;

                    EllipseGeometry   ellipse   = null;
                    RectangleGeometry rectangle = null;
                    PathGeometry      path      = null;
                    if (TryCast.Cast(geometry, out ellipse))
                    {
                        if (ellipse.FillContains(pt))
                        {
                            return(true);
                        }
                    }
                    else if (TryCast.Cast(geometry, out rectangle))
                    {
                        if (rectangle.FillContains(pt))
                        {
                            return(true);
                        }
                    }
                    else if (TryCast.Cast(geometry, out path))
                    {
                        PathFigureCollection pathFigures = path.Figures;
                        int itemCount = pathFigures.Count;
                        if (itemCount == 1)
                        {
                            if (pathFigures[0].IsClosed && path.FillContains(pt))
                            {
                                return(true);
                            }
                        }
                        else
                        {
                            for (int f = 0; f < itemCount; f++)
                            {
                                PathFigure pathFigure = pathFigures[f];
                                if (pathFigure.IsClosed)
                                {
                                    PathFigureCollection testFigures = new PathFigureCollection();
                                    testFigures.Add(pathFigure);

                                    PathGeometry testPath = new PathGeometry();
                                    testPath.Figures = testFigures;

                                    if (testPath.FillContains(pt))
                                    {
                                        return(true);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else if (brush != null && drawing.Geometry.FillContains(pt))
            {
                return(true);
            }

            return(false);
        }
Exemple #3
0
 /// <summary>
 /// Test whether object contains point
 /// </summary>
 public override bool Contains(Point point)
 {
     return(pathGeometry.FillContains(point) ||
            pathGeometry.StrokeContains(new Pen(Brushes.Black, LineHitTestWidth), point));
 }
        private bool HitTestDrawing(GeometryDrawing drawing, Point pt)
        {
            Pen   pen   = drawing.Pen;
            Brush brush = drawing.Brush;

            if (pen != null)
            {
                if (drawing.Geometry.StrokeContains(pen, pt))
                {
                    return(true);
                }
                Geometry geometry = drawing.Geometry;

                LineGeometry      line      = null;
                EllipseGeometry   ellipse   = null;
                RectangleGeometry rectangle = null;
                PathGeometry      path      = null;

                if (TryCast.Cast(geometry, out path))
                {
                    if (path.FillContains(pt, 1, ToleranceType.Absolute))
                    {
                        return(true);
                    }

                    //PathFigureCollection pathFigures = path.Figures;
                    //int itemCount = pathFigures.Count;
                    //if (itemCount == 1)
                    //{
                    //    if (pathFigures[0].IsClosed && path.FillContains(pt))
                    //    {
                    //        return true;
                    //    }
                    //}
                    //else
                    //{
                    //    for (int f = 0; f < itemCount; f++)
                    //    {
                    //        PathFigure pathFigure = pathFigures[f];
                    //        if (pathFigure.IsClosed)
                    //        {
                    //            PathFigureCollection testFigures = new PathFigureCollection();
                    //            testFigures.Add(pathFigure);

                    //            PathGeometry testPath = new PathGeometry();
                    //            testPath.Figures = testFigures;

                    //            if (testPath.FillContains(pt))
                    //            {
                    //                return true;
                    //            }
                    //        }
                    //    }
                    //}
                }
                else if (TryCast.Cast(geometry, out line))
                {
                    if (line.FillContains(pt))
                    {
                        return(true);
                    }
                }
                else if (TryCast.Cast(geometry, out ellipse))
                {
                    if (ellipse.FillContains(pt))
                    {
                        return(true);
                    }
                }
                else if (TryCast.Cast(geometry, out rectangle))
                {
                    if (rectangle.FillContains(pt))
                    {
                        return(true);
                    }
                }
            }
            else if (brush != null && drawing.Geometry.FillContains(pt))
            {
                return(true);
            }
            else if (drawing.Geometry.FillContains(pt))
            {
                return(true);
            }

            return(false);
        }
Exemple #5
0
        private BitmapSource CreateRoiMask(Color background, Color roi)
        {
            // Create a PathGeometry containing all of the points
            PathGeometry path       = new PathGeometry();
            PathFigure   pathFigure = new PathFigure();

            pathFigure.IsClosed   = true;
            pathFigure.StartPoint = this.pathPoints.First();
            foreach (var point in this.pathPoints.Skip(1))
            {
                pathFigure.Segments.Add(new LineSegment(point, false));
            }
            pathFigure.Segments.Add(new LineSegment(this.pathPoints.First(), false));
            path.Figures.Add(pathFigure);

            // Calculate byte-aligned stride using 2 bits per pixel
            var pixelFormat = PixelFormats.Indexed1;
            int roiStride   = (Convert.ToInt32(this.bounds.Width * pixelFormat.BitsPerPixel) + 7) / 8;

            // Create a BitArray covering the ROI bounds using stride
            BitArray roiMask = new BitArray(Convert.ToInt32(roiStride * 8 * this.bounds.Height), false);

            // Iterate within the ROI bounds and test each coordinate against the drawn ROI
            int roiWidth  = Convert.ToInt32(this.bounds.Width);
            int roiHeight = Convert.ToInt32(this.bounds.Height);

            for (int row = 0; row < roiHeight; row++)
            {
                int rowOffset = row * roiStride * 8;
                for (int col = 0; col < roiWidth; col++)
                {
                    // Create Point relative to full image
                    var testPoint = new Point(col + this.bounds.Left, row + this.bounds.Top);

                    // Test coordinate inside the path created by pathPoints
                    if (path.FillContains(testPoint))
                    {
                        int pixelIndex = rowOffset + (col * pixelFormat.BitsPerPixel); // Each pixel is 2 bits
                        roiMask[pixelIndex] = true;
                    }
                }
            }

            // Create a WriteableBitmap with a transparent and white palette
            var palette = new BitmapPalette(new List <Color> {
                background, roi
            });
            var             imgSize    = this.AdornedImage.RenderSize;
            WriteableBitmap maskBitmap = new WriteableBitmap(Convert.ToInt32(imgSize.Width), Convert.ToInt32(imgSize.Height), 96, 96, pixelFormat, palette);

            // Convert the roiMask into a byte array
            byte[] roiMaskBuffer = new byte[roiMask.Length / 8];
            roiMask.CopyTo(roiMaskBuffer, 0);
            // Adjust the roiMaskBuffer by flipping each byte!
            for (int i = 0; i < roiMaskBuffer.Length; i++)
            {
                roiMaskBuffer[i] = ReverseByte(roiMaskBuffer[i]);
            }

            // Update the ROI with the roiMask
            var roiRect = new Int32Rect(Convert.ToInt32(this.bounds.Left), Convert.ToInt32(this.bounds.Top), roiStride * 8 / pixelFormat.BitsPerPixel, Convert.ToInt32(this.bounds.Height));

            maskBitmap.WritePixels(roiRect, roiMaskBuffer, roiStride, 0);
            maskBitmap.Freeze();
            return(maskBitmap);
        }