ASP图象组件使用

ASPImage组件的实现过程
[日期:2005-12-19 0:00:00] [字体: ]

下载本文示例源代码

在阅读这篇文章之前,最好能够接触过以下几项

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

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





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



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



四、在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= 40picture.ImgFormat = "image/jpeg"picture.FontStyle= 1Picture.SetFontColor 255,3,242,4 ''代表Alpha ,Red,Green,BluePicture.SetBackColor 10,243,42,54 ''代表Alpha ,Red,Green,BluePicture.ShowPicset Picture=nothing%></body></html>
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值