| 网站首页 | 文章中心 | 电子书下载 | 矢量图库 | 视频教程 | 素材下载 | 程序代码下载 | JS代码 | 论坛 | 
常用软件类:
|杀毒安全 |联络聊天 |网络软件 |多媒体类 |系统工具 |图形图像 |系统工具 |应用软件 |行业软件
开发设计类:
|动画制作 |图像处理 |3D设计 |操作系统 |站长学院 |网络相关 |WEB设计 |数据库类 |程序开发
ASPImage组件的实现过程
作者:佚名    文章来源:网络    点击数:    更新时间:2006-9-30
 



1、你需要在Stdafx.h文件中加入下面两行:

#include <Gdiplus.h> 
using namespace Gdiplus; 

而且需要连接GDIPlus.lib库

#pragma comment(lib,"gdiplus.lib")

2、声明ULONG_PTR gdiplusToken;为一个全局或者类的内部成员变量。
3、在实现类的FinalConstruct函数中加入:

GdiplusStartupInput gdiplusStartupInput; 
//初始化 GDI+ 
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); 

4、在FinalRelease函数中加入:

GdiplusShutdown(gdiplusToken); 

这样你就可以使用GDI+提供的图形处理函数了。

注:关于GDI+的使用你可以在以下网址找到参考

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdicpp/gdi+/gdi+.asp

http://www.codeproject.com/vcpp/gdiplus/

六 接下来就是实现在四中列出的这些属性和方法,下面列出ShowPic方法的一段代码,其他代码请查看源代码

Bitmap bitmap(1,1,PixelFormat48bppRGB);
Graphics graphics(&bitmap);
USES_CONVERSION;
Font font( OLE2CW(m_bstrFontName),(float)m_fFontSize,m_nFontStyle,
UnitPoint,NULL); PointF origin(0, 0); StringFormat format; format.SetAlignment(StringAlignmentCenter); RectF boundRect; graphics.MeasureString(OLE2CW(m_bstrText),m_bstrText.Length (),
&font, origin, &format, &boundRect); int nWidth = (int)boundRect.Width; int nHeight = (int)boundRect.Height; Bitmap bm(nWidth,nHeight,PixelFormat48bppRGB); Graphics* g=Graphics::FromImage (&bm); boundRect.Width=boundRect.Width*2; SolidBrush solidbrush(m_cBackground); g->FillRectangle(&solidbrush,boundRect); SolidBrush SolidFont(m_cFontColor); PointF fPoint(0,0); g->DrawString(OLE2CW(m_bstrText),m_bstrText.Length (),
&font,fPoint,&SolidFont); int result; CLSID pngClsid; result = GetCodecClsid(OLE2W(m_btrImgFormat ), &pngClsid); HRESULT hr; HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, 0); CComPtr<IStream> pStm; if (FAILED(hr = CreateStreamOnHGlobal(hGlobal, TRUE, &pStm) )) return hr; bm.Save(pStm,&pngClsid,NULL); ULONG cElements = (ULONG)GlobalSize(hGlobal); LPBYTE lpData = (LPBYTE)GlobalLock(hGlobal); SAFEARRAY* pArray = SafeArrayCreateVector(VT_UI1, 0, cElements); for (UINT iElement = 0; iElement < cElements; iElement++) { long idx = iElement; SafeArrayPutElement(pArray, &idx, ((LPBYTE)lpData) + iElement); } GlobalUnlock(hGlobal); CComVariant vBytes; vBytes.vt = VT_ARRAY | VT_UI1; vBytes.parray = pArray; m_piResponse->Clear (); m_piResponse->put_ContentType (m_btrImgFormat); m_piResponse->BinaryWrite(vBytes); m_piResponse->End ();

七、现在组件的实现部分就大功告成了,我们写一段ASP来测试以下这个组件

<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<html>
<body>
<%
set Picture=Server.CreateObject("AspPic.AspPicCom") 
picture.Text ="你能看见我吗?我来自www.goodassister.com!"
picture.FontName="黑体"
picture.FontSize= 40
picture.ImgFormat = "image/jpeg"
picture.FontStyle= 1
Picture.SetFontColor 255,3,242,4 ''代表Alpha ,Red,Green,Blue
Picture.SetBackColor 10,243,42,54 ''代表Alpha ,Red,Green,Blue
Picture.ShowPic
set Picture=nothing
%>
</body>
</html>

注:在使用前先注册组件 regsvr32 AspPic.dll



打开这个ASP网页,你将会看到如下的图片


现在实现aspImage的具体思路就讲完了,现在如果需要更多的效果,你可以自己丰富这个组件

题外话:这类组件的用途很广,如yahoo.com注册会员时会显示图形单词以防止计算机自动注册,因为计算机要识别图片上的文字是低效而且很不容易的事情,这类组件更多的是应用到图表的生成,如柱状图、馅饼图、波形图等等。

这篇文章就写到这里了,如果你觉得有什么意见或者我讲错了的地方,请你告诉我
Email:sillyboy@china.com

上一页  [1] [2] 


相关文章