您的位置:首页 > 其它

GDIplus实现带光圈文字输出

2011-11-10 15:36 183 查看
原文:http://www.vckbase.com/document/viewdoc/?id=1459

你可能会认为生成一个带柔和阴影的特效文字与生成一个带光圈的特效文字是完全不同的,其实他们所用到的技术是完全相同的,只是在设置上有些许变化。 在带柔和阴影的效果中,我用到了GDI+中的一些插值模式来生成模糊的文字轮廓,当位图绘制平面放大时,插值模式决定原来某点像素应该怎样和周围的融合。 低质量的插值只是简单的把一个像素变换成同色色块,高质量插值如高质量双线性插值与高质量双三次插值会考虑像素色的平滑与反走样,我发现高质量双线 性插值模式最好。 

  这个技术把文字绘制两次,一次在一个小位图上绘制光圈,它将被用你所选的插值模式放大,另一次将在平面上绘制实际文字。用于生成光圈的位图必须与实际 文字成比例,在这儿我用1/5,因此光圈文字大小是实际文字的1/5。 

步骤如下:
建一个比实际绘制区域小的成比例的位图,这儿我用1/5;
建一个路径,把你想要生成效果的文字加入路径中;
用1中位图创建Graphics,建一个能缩小输出图形的矩阵;
用你想要的光圈颜色,填充文本路径,为了调节,用很细的画笔描出路径;
设置你要输出位图的Graphics的插值模式为高质双线形性,把光圈位图按比例放大到目标Graphic上;
最后,在目标Graphic上,按实际尺寸填充文本路径,这样将生成下面所示光圈效果;

二、代码说明请使用

void CTextHaloEffectView::OnDraw(CDC* pDC)
{
CTextHaloEffectDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
//客户区大小
CRect ClientRect;
GetClientRect(&ClientRect);
CSize ClientSize(ClientRect.Width(),ClientRect.Height());
using namespace Gdiplus;
Graphics g(pDC->m_hDC);
RectF ClientRectangle(ClientRect.top,ClientRect.left,ClientRect.Width(),ClientRect.Height());
g.FillRectangle(&SolidBrush(Color::Black),ClientRectangle);
Bitmap bm(ClientSize.cx/5,ClientSize.cy/5,&g);

//创建文字路径

GraphicsPath pth;

//Add the string in the chosen style.
int style = FontStyleRegular;
pth.AddString(L"文字光圈",-1,&FontFamily(L"宋体"),style,100,Point(20,20),NULL);

//位图Graphics

Graphics* bmpg = Graphics::FromImage(&bm);

//Create a matrix that shrinks the drawing output by the fixed ratio.

Matrix mx(1.0f/5,0,0,1.0f/5,-(1.0f/5),-(1.0f/5));

//Choose an appropriate smoothing mode for the halo.

bmpg->SetSmoothingMode(SmoothingModeAntiAlias);

//Transform the graphics object so that the same half may be used for both halo and text output.

//变换为位图的1/5,放大后将和实际文本相仿
bmpg->SetTransform(&mx);

//Using a suitable pen...

Pen p(Color::Yellow,3);

//Draw around the outline of the path

bmpg->DrawPath(&p,&pth);

//and then fill in for good measure.

bmpg->FillPath(&SolidBrush(Color::Yellow),&pth);

//this just shifts the effect a little bit so that the edge isn''t cut off in the demonstration

//移动50,50
g.SetTransform(&Matrix(1,0,0,1,50,50));

//setup the smoothing mode for path drawing

g.SetSmoothingMode(SmoothingModeAntiAlias);

//and the interpolation mode for the expansion of the halo bitmap

g.SetInterpolationMode(InterpolationModeHighQualityBicubic);

//expand the halo making the edges nice and fuzzy.

g.DrawImage(&bm,ClientRectangle,0,0,bm.GetWidth(),bm.GetHeight(),UnitPixel);

//Redraw the original text

g.FillPath(&SolidBrush(Color::Black),&pth);

//and you''re done.

}

效果图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: