How do I remove the white background when drawing text on Canvas via TextOut?

Good time of day!

I make a small, purposeful, image editor. This is what the image looks like in the editor: alt text On top of the TImage, lie simple TLabels, something like layers.

And here is how these layers are drawn on the image: alt text Drawing happens like this:

procedure TPrintForm.BuildPreview(aSsignTo: TImage);
    var
      Img: TBitmap;
      i: Integer;
    begin
      Img := TBitmap.Create;
      try
        Img.Assign(fSrcBitmap);
        for i := 0 to Count - 1 do
        begin
          Img.Canvas.Font := Items[i].Text.Font;
          Img.Canvas.TextOut(Items[i].Text.BoundsRect.TopLeft.X - Items[i].Text.Font.Size,
            Items[i].Text.BoundsRect.TopLeft.Y - Items[i].Text.Height -
            Items[i].Text.Font.Size, Items[i].Text.Caption);
        end;
        aSsignTo.Picture.Assign(Img);
      finally
        FreeAndNil(Img);
      end;
    end;

How do I get rid of the white background under the text when using TextOut?

Thanks.

Author: Человек_Борща, 2013-07-30

1 answers

When creating an object Img, its background will be filled in with the default background color, i.e. white. First, fill it with the same background as the main image:

Img.Canvas.Brush.Color:= clGray;
Img.Canvas.FillRect(0, 0, Img.Width, Img.Height);

Then set the transparency:

SetBkMode(Img.Picture.Bitmap.Canvas.Handle,TRANSPARENT);

Now the text will be displayed on a gray background.

 7
Author: roach1967, 2013-07-31 14:00:56