How can I get a simple "block" of the path contour of GDI +?

Suppose I have GDI + GraphicsPath, which is relatively complex, with holes in it. The text is a good example, for example, the letter "O". I want to transform this path to completely fill it, including holes. How can i do this?

sample image

+3
source share
2 answers

Cody

I have not seen that you accepted the answer yet, so I put this C # function here so you can see if it helps. It has been tested.

: , , , , "" 2 , .

, .Net :

GraphicsPath solid = LetterPath.ToSolidPath();

- GraphicsPath, ( , ).

/// <summary>
///  Removes all subpaths (holes) from a graphics path, leaving only the largest whole path behind
/// </summary>
public static GraphicsPath ToSolidPath(this GraphicsPath path)
{
    GraphicsPath BiggestPath = null;
    GraphicsPath SubPath = new GraphicsPath();
    RectangleF BoundsRect = RectangleF.Empty;
    RectangleF BiggestRect = RectangleF.Empty;
    bool bIsClosed = false;

    var pathIterator = new GraphicsPathIterator(path);
    pathIterator.Rewind();

    for (int i = 0; i < pathIterator.SubpathCount; i++)
    {
        SubPath.Reset();
        pathIterator.NextSubpath(SubPath, out bIsClosed);
        BoundsRect = SubPath.GetBounds();
        if (BoundsRect.Width * BoundsRect.Height > BiggestRect.Width * BiggestRect.Height)
        {
            BiggestRect = BoundsRect;
            BiggestPath = (GraphicsPath)SubPath.Clone();
        }
    }

    return BiggestPath;
}
+2

, Delphi, . , "" . , . , - :

function BlockPath(Path: IGPGraphicsPath): IGPGraphicsPath;
var
  PathIterator: IGPGraphicsPathIterator;
  SubPath: IGPGraphicsPath;
  I: Integer;
  IsClosed: Boolean;
  BiggestPath: IGPGraphicsPath;
  BiggestRect, BoundsRect: TGPRectF;
begin
  Result := TGPGraphicsPath.Create;
  SubPath := TGPGraphicsPath.Create;
  PathIterator := TGPGraphicsPathIterator.Create(Path);
  PathIterator.Rewind;
  BiggestPath := nil;
  BiggestRect.Width := 0;
  BiggestRect.Height := 0;
  for I := 0 to PathIterator.SubpathCount - 1 do
  begin
    SubPath.Reset;
    PathIterator.NextSubPath(SubPath, IsClosed);
    SubPath.GetBounds(BoundsRect);
    if (BoundsRect.Width >= BiggestRect.Width) and
     (BoundsRect.Height >= BiggestRect.Height) then
    begin
     BiggestRect := BoundsRect;
     BiggestPath := SubPath.Clone;
    end;
  end;
  if BiggestPath <> nil then
  begin
    Result.AddPath(BiggestPath, True);
  end;
end;

.

+1

Source: https://habr.com/ru/post/1783357/


All Articles