GDIplus实现带光圈文字输出

原文:http://www.vckbase.com/document/viewdoc/?id=1459

你可能会认为生成一个带柔和阴影的特效文字与生成一个带光圈的特效文字是完全不同的,其实他们所用到的技术是完全相同的,只是在设置上有些许变化。 在带柔和阴影的效果中,我用到了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.  


} 

效果图:


要使用Gdiplus实现裁剪圆形头像,可以按照以下步骤进行: 1. 加载图片 首先,需要使用Gdiplus中的Image类加载一张图片,例如: ```c Image* pImage = new Image(L"avatar.png"); ``` 2. 创建设备上下文 接着,需要创建一个设备上下文,用于后面的绘制,例如: ```c HDC hdc = GetDC(hWnd); Graphics* pGraphics = new Graphics(hdc); ``` 其中,hWnd是窗口的句柄。 3. 创建裁剪区域 创建一个圆形的裁剪区域,可以使用Gdiplus中的GraphicsPath类和Region类,例如: ```c GraphicsPath path; path.AddEllipse(x, y, width, height); Region region(&path); pGraphics->SetClip(&region); ``` 其中,x、y、width、height分别表示圆形的位置和大小。 4. 绘制头像 最后,使用Gdiplus中的DrawImage函数绘制头像,例如: ```c pGraphics->DrawImage(pImage, x, y, width, height); ``` 其中,x、y、width、height分别表示头像的位置和大小。 5. 释放资源 绘制完成后,需要释放之前创建的资源,例如: ```c delete pImage; delete pGraphics; ReleaseDC(hWnd, hdc); ``` 完整的代码示例: ```c void DrawAvatar(Graphics* pGraphics, LPCWSTR lpszFileName, int x, int y, int width, int height) { Image* pImage = new Image(lpszFileName); HDC hdc = pGraphics->GetHDC(); GraphicsPath path; path.AddEllipse(x, y, width, height); Region region(&path); pGraphics->SetClip(&region); pGraphics->DrawImage(pImage, x, y, width, height); pGraphics->ReleaseHDC(hdc); delete pImage; } void OnPaint(HWND hWnd) { PAINTSTRUCT ps; HDC hdc = BeginPaint(hWnd, &ps); Graphics* pGraphics = new Graphics(hdc); DrawAvatar(pGraphics, L"avatar.png", 50, 50, 100, 100); delete pGraphics; EndPaint(hWnd, &ps); } ``` 注意,Gdiplus需要在程序开始时初始化,在程序结束时释放。可以使用以下代码进行初始化和释放: ```c GdiplusStartupInput gdiplusStartupInput; ULONG_PTR gdiplusToken; GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); // ... GdiplusShutdown(gdiplusToken); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值