To my surprise I have found out that rendering text repeatedly on a TCanvas is somehow "additive". I realise that the setting Canvas.Brush.Style:=bsClear
is the cause of the problem, but I really do need to render the text transparently and repeatedly (i.e in the OnPaint
event). After doing this the text doesn't look good.
How can I avoid that?
Here is some sample code; you can see the effect if you make several clicks on a TButton called btn1
.
procedure TForm1.btn1Click(Sender: TObject);
begin
Form1.Canvas.Brush.Style:=bsClear; //if you omit this, everything is OK.
Form1.Canvas.Font.Color:=clRed;
Form1.Canvas.Font.Name:='Times new Roman';
Form1.Canvas.Font.Style:=[fsBold];
Form1.Canvas.Font.Size:=12;
Form1.Canvas.TextOut(50,50,'www.stackoverflow.com');
end;
That is because the GDI applies some antialiasing when drawing the text, for the text to look better. This causes some pixels of the background outside the drawn text to be painted red/reddish near the text. When you next draw your text, if you don't clear the background, antialiasing causes these reddish pixels to become more red.
You would either clear the background as Arioch 'The stated in his answer, or if you really need to render the text transparently and repeatedly, you can turn off antialiasing.
TOndrej has provided a nice function in this answer for specifying text output quality. Using it, the code becomes:
begin
Form1.Canvas.Brush.Style:=bsClear;
Form1.Canvas.Font.Color:=clRed;
Form1.Canvas.Font.Name:='Times new Roman';
Form1.Canvas.Font.Style:=[fsBold];
Form1.Canvas.Font.Size:=12;
SetFontQuality(Form1.Canvas.Font, NONANTIALIASED_QUALITY); // <--
Form1.Canvas.TextOut(50,50,'www.stackoverflow.com');
end;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With