System.Drawing.Drawing2D.GraphicsPath.GetBounds C# (CSharp) Method

GetBounds() public method

public GetBounds ( Matrix matrix, Pen pen ) : RectangleF
matrix Matrix
pen Pen
return RectangleF
        public RectangleF GetBounds(Matrix matrix,Pen pen)
        {
            var bounds = RectangleF.Empty;

            if (points.Count < 1) {
                return bounds;
            }

            var workPath = (GraphicsPath)Clone ();

            // We don't need a very precise flat value to get the bounds (GDI+ isn't, big time) -
            // however flattening helps by removing curves, making the rest of the algorithm a
            // lot simpler.
            // note: only the matrix is applied if no curves are present in the path
            var status = FlattenPath (workPath, matrix, 25.0f);

            if (status == 0)
            {
                int i;
                PointF boundaryPoints;

                boundaryPoints = workPath.points [0];//g_array_index (workpath->points, GpPointF, 0);
                bounds.X = boundaryPoints.X;		// keep minimum X here
                bounds.Y = boundaryPoints.Y;		// keep minimum Y here
                if (workPath.points.Count == 1) {
                    // special case #2 - Only one element
                    bounds.Width = 0.0f;
                    bounds.Height = 0.0f;
                    return bounds;
                }

                bounds.Width = boundaryPoints.X;	// keep maximum X here
                bounds.Height = boundaryPoints.Y;	// keep maximum Y here

                for (i = 1; i < workPath.points.Count; i++) {
                    boundaryPoints = workPath.points[i];
                    if (boundaryPoints.X < bounds.X)
                        bounds.X = boundaryPoints.X;
                    if (boundaryPoints.Y < bounds.Y)
                        bounds.Y = boundaryPoints.Y;
                    if (boundaryPoints.X > bounds.Width)
                        bounds.Width = boundaryPoints.X;
                    if (boundaryPoints.Y > bounds.Height)
                        bounds.Height = boundaryPoints.Y;
                }

                // convert maximum values (width/height) as length
                bounds.Width -= bounds.X;
                bounds.Height -= bounds.Y;

                if (pen != null)
                {
                    /* in calculation the pen's width is at least 1.0 */
                    float width = (pen.Width < 1.0f) ? 1.0f : pen.Width;
                    float halfw = (width / 2);

                    bounds.X -= halfw;
                    bounds.Y -= halfw;
                    bounds.Width += width;
                    bounds.Height += width;
                }
            }
            return bounds;
        }

Same methods

GraphicsPath::GetBounds ( ) : RectangleF
GraphicsPath::GetBounds ( Matrix matrix ) : RectangleF

Usage Example

示例#1
0
        public static Image GetRegionImage(Image surfaceImage, GraphicsPath regionFillPath, GraphicsPath regionDrawPath, SurfaceOptions options)
        {
            if (regionFillPath != null)
            {
                Image img;

                Rectangle regionArea = Rectangle.Round(regionFillPath.GetBounds());
                Rectangle screenRectangle = CaptureHelpers.GetScreenBounds0Based();
                Rectangle newRegionArea = Rectangle.Intersect(regionArea, screenRectangle);

                using (GraphicsPath gp = (GraphicsPath)regionFillPath.Clone())
                {
                    MoveGraphicsPath(gp, -Math.Max(0, regionArea.X), -Math.Max(0, regionArea.Y));
                    img = ImageHelpers.CropImage(surfaceImage, newRegionArea, gp);

                    if (options.DrawBorder)
                    {
                        GraphicsPath gpOutline = regionDrawPath ?? regionFillPath;

                        using (GraphicsPath gp2 = (GraphicsPath)gpOutline.Clone())
                        {
                            MoveGraphicsPath(gp2, -Math.Max(0, regionArea.X), -Math.Max(0, regionArea.Y));
                            img = ImageHelpers.DrawOutline(img, gp2);
                        }
                    }
                }

                return img;
            }

            return null;
        }
All Usage Examples Of System.Drawing.Drawing2D.GraphicsPath::GetBounds