当前位置:中国站长下载文章中心网页编程ASP编程 → ASPImage组件的实现过程

ASPImage组件的实现过程

减小字体 增大字体 作者:不详  来源:不详  发布时间:2006-8-13 0:22:51
     在阅读这篇文章之前,最好能够接触过以下几项
  
  GDI+
  利用ATL生成组件
  STL
  ASP使用组件
  aspImage是ServerObjects站点上非常好的一个组件,它可以使我们利用Asp实现很多对于图形的处理功能,他的功能强大,如果你需要了解更详细的信息你可以访问它的官方网站,在这里就不讲如何使用这个组件了,我们在这里要讲的是如何实现这类组件,当然下面的例子中使用GDI+并不是实现组件图形处理的唯一方法,大家也可以自己试试其他的方法.
  以下是实现过程
  
  一、首先使用Visual C++的向导建立ATL项目
  
  二、接下来添加一个ATL Active Server Page 组件接口类
  
  四、在IAspPicCom接口上添加属性和方法,如下表
  
  名称 类别 含义 调用方法
  FontName 属性 字体名称 字符串类型
  
  使用方法
  .FontName="宋体"
  FontSize 属性 字体大小 整型
  
  使用方法
  .FontSize=40
  FontStyle 属性 字体类型 整形
  
  Regular = 0,
  Bold = 1,
  Italic = 2,
  BoldItalic = 3,
  Underline = 4,
  Strikeout = 8
  
  使用方法
  .FontStyle=8
  ImgFormat 属性 图形格式 字符串类型
  
  image/gif
  image/jpeg
  image/bmp
  ......
  
  使用方法
  .ImgFormat="image/gif"
  SetFontColor 方法 设置字体颜色 使用方法
  .SetFontColor 255,3,242,4
  上面的数字分别代表Alpha,Red,Green,Blue
  SetBackColor 方法 设置字体背景颜色 使用方法
  .SetBackColor 255,3,242,4
  上面的数字分别代表Alpha,Red,Green,Blue
  ShowPic 方法 将图片发送到客户端 使用方法
  .ShowPic
  五、 要在ASP组件中使用GDI+,需要加入一些代码
  
  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
  
  
  
  
  
  
    做人要厚道,请注明转自chinazhan中国站长(www.ChinaZhan.com)。