BGRABitmap tutorial 6

Home | Tutorial 1 | Tutorial 2 | Tutorial 3 | Tutorial 4 | Tutorial 5 | Tutorial 6 | Tutorial 7 | Tutorial 8 | Tutorial 9 | Tutorial 10 | Tutorial 11 | Tutorial 12 | Tutorial 13 | Tutorial 14 | Tutorial 15 | Tutorial 16 | Edit

This tutorial shows you how to use different line styles and shapes.

Create a new project

edit

Create a new project and add a reference to BGRABitmap, the same way as in the first tutorial.

Add a painting handler

edit

With the object inspector, add an OnPaint handler and write:

procedure TForm1.FormPaint(Sender: TObject);
var image: TBGRABitmap;
    c: TBGRAPixel;
begin
  image := TBGRABitmap.Create(ClientWidth, ClientHeight, clBtnFace);
  c := clWindowText; 

  image.RectangleAntialias(80,80,300,200, c, 50);

  image.Draw(Canvas,0,0,True);
  image.free;
end;

Run the program

edit

This should draw a rectangle with a wide black pen.

Change join style

edit

If you want round corners, you can specify:

    image.JoinStyle := pjsRound;

Run the program

edit

This should draw a rectangle with a wide black pen with rounded corners.

Mix join styles

edit

You can mix join styles for a rectangle like this:

    image.FillRoundRectAntialias(80,80,300,200, 20,20, c, [rrTopRightSquare,rrBottomLeftSquare]);

This function use round corners by default, but you can override it with square corners or bevel corners. You should obtain the following image.

Change pen style

edit

You can draw a dotted line like this:

    image.JoinStyle := pjsBevel;
    image.PenStyle := psDot;
    image.DrawPolyLineAntialias([PointF(40,200), PointF(120,100), PointF(170,140), PointF(250,60)],c,10);
Note that the pen style can be defined independently of a bitmap by using the TBGRAPenStroker class of the BGRAPenhttps://github.com/bgrabitmap/bgrabitmap/blob/master/bgrabitmap/bgrapen.pas unit.

You should obtain the following image. Notice that line begins with a round cap.

Change line cap

edit

You can draw a polyline with a square cap like this:

    image.JoinStyle := pjsBevel;
    image.LineCap := pecSquare;
    image.PenStyle := psSolid;
    image.DrawPolyLineAntialias([PointF(40,200), PointF(120,100), PointF(170,140), PointF(250,60)],c,10);

Opened line drawing

edit

You can draw a line which is opened, i.e. the end of the line is rounded inside.

    image.DrawPolyLineAntialias([PointF(40,200), PointF(120,100), PointF(170,140), PointF(250,60)],c,10,False);

This way you can connect lines one after another without drawing the junction twice, which is useful with semi-transparent drawing. You can compare it like this:

    c := BGRA(0,0,0,128);

    image.DrawLineAntialias(40,150, 120,50, c, 10);
    image.DrawLineAntialias(120,50, 170,90, c, 10);
    image.DrawLineAntialias(170,90, 250,10, c, 10);

    image.DrawLineAntialias(40,250, 120,150, c, 10, False);
    image.DrawLineAntialias(120,150, 170,190, c, 10, False);
    image.DrawLineAntialias(170,190, 250,110, c, 10, True);

Previous tutorial (layers and masks) Next tutorial (splines)