0

我正在为 Delphi 使用 Mitov 的 IGDIPlus 库。在完全无辜的情况下,我得到了异常“通用错误”。我有五个重叠的矩形,想用它aPath.outline()来查找轮廓路径(我在 Win7 上运行此示例,应用了所有与轮廓相关的补丁)。

uses
System.Types, IGDIPlus, VCL.IGDIPlusExt;


procedure TForm1.FormPaint(Sender: TObject);
var
  AGraphics       : IGPGraphics;
  APath,aPathO    : IGPGraphicsPath;

begin
  AGraphics := TIGPGraphics.Create( Canvas );
  AGraphics.SmoothingMode := SmoothingModeAntiAlias;
  AGraphics.TextRenderingHint := TextRenderingHintAntiAlias;
  aPath := TIGPGraphicsPath.create() ;
  // this path gives generic error 
  aPath.addRectangle(73,201,108,96);
// --------the difference----------
  aPath.addRectangle(73,292,58,96);
// --------the difference----------
  aPath.addRectangle(73,383,108,96);
  aPath.addRectangle(177,201,108,96);
  aPath.addRectangle(177,383,108,96);

  try
    aPathO := apath.clone().Outline();
    AGraphics.DrawPath(TIGPPen.Create( aclRed,1),
      aPathO);
  except
    on E: Exception do
    begin
      AGraphics.DrawStringF( e.message,
                            TIGPFont.Create( 'Microsoft Sans Serif', 16, [ fsBold ] ),
                            TPointF.Create( 23, 23 ),
                            TIGPSolidBrush.Create( aclRed ) )   ;

      AGraphics.DrawPath(TIGPPen.Create( aclRed,1),
        aPath);
    end;
  end;
  aPath.reset();
// this path works fine
  aPath.addRectangle(373,201,108,96);
// --------the difference----------
  aPath.addRectangle(373,292,108,96);    
// --------the difference----------
  aPath.addRectangle(373,383,108,96);
  aPath.addRectangle(477,201,108,96);
  aPath.addRectangle(477,383,108,96);

  try
    aPathO := apath.clone().Outline();
    AGraphics.DrawPath(TIGPPen.Create( aclRed,1),
      aPathO);
    AGraphics.DrawStringF( 'it works',
                            TIGPFont.Create( 'Microsoft Sans Serif', 16, [ fsBold ] ),
                            TPointF.Create( 423, 23 ),
                            TIGPSolidBrush.Create( aclRed ) )   ;

  except
    on E: Exception do
    begin
      AGraphics.DrawStringF( e.message,
                            TIGPFont.Create( 'Microsoft Sans Serif', 16, [ fsBold ] ),
                            TPointF.Create( 223, 23 ),
                            TIGPSolidBrush.Create( aclRed ) )   ;

      AGraphics.DrawPath(TIGPPen.Create( aclRed,1),
        aPath);
    end;
  end;
end;

欢迎任何想法,建议。

在 C# 中运行相同的示例会产生相同的错误(在 .Net 中使用带有 GDI+ 的 Outline 路径



    GraphicsPath aPath = new GraphicsPath();
                aPath.AddRectangle(new Rectangle(73, 201, 108, 96));
                aPath.AddRectangle(new Rectangle(73, 292, 58, 96));
                aPath.AddRectangle(new Rectangle(73, 383, 108, 96));
                aPath.AddRectangle(new Rectangle(177, 201, 108, 96));
                aPath.AddRectangle(new Rectangle(177, 383, 108, 96));

                HandleRef handle = new HandleRef(aPath, (IntPtr)aPath.GetType().GetField("nativePath", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(aPath));
                try
                {

                    int status = GdipWindingModeOutline(handle, IntPtr.Zero, 0.25F);
                    using (Pen outlinePen = new Pen(Color.FromArgb(255, Color.Red), 2))
                    {
                        g.DrawPath(outlinePen, aPath);
                    }
                }

                catch (Exception exp)
                { ...
                }  

GdipWindingModeOutline函数返回状态=1(一般错误)

因为我确实需要使用轮廓曲线,而直接的方式aPath.clone().outline()是由于gdi +错误不可能,我不得不去arround。合适的解决方案 - 也许不是最好的,但有效 - 可能是:



        procedure TForm1.FormPaint(Sender: TObject);
        var
          AGraphics       : IGPGraphics;
          APath,aPathO    : IGPGraphicsPath;
          aRegion         : IGPRegion;
          aScan           :  TArray;
          aMatrix         : IGPmatrix;

        begin
          AGraphics := TIGPGraphics.Create( Canvas );
          AGraphics.SmoothingMode := SmoothingModeAntiAlias;
          AGraphics.TextRenderingHint := TextRenderingHintAntiAlias;
          aPath := TIGPGraphicsPath.create() ;
          aRegion := TIGPRegion.create().MakeEmpty();
          aRegion.Union(  TIGPRect.create(73,201,108,96));
          aRegion.Union(  TIGPRect.create(73,292,58,96));
          aRegion.Union(  TIGPRect.create(73,383,108,96));
          aRegion.Union(  TIGPRect.create(177,201,108,96));
          aRegion.Union(  TIGPRect.create(177,383,108,96));
          aMatrix := TIGPMatrix.Create();
          aScan := lRegion.GetRegionScans(aMatrix);
          for i  := 0 to High(aScan) do
            aPath.addRectangle( GPInflateRect( aScan[i], 1));


          aPathO := aPath.Clone().Outline();
          AGraphics.DrawPath(TIGPPen.Create( aclRed,1), aPathO);

      end;

4

1 回答 1

0

AGraphics.DrawPath(TIGPPen.Create( aclRed,1), ...如果你使用接口(这个库显然是这样做的),你的行在Delphi 世界中是危险的。Delphi 在函数调用中的引用计数和对象创建方面存在一些问题(我不记得是哪个...).. 尽量不要在函数调用中创建对象,而是定义一个新的局部变量来保存参考接口。

于 2016-04-22T14:04:23.320 回答