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.
}
效果图:
你可能会认为生成一个带柔和阴影的特效文字与生成一个带光圈的特效文字是完全不同的,其实他们所用到的技术是完全相同的,只是在设置上有些许变化。 在带柔和阴影的效果中,我用到了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.
}
效果图:
相关文章推荐
- 实现文字由右向左一个一个的变色输出,可有css+div来控制页面输出位置
- Gdiplus阴影文字实现
- Uboot实现文字纵向输出的原理和实现
- matlab中实现文字和数字和混输出excel
- Servlet中Response对象应用1(输出简单文字、实现文件下载)
- mfc主窗口添加背景图片后,如何实现在背景图片上输出文字
- jQuery实现的动态文字变化输出效果示例【附演示与demo源码下载】
- Opencv 输出文字,实现简单水印
- css实现环绕文字发出光圈
- 根据输入的文字实现输出倒文的批处理
- python处理计算机辅助设计软件文字信息并实现排序输出
- Android实现OCR文字识别并且转换为Excel、PDF格式输出
- Linux C 编程 实现彩色文字输出
- MFC下窗口分割和文字输出的实现
- 用opencv实现绘图和文字输出。。。。
- 文字到叙输出的实现步骤
- python实现自动输出文字小demo(cmd下)
- JS实现默认显示部分文字点击按钮显示全部内容
- [置顶] DIV+CSS 实现背景透明,文字不透明
- VC实现基于Office 2007 MODI 12.0实现图形文字识别(OCR)的程序