Delphi 6:如何通过 TextOut() 方法显示大尺寸高质量文本?

发布于 2024-09-26 16:52:05 字数 298 浏览 3 评论 0原文

我有一个 TImage 组件,我使用 TCanvas.TextOut() 打印文本字符串。我将 TImage 的高度和宽度设置为较大的尺寸,如 50 像素 X (TextWidth) 像素,并将 Canvas 字体高度设置为较小的尺寸,如 48 像素。然后我将 TImage 的位图 BitBlt() 到主画布上。我在屏幕上看到的是又大又瘦的字母,而且锯齿状得很厉害。相反,我想要的是看起来光滑的粗大字母。使用 TImage/BitBlt 组合的原因是因为我需要动态调整文本大小和 alpha 混合。

将大而平滑的字母打印到 TImage 位图上的最简单方法是什么?

I have a TImage component that I print a Text string to using TCanvas.TextOut(). I set the height and width of the TImage to a large size like 50 pixels X (TextWidth) pixels, and set the Canvas font Height to something a little smaller like 48 pixels. I then BitBlt() the TImage's bitmap on to the main Canvas. What I see on the screen is big skinny letters that are terribly jagged. What I want instead are thick jumbo letters that appear smooth. The reason for using the TImage/BitBlt combo is because I need to do resizing and alpha blending of the text on the fly.

What is the easiest way for me to get big smooth letters to be printed to my TImage bitmap?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜清冷一曲。 2024-10-03 16:52:05

您是否从不显示TImage?那么你真的应该使用离屏位图。这是实现双缓冲(无闪烁渲染)的非常常见的技术。

例如,

var
  bm: TBitmap;

procedure InitOffscreenBitmap;
begin
  bm := TBitmap.Create;
  bm.SetSize(bmWidth, bmHeight);
end;

procedure DrawBitmap;
begin
  // Draw on bm
end;

procedure Swap;
begin
  BitBlt(Canvas.Handle, X, Y, bmWidth, bmHeight, bm.Canvas.Handle, 0, 0, SRCCOPY)
end;

如果您使用的是现代版本的 Windows(例如 Vista+),或启用了 ClearType 的 Windows XP(由于某些非常奇怪的原因,默认情况下禁用它),文本应该是平滑的。只要确保您使用现代字体即可。大多数字体都可以,但非常旧的字体(例如 MS Sans Serif)无法使用 ClearType 进行平滑处理。

另外,自然地,bm 必须具有与表单相同的背景颜色,因为在 bm 上绘制文本时会发生 alpha 混合。因此,如果形式是 clRed (出于某种不正当的原因),您需要

bm.Canvas.Brush.Color := clRed;
bm.Canvas.Brush.Style := bsSolid;
bm.FillRect(Rect(0, 0, bmWidth, bmHeight));

bm.TextOut(...)

Just 之前执行此操作,这样我们就在谈论同一件事:这还不够平滑吗?

procedure TForm3.FormPaint(Sender: TObject);
begin
  Canvas.Font.Name := 'Segoe UI';
  Canvas.Font.Height := 64;
  Canvas.TextOut(10, 10, 'This is an example.');
end;

示例文本输出
(高分辨率)

Are you never displaying the TImage? Then you should really use an off-screen bitmap instead. This is a very common technique to achieve double buffering (flicker-free rendering).

For example,

var
  bm: TBitmap;

procedure InitOffscreenBitmap;
begin
  bm := TBitmap.Create;
  bm.SetSize(bmWidth, bmHeight);
end;

procedure DrawBitmap;
begin
  // Draw on bm
end;

procedure Swap;
begin
  BitBlt(Canvas.Handle, X, Y, bmWidth, bmHeight, bm.Canvas.Handle, 0, 0, SRCCOPY)
end;

If you are using a modern version of Windows (e.g. Vista+), or Windows XP with ClearType enabled (for some very odd reason, it is disabled by default), text should be smooth. Just make sure that you use a modern font. Most of them will do, but very old fonts such as MS Sans Serif cannot be smoothed using ClearType.

Also, naturally, it is imperative that bm has the same background colour as the form, because alpha-blending will occur when the text is drawn on bm. So if the form is clRed (for some perverse reason), you need to do

bm.Canvas.Brush.Color := clRed;
bm.Canvas.Brush.Style := bsSolid;
bm.FillRect(Rect(0, 0, bmWidth, bmHeight));

prior to

bm.TextOut(...)

Just so we are talking about the same thing: Isn't this smooth enough?

procedure TForm3.FormPaint(Sender: TObject);
begin
  Canvas.Font.Name := 'Segoe UI';
  Canvas.Font.Height := 64;
  Canvas.TextOut(10, 10, 'This is an example.');
end;

Sample Text Output
(High-Res)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文