自从文章《GDI+ 在Delphi程序的应用 -- 可调节的文字阴影特效》发表后,不少人问我怎样实现文字描边。由于我只是个业余编程爱好者,加上文化底蕴差,只要涉及算法和编程理论方面的东西,我就无能为力了,所以直到目前,我也不知道具体的描边算法是怎样的(网上搜索过N次,也没找到答案,可能这方面的东西是要卖钱的)。
因问得人多了,有时我也思索和研究一下,总算找了个方法可以实现,虽然同专业的图像软件(如PhotoShop)文字描边效果相比差强人意,但可以凑合凑合,作为研究心得,将代码贴在这里备查。
在《GDI+ 在Delphi程序的应用 -- 可调节的文字阴影特效》一文的内容的基础上,对文字阴影效果代码进行了改进和扩充,扩充的功能有2点:一是由原来只能产生黑色阴影扩充为任意颜色阴影;二是可以对阴影进行扩展。有了这2个功能,利用阴影效果也就可以进行文字描边了,推而广之,也可实现图像的描边。下面是具体的代码内容:
-
-
procedureBackImage(Data:TBitmapData;Dest:Pointer;Color:TARGB);
-
asm
- pushesi
- pushedi
-
movesi,[eax+16]
-
movedi,edx
-
movedx,ecx
-
andedx,0FFFFFFh
-
movecx,[eax]
-
imulecx,[eax+4]
- cld
-
@Loop:
-
or[esi],edx
-
movsd
- loop@Loop
- popedi
- popesi
-
end;
-
-
-
procedureMakeExpand(Data:TBitmapData;Source,ExpMatrix:Pointer;
- MatrixSize:LongWord);
-
var
- Radius,mSize,rSize:LongWord;
- x,y:LongWord;
- Width,Height:Integer;
- Matrix:Pointer;
- Stride:LongWord;
-
asm
- pushesi
- pushedi
- pushebx
-
movesi,edx
-
movedi,[eax+16]
-
addedi,3
- add ecx, 3
-
movMatrix,ecx
- movecx,MatrixSize
- movedx,ecx
- dececx
- movebx,[eax]
- subebx,ecx
-
movWidth,ebx
-
movebx,[eax+4]
- subebx,ecx
-
movHeight,ebx
-
shrecx,1
-
movRadius,ecx
-
moveax,[eax+8]
- movStride,eax
- movmSize,eax
-
shledx,2
-
submSize,edx
-
addeax,4
- imuleax,ecx
-
addeax,3
-
addesi,eax
-
shlecx,3
-
movrSize,ecx
-
movy,0
-
@yLoop:
-
movx,0
-
@xLoop:
-
test[esi],0ffh
-
jz@NextPixel
-
test[esi-4],0ffh
-
jz@001
-
test[esi+4],0ffh
- jz@001
- movebx,Stride
- test[esi+ebx],0ffh
- jz@001
- negebx
- test[esi+ebx],0ffh
- jnz@NextPixel
-
@001:
-
pushedi
-
movebx,Matrix
-
movedx,MatrixSize
-
@Loop3:
-
movecx,MatrixSize
-
@Loop4:
-
mov al,[ebx]
- cmp al,[edi]
-
jb @002
-
mov[edi],al
-
@002:
-
addedi,4
-
addebx,4
-
loop@Loop4
-
addedi,mSize
- decedx
-
jnz @Loop3
-
popedi
-
@NextPixel:
-
addedi,4
-
addesi,4
- incx
- moveax,x
- cmpeax,Width
-
jl@xLoop
- addesi,rSize
- addedi,rSize
- incy
- moveax,y
- cmpeax,Height
-
jl@yLoop
- popebx
- popedi
- popesi
-
end;
-
procedureGdipShadow(Data:TBitmapData;Buf:Pointer;Radius:LongWord);
-
var
-
Gauss:arrayofInteger;
- Q:Double;
- x,y,n,z:Integer;
- p:PInteger;
-
begin
-
-
Q:=Radius/2;
-
ifQ=0thenQ:=0.1;
-
n:=Radiusshl1+1;
- SetLength(Gauss,n*n);
-
p:=@Gauss[0];
-
z:=0;
-
forx:=-RadiustoRadiusdo
-
fory:=-RadiustoRadiusdo
-
begin
-
p^:=Round(Exp(-(x*x+y*y)/(2.0*Q*Q))/(2.0*PI*Q*Q)*1000.0);
- Inc(z,p^);
- Inc(p);
-
end;
- MakeShadow(Data,Buf,Gauss,n,z);
-
end;
-
procedureGdipBorder(Data:TBitmapData;Buf:Pointer;Expand:LongWord;Color:TARGB);
-
var
- bmp:TGpBitmap;
- bg:TGpGraphics;
- Data1:TBitmapData;
- Size:Integer;
-
begin
-
Size:=Expandshl1+1;
-
bmp:=TGpBitmap.Create(Size,Size,pf32bppARGB);
-
bg:=TGpGraphics.Create(bmp);
-
try
-
-
bg.SmoothingMode:=smAntiAlias;
-
bg.PixelOffsetMode:=pmHalf;
-
bg.FillEllipse(Brushs[Color],0,0,Size,Size);
-
Data1:=bmp.LockBits(GpRect(0,0,Size,Size),[imRead],pf32bppARGB);
-
try
-
-
MakeExpand(Data,Buf,Data1.Scan0,Size);
-
finally
-
bmp.UnlockBits(Data1);
-
end;
-
finally
-
bg.Free;
-
bmp.Free;
-
end;
-
end;
-
procedureDrawShadow(constg:TGpGraphics;constBitmap:TGpBitmap;
-
constlayoutRect:TGpRectF;ShadowSize,Distance:LongWord;
- Angle:Single;Color:TARGB;Expand:LongWord);
-
var
- dr,sr:TGpRectF;
- Data:TBitmapData;
- Buf:Pointer;
- SaveScan0:Pointer;
-
begin
-
Data:=Bitmap.LockBits(GpRect(0,0,Bitmap.Width,Bitmap.Height),
- [imRead,imWrite],pf32bppARGB);
-
GetMem(Buf,Data.Height*Data.Stride);
-
try
- BackImage(Data,Buf,Color);
-
ifExpand>ShadowSizethen
- Expand:=ShadowSize;
-
ifExpand<>0then
-
ifExpand<>ShadowSizethen
-
begin
-
SaveScan0:=Data.Scan0;
-
Data.Scan0:=Buf;
- GdipBorder(Data,SaveScan0,Expand,Color);
-
Data.Scan0:=SaveScan0;
-
endelse
- GdipBorder(Data,Buf,Expand,Color);
-
ifExpand<>ShadowSizethen
- GdipShadow(Data,Buf,ShadowSize-Expand);
-
finally
- FreeMem(Buf);
-
Bitmap.UnlockBits(Data);
-
end;
-
sr:=GpRect(0.0,0.0,Data.Width,Data.Height);
-
-
-
dr:=GpRect(layoutRect.Point,sr.Size);
-
-
Offset(dr,Cos(PI*Angle/180)*Distance-ShadowSize-1,
-
Sin(PI*Angle/180)*Distance-ShadowSize-1);
-
-
g.DrawImage(Bitmap,dr,sr.X,sr.Y,sr.Width,sr.Height,utPixel);
-
end;
-
-
-
-
-
-
procedureDrawShadowString(constg:TGpGraphics;conststr:WideString;
-
constfont:TGpFont;constlayoutRect:TGpRectF;
-
ShadowSize,Distance:LongWord;Angle:Single=60;
-
Color:TARGB=$C0000000;Expand:LongWord=0;
-
constformat:TGpStringFormat=nil);overload;
-
var
- Bmp:TGpBitmap;
- Bg:TGpGraphics;
-
begin
-
-
Bmp:=TGpBitmap.Create(Round(layoutRect.Width+0.5)+ShadowSizeshl1+2,
-
Round(layoutRect.Height+0.5)+ShadowSizeshl1+2,
- pf32bppARGB);
-
Bg:=TGpGraphics.Create(Bmp);
-
try
-
Bg.TextRenderingHint:=thAntiAlias;
-
-
-
Bg.DrawString(str,font,Brushs[Colorand$FF000000],
-
GpRect(ShadowSize+1,ShadowSize+1,
-
layoutRect.Width,layoutRect.Height),format);
- DrawShadow(g,Bmp,layoutRect,ShadowSize,Distance,Angle,Color,Expand);
-
finally
-
Bg.Free;
-
Bmp.Free;
-
end;
-
end;
-
-
procedureDrawShadowString(constg:TGpGraphics;conststr:WideString;
-
constfont:TGpFont;constorigin:TGpPointF;
-
ShadowSize,Distance:LongWord;Angle:Single=60;
-
Color:TARGB=$C0000000;Expand:LongWord=0;
-
constformat:TGpStringFormat=nil);overload;
-
begin
-
DrawShadowString(g,str,font,g.MeasureString(str,font,origin,format),
- ShadowSize,Distance,Angle,Color,Expand,format);
-
end;
上面代码中MakeShadow过程的代码在《GDI+ 在Delphi程序的应用 -- 可调节的文字阴影特效》一文中,本文没有贴出。由于代码中已经有了较详细的注释,故不再解释。下面贴出测试代码:
-
procedureTextPaint(g:TGpGraphics);
-
var
- brush:TGpLinearGradientBrush;
- font:TGpFont;
- fontFamily:TGpFontFamily;
- r:TGpRect;
-
begin
-
fontFamily:=TGpFontFamily.Create('华文行楷');
-
font:=TGpFont.Create(fontFamily,55,[fsBold],utPixel);
-
r:=GpRect(Form1.PaintBox1.ClientRect);
-
brush:=TGpLinearGradientBrush.Create(r,kcBlue,kcAliceBlue,90);
-
g.FillRectangle(Brush,r);
-
DrawShadowString(g,'文字阴影特效',font,GpPoint(10,r.Height/3),5,10,60,$C0000000,1);
-
DrawShadowString(g,'文字阴影特效',font,GpPoint(10,r.Height/3),1,0,60,$FFFF0000,1);
-
-
-
g.TextRenderingHint:=thAntiAlias;
-
g.DrawString('文字阴影特效',font,Brushs.White,10,r.Height/3);
-
font.Free;
-
fontFamily.Free;
-
Brush.Free;
-
end;
以下是测试代码效果图,图一和图二都是文字描边(1个像素的边框)加阴影效果,其中图一没进行阴影扩展,即上面的15行的代码最后一个参数为0,图二是加了1个像素的阴影扩展效果(上述代码的“正宗”输出):
图一
图二
利用改进的阴影效果,不仅可实现文字描边,也可显示类似立体文字的效果(改变显示距离),上面测试代码中,被注释的2句代码输出效果如下:
图三
至于图像的描边,似乎没有文字的描边效果好,究其原因,主要是图像的轮廓看起来好像是圆润平滑的,其实有很多半影锯齿,在Photoshop中,通过先选区后描边,可能对选区边缘作了处理,所以效果相当好(专业的软件,肯定有很好的算法)。下面是我对一张小图片作的描边处理代码和输出效果图:
-
-
-
-
-
procedureDrawImageBorder(constg:TGpGraphics;constImage:TGpImage;
- x,y:Single;BorderWidth:LongWord;Color:TARGB=kcWhite;
-
Expand:LongWord=0;constAttributes:TGpImageAttributes=nil);
-
var
- Bmp:TGpBitmap;
- Bg:TGpGraphics;
- ColorMatrix:TColorMatrix;
- Attr:TGpImageAttributes;
- layoutRect:TGpRectF;
-
begin
-
Bmp:=TGpBitmap.Create(Image.Width+BorderWidthshl1+2,
-
Image.Height+BorderWidthshl1+2,
- pf32bppARGB);
-
Bg:=TGpGraphics.Create(Bmp);
- Attr:=Attributes;
-
ifAttr=nilthen
-
Attr:=TGpImageAttributes.Create;
-
try
-
FillChar(ColorMatrix,Sizeof(TColorMatrix),0);
-
ColorMatrix[3,3]:=1;
-
ColorMatrix[4,4]:=1;
-
-
Attr.SetColorMatrix(ColorMatrix);
-
layoutRect:=GpRect(x,y,Image.Width,Image.Height);
-
Bg.DrawImage(Image,
-
GpRect(BorderWidth+1,BorderWidth+1,layoutRect.Width,layoutRect.Height),
-
0,0,layoutRect.Width,layoutRect.Height,utPixel,Attr);
-
DrawShadow(g,Bmp,layoutRect,BorderWidth,0,0,Color,BorderWidth-Expand);
-
finally
-
ifAttributes<>nilthen
-
Attr.ClearColorMatrix
-
else
-
Attr.Free;
-
Bg.Free;
-
Bmp.Free;
-
end;
-
end;
-
procedureImagePaint(g:TGpGraphics);
-
var
- brush:TGpLinearGradientBrush;
- r:TGpRect;
- Image:TGpImage;
- Attributes:TGpImageAttributes;
-
begin
-
r:=GpRect(Form1.PaintBox1.ClientRect);
-
brush:=TGpLinearGradientBrush.Create(r,kcBlue,kcAliceBlue,90);
-
g.FillRectangle(Brush,r);
-
Image:=TGpImage.Create('../../Media/Watermark.bmp');
-
-
g.TranslateTransform(20,r.Height/3);
-
g.DrawImage(Image,0,0,Image.Width,Image.Height);
-
-
Attributes:=TGpImageAttributes.Create;
-
Attributes.SetColorKey($ff00ff00,$ff00ff00);
-
-
g.TranslateTransform(Image.Width+20,0);
-
DrawImageBorder(g,Image,0,0,2,kcWhite,0,Attributes);
-
g.DrawImage(Image,GpRect(0.0,0,Image.Width,Image.Height),
-
0.0,0.0,Image.Width,Image.Height,utPixel,Attributes);
-
-
g.TranslateTransform(Image.Width+20,0);
-
DrawImageBorder(g,Image,0,0,5,kcWhite,3,Attributes);
-
g.DrawImage(Image,GpRect(0.0,0,Image.Width,Image.Height),
-
0.0,0.0,Image.Width,Image.Height,utPixel,Attributes);
-
Attributes.Free;
-
Brush.Free;
-
Image.Free;
-
end;
图四
上面的效果图中,左边是原图,中间是2个像素的描边图,右边是5个像素的描边图,其中有3像素的模糊扩散。从图中可以看出,我以$ff00ff00为透明色处理图像四个角后,在中间和右边的描边图中,还是很明显的看到四个角有很淡的绿色,正是这个原因,在中间图的圆角描边有明显的锯齿。
最后作几点说明:
1、本文纯属业余学习和研究的心得,并非什么正宗的算法;
2、因为本文代码是学习时即兴写的,并非优化代码,而且是以过程形式出现的,有兴趣的朋友可以自己进行优化改进,写成类或者元件更好(由于算法和功能都不是很完善,所以我没写成类的形式);
3、例子中的GDI+版本系本人自己改写的,与网上流通的版本不完全兼容,如需使用本版本,请参照《GDI+ for VCL基础 -- GDI+ 与 VCL 》一文的下载地址,并请留意后面的修改说明。
4、如有好的建议,请来信:maozefa@hotmail.com
更新(2008-8-5 12:50):在MakeExpand过程中,是按图象逐点用位图画笔矩阵填充的,每个像素点都要进行矩阵大小的操作,最小的1像素扩展的矩阵大小为3 * 3,可见扩展速度是不大理想的。今天对代码作了一点修改,对每个象素点都进行了判断,如果是边界像素,则作画笔矩阵填充,否则直接跳过,这样一来,速度应该提高不少(没作测试,增加的代码用红色标出,有兴趣者可以测试)。
分享到:
相关推荐
易语言GDI文字描边源码,GDI文字描边
GDI文字描边.rar
delphi自定义窗体描边演示delphi自定义窗体描边演示
自已对别人给的一个不完整的类,进行了封装。经过测试,可以完美的实现文字描边。
是用GDI+对输入的文字重绘!描边!易语言源代码!学习GDI挺不错的!
AI 课件 ,AI教案,AI教学计划I llustrator课件、教案 、教学计划
dw文字描边效果 span { position:absolute; padding:4px; filter: Dropshadow(offx=1,offy=0,color=white) Dropshadow(offx=0,offy=1,color=white) Dropshadow(offx=0,offy=-1,color=white) Dropshadow(offx=-1,...
商业编程-源码-给 RGN 描边.zip
继承自Label的一个可支持文字描边的空间,可以添加各种炫酷效果,包括一个库和一个DEMO,文字描边方法网上比较少,研究了半天,最后是在MSDN上面找到了方案,把他发上来,可以直接用,也可以改,希望能节省大家的...
易语言GDI文字描边源码
易语言源码易语言GDI文字描边源码.rar
swift给文字添加描边效果封装方法/// 给字体添加描边效果 : strokeWidth为正数为空心文字描边 strokeWidth为负数为实心文字描边let
今天来一起了解一个实现SVG路径描边绘制与动画的轻量级类库segment,我们从轻松上手、使用详解、资源和案例、源码解读等几个方面进行介绍。 html方面添加segment,定义path。
Cocos2d-x 2.X实现的一基于CCLabelTTF + Shader的字体描边效果,具体可以参考这篇博客,http://blog.csdn.net/oktears/article/details/43200757
源码是自编_GDI+例程,源码实现了GDI文字描边。
易语言GDI文字描边源码.rar
易语言GDI文字描边源码.zip易语言项目例子源码下载易语言GDI文字描边源码.zip易语言项目例子源码下载 1.合个人学习技术做项目参考 2.适合学生做毕业设计参考 3.适合小团队开发项目参考