delphitimage

trying to draw over a timage


I have written a simple program (using Delphi XE7); it has a TImage and a TPaintBox (partially over the Timage). In the FormPaint procedure I am calling "BringToFront" and then drawing a (filled) rectangle. The rectangle appears under the image.

procedure TForm1.FormPaint(Sender: TObject);
begin
 with paintbox1 do
   begin
     BringToFront;
     canvas.Rectangle(0,0,width-1,height-1);
   end;
end;

This is a image of the program window:

enter image description here

I expected the rectangle to be over the image. What am I doing wrong? :)


Solution

  • You are not using PaintBox correctly. PaintBox has its own event for drawing. You should use TPaintBox.OnPaint instead of TForm.OnPaint and result will be as you wrote.

    You can also draw directly on the picture. In this case, used event TForm.OnPaint.

    You can also draw directly on the image after its picture has changed (for example, after loading). It works if used TBitmap graphics.

    procedure TForm1.button1click(Sender: TObject);
    begin
      image1.LoadFromFile('c:\temp\1.bmp')
      image1.canvas.Rectangle(0,0,100,100);
    end;