日期:2010-10-03  浏览次数:20897 次

  ASP使用组件

  aspImage是ServerObjects站点上非常好的一个组件,它可以使我们利用Asp实现很多对于图形的处理功能,他的功能强大,如果你需要了解更详细的信息你可以访问它的官方网站,在这里就不讲如何使用这个组件了,我们在这里要讲的是如何实现这类组件,当然下面的例子中使用GDI+并不是实现组件图形处理的唯一方法,大家也可以自己试试其他的方法.

  以下是实现过程

  一、首先使用Visual C++的向导建立ATL项目

ASPImage组件制作水印的实现过程

  二、接下来添加一个ATL Active Server Page 组件接口类

ASPImage组件制作水印的实现过程

  三、生成名称为AspPicCom的类并且选择ASP内部对象Response

ASPImage组件制作水印的实现过程

  四、在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

  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=Graphi