2

I am trying to convert a delphi XE4 application to use the Graphics32 libraries for drawing rather than the standard delphi drawing methods.

One thing I do is draw an icon that contains a small ellipse with diagonal cross hatch pattern. The icon should look like this:

enter image description here

Here is how I do it with standard TCanvas drawing methods:

ACanvas.Brush.Color := shape.pcolor;
ACanvas.Brush.Style := bsdiagCross;
ACanvas.Ellipse(-13, -9, 13, 9);

I can draw an ellipse with Graphics32 doing the following:

var    
  Polygon : TArrayOfFloatPoint;   
begin    
  Polygon := Ellipse(0, 0, 13, 9);
  PolylineFS(Bitmap, Polygon, pcolor, True, UAVPenWidth);

but is there an easy way to replicate the diagonal cross hatching pattern? I assume I can use TBitmapPolygonFiller class but this is to fill using a bitmap. Note that this drawing is to a TPositionedLayer in it's OnPaint event handler if that is relevant.

4

2 回答 2

3

到目前为止,Graphics32 中没有直接的模式支持,但是有几十种方法可以创建您想要使用的模式。

这是使用示例多边形填充的一种解决方案:

首先,您需要为阴影图案编写一个采样器类。有几种方法可以构建这样的采样器。下面你可以找到一个非常简单的:

type
  THatchedPatternSampler = class(TCustomSampler)
  public
    function GetSampleInt(X, Y: Integer): TColor32; override;
  end;

function THatchedPatternSampler.GetSampleInt(X, Y: Integer): TColor32;
begin
  Result := 0;
  if ((X - Y) mod 8 = 0) or ((X + Y) mod 8 = 0) then
    Result := clRed32
end;

您只需在此处重写一个方法(GetSampleInt),所有其他方法都可以从祖先类中使用。

现在它变得有点复杂了。为了使用示例,您必须在 TSamplerFiller 上使用它,如下所示:

Sampler := THatchedPatternSampler.Create;
Filler := TSamplerFiller.Create(Sampler);

一旦你有了这个填充物,你就可以在 PolygonFS 甚至 PolylineFS 中使用它。

最后代码可能如下所示:

var
  Polygon: TArrayOfFloatPoint;
  Sampler: THatchedPatternSampler;
  Filler: TSamplerFiller;
begin
  Polygon := Ellipse(128, 128, 120, 100);
  Sampler := THatchedPatternSampler.Create;
  try
    Filler := TSamplerFiller.Create(Sampler);
    try
      PolygonFS(PaintBox32.Buffer, Polygon, Filler);
    finally
      Filler.Free;
    end;
      finally
    Sampler.Free;
  end;

  PolylineFS(PaintBox32.Buffer, Polygon, clRed32, True, 1);
end;

这将在位图的中心(这里:TPaintBox32 实例的缓冲区)绘制一个相当大的椭圆,并用阴影采样器代码填充它。最后使用 PolylineFS 函数绘制一个实心轮廓。

从性能的角度来看,这不是最快的方法,因为 GetSampleInt 是按像素调用的。但是,最容易理解发生了什么。

要获得更快的替代方案,您应该直接使用填充剂。您可以像这样直接从 TCustomPolygonFiller 派生:

type
  THatchedPatternFiller = class(TCustomPolygonFiller)
  private
    procedure FillLine(Dst: PColor32; DstX, DstY, Length: Integer; AlphaValues: PColor32);
  protected
    function GetFillLine: TFillLineEvent; override;
  end;

GetFillLine 方法变得如此简单:

function THatchedPatternFiller.GetFillLine: TFillLineEvent;
begin
  Result := FillLine;
end;

但是,FillLine 方法会稍微复杂一些,如下所示:

procedure THatchedPatternFiller.FillLine(Dst: PColor32; DstX, DstY,
  Length: Integer; AlphaValues: PColor32);
var
  X: Integer;
begin
  for X := DstX to DstX + Length do
  begin
    if ((X - DstY) mod 8 = 0) or ((X + DstY) mod 8 = 0) then
      Dst^ :=clRed32
    else
      Dst^ := 0;

    Inc(Dst);
  end;
end;

由于 DstY 保持不变,您还可以重构代码以提高性能。或者您可以使用汇编程序 (SSE) 加速代码,但我想这对于这样一个简单的函数来说太过分了。

于 2017-07-27T16:41:32.790 回答
-1

我尝试了上述自定义填充,但结果出乎意料。U 形多边形的两条腿之间的区域被填充。任何关于我正在做的事情的意见将不胜感激。

procedure TForm1.Button1Click(Sender: TObject);
var Filler2: THatchedPatternFiller;
    Polygon: TArrayOfFloatPoint;
begin
  polygon := [floatpoint(100, 10), floatpoint(200, 10), floatpoint(200, 400), floatpoint(300, 400), floatpoint(300, 10), floatpoint(400, 10), floatpoint(400, 500), floatpoint( 100, 500), floatpoint( 100, 10)]; // U shaped polygon
  filler2 := THatchedPatternFiller.Create;
  PolygonFS(PreviewImage.Bitmap, polygon, filler2);   // Wrong, red fill inside U share
  PolygonFS(PreviewImage.Bitmap, polygon, clGreen32); // Works fine, green fill
  Filler2.Free;
end;

填充错误的图像,红色部分在中心和右侧不可见

于 2018-06-28T17:28:13.667 回答