Draw polygon on Image

delphi, delphi-xe2

Solution

You might superimpose a Paintbox over your image and use a code like this

unit Unit3;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls;

type
  TPointArray=array of TPoint;
  TForm3 = class(TForm)
    Image1: TImage;
    PaintBox1: TPaintBox;
    Button1: TButton;
    procedure PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure PaintBox1Paint(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  private
    { Private-Deklarationen }
    FPointArray:TPointArray;
  public
    { Public-Deklarationen }
  end;
var
  Form3: TForm3;
implementation
{$R *.dfm}

procedure TForm3.Button1Click(Sender: TObject);
begin
   PaintBox1.Visible := false;
   Image1.Canvas.Polygon(FPointArray);
end;

procedure TForm3.PaintBox1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
   SetLength(FPointArray,Length(FPointArray)+1);
   FPointArray[High(FPointArray)].X := X;
   FPointArray[High(FPointArray)].Y := Y;
   Paintbox1.Invalidate;
end;

procedure TForm3.PaintBox1Paint(Sender: TObject);
var
 i:Integer;
begin
  PaintBox1.Canvas.Brush.Style := bsClear; //as suggested by TLama
  PaintBox1.Canvas.Polygon(FPointArray);
  for I := 0 to High(FPointArray) do
      begin
        PaintBox1.Canvas.TextOut(FPointArray[i].X-5,FPointArray[i].y-5,IntToStr(i));
      end;
end;

end.

Problem

I've been searching this quite a while but couldn't get the answer. I want to draw a polygon on an image, but I want to do this with by creating points; With the `MouseCursor` create this specific points, and with a button draw a line along these points; I found this: ``` var Poly: array of TPoint; begin // Allocate dynamic array of TPoint SetLength(Poly, 6); // Set array elements Poly[0] := Point(10, 10); Poly[1] := Point(30, 5); Poly[2] := Point(100, 20); Poly[3] := Point(120, 100); Poly[4] := Point(50, 120); Poly[5] := Point(10, 60); // Pass to drawing routine Canvas.Polygon(Poly); // Redim if needed SetLength(Poly, 7); Poly[6] := Point(1, 5); // Pass to drawing routine Canvas.Polygon(Poly); end; ``` This is what I want, but the difference is the `Point[1]`, `Point[2]`, etc is given by the user with a `MouseEvent`.

Original source

Related problems