Just loading the image in the TImage and then writing on TImage.Picture.Bitmap.Canvas works fine.
No, it does not, drawing a line on TImage.Picture.Bitmap.Canvas results in a gray line. The line is gray because TImage expands the loaded image to 32 bpp, i.e. the image has an alpha channel now. In case of a non-transparent jpg, all alpha values are 255 (opaque). But when you draw on such an image, e.g. black (with R=0, G=0, B=0), the canvas erases the alpha byte to zero (black = $00000000 = (red=0, green=0, blue=0,
alpha=0), and alpha=0 means: transparent. You can see this, when you change the color of the form - the color of the drawn line on the image changes, too!
Maybe there are other ways, but for me the simplest way to directly draw on the TImage.Picture.Bitmap.Canvas is via a LazIntfImage:
uses
FPImage, IntfGraphics, LazCanvas;
procedure TForm1.Button4Click(Sender: TObject);
var
img: TLazIntfImage;
canv: TLazCanvas;
begin
Image1.Picture.LoadFromFile('C:\Lazarus\lazarus-main_fpc3.2.2\images\splash_source\cheetah.jpg');
img := Image1.Picture.Bitmap.CreateIntfImage;
try
canv := TLazCanvas.Create(img);
canv.pen.FPColor := colRed;
canv.Pen.Width := 5;
canv.Line(0, 0, 100, 100);
Image1.Picture.Bitmap.LoadFromIntfImage(img);
finally
canv.Free;
img.Free;
end;
end;
But since an intermediate object is already needed here, and since LazIntfImage is a bit tricky sometimes, it really is the best to load the image into a temporary bitmap, do the drawing here, and then load this bitmap into the image as already suggested by KodeZwerg in the beginning. (However, I'd transfer the bitmap via stream (rather than using Assign) because this way the transparency is preserved).