VC带光圈文字输出的实现

下载源代码

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

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

步骤如下:

1建一个比实际绘制区域小的成比例的位图,这儿我用1/5;

2建一个路径,把你想要生成效果的文字加入路径中;

3用1中位图创建Graphics,建一个能缩小输出图形的矩阵;

4用你想要的光圈颜色,填充文本路径,为了调节,用很细的画笔描出路径;

5设置你要输出位图的Graphics的插值模式为高质双线形性,把光圈位图按比例放大到目标Graphic上;

6最后,在目标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. }  

三、效果图

VC带光圈文字输出的实现_第1张图片

四、说明

为了让没有装GDI+的朋友使用方便,此次把在GDI+直接包含在源代码中,使用时把gdiplus.dll文件直接拷到$path即可。
转自:http://www.vckbase.com/document/viewdoc/?id=1459

你可能感兴趣的:(object,String,Path,Matrix,output,GDI+)