end;
s:=s+crlf;
end;
result:=s;
end;
这是一个常用且效果比较好的灰度:“mnhq$oc?7>!":-';. ”
第四个关键技术:把文本转换为图像
要把文本转换为图片,必须获取两个重要参数:转换后的宽和高,要取得这两个参数,我们可以使用 gettextextentpoint32 函数,该函数的定义如下:
function gettextextentpoint32(dc: hdc; str: pchar; count: integer; var size: tsize): bool;
dc 传入设备句柄
str 为文本内容
count 为文本的长度(字节)
size 返回宽和高
在实际应用中,往往被转换的文本有多行,且每一行的长度不定,
所以我们还需要在生成图像前进行一遍预扫,以便获得完整的图像大小
下面演示了文本转换为图像的代码
////////////////////////////////////////////////////////////////////////////////
// 功能 : 把文本转换为位图
// aowner : 窗体参数
// atext : 要转换的文本
// afont : 文本的字体
// abitmap : 转换后的位图对象
// 日期 : 2003.12.15
////////////////////////////////////////////////////////////////////////////////
procedure texttobitmap(aowner:tobject;const atext:tstrings;afont:tfont;abitmap:tbitmap);
var
i :integer;
iwidth,iheight :integer;
icharheight :integer;
s :string;
r :trect;
size :tsize;
lbltemp :tlabel;
begin
iwidth:=0;
iheight:=0;
lbltemp:=tlabel.create(nil);
r.top:=0;
try
lbltemp.visible:=false;
lbltemp.parent:=twincontrol(aowner);
lbltemp.font.assign(afont);
abitmap.canvas.brush.style:=bsclear;
abitmap.canvas.pen.color:=rgb(0,0,0);
abitmap.canvas.brush.color:=rgb(255,255,255);
abitmap.canvas.font.assign(afont);
// 下面代码用户获得文本的最大宽度和高度
for i:=0 to atext.count-1 do
begin
s:=atext.strings[i];
if s='' then s:=' ';
lbltemp.caption:=s;
gettextextentpoint32(lbltemp.canvas.handle,pchar(lbltemp.caption),lbltemp.gettextlen,size);
if iwidth<size.cx then iwidth:=size.cx;
iheight:=iheight+size.cy;
end;
// 获得一个字符的高度
gettextextentpoint32(lbltemp.canvas.handle,pchar(' '),length(' '),size);
icharheight:=size.cy;
abitmap.width:=iwidth;
abitmap.height:=iheight;
for i:=0 to atext.count-1 do
begin
s:=atext.strings[i];
r.left:=0;
r.right:=abitmap.width;
r.bottom:=r.bottom+icharheight;
drawtext(abitmap.canvas.handle,pchar(s),length(s),r,0);
r.top:=r.top+icharheight;
end;
finally
lbltemp.free;
end;
end;
2003.12.15
凌丽软件工作室