Download the sample source code for this article
Before reading this article, it is best to have contact with the following
GDI+
Generate components using ATL
STL
ASP usage components
aspImage is a very good component on the ServerObjects site. It allows us to use Asp to implement many graphics processing functions. Its functions are powerful. If you need more detailed information, you can visit its official website, which will not be discussed here. Let’s talk about how to use this component. What we are going to talk about here is how to implement this type of component. Of course, using GDI+ in the following example is not the only way to implement component graphics processing. You can also try other methods yourself.
The following is the implementation process
. First, use the Visual C++ wizard to create an ATL project.
2. Next add an ATL Active Server Page component interface class
3. Generate a class named AspPicCom and select the ASP internal object Response
4. Add properties and methods to the IAspPicCom interface, as shown in the following table:
name | category | meaning | call method | |
FontName | attribute | font | name string type How to use .FontName="宋体" | |
FontSize | property | font size | integer How to use .FontSize=40 | |
FontStyle | property | font type | shaping Regular = 0, Bold = 1, Italic = 2, BoldItalic = 3, Underline = 4, Strikeout = 8 How to use .FontStyle=8 | |
ImgFormat | property | graphic format | string type image/gif image/jpeg image/bmp ... How to use .ImgFormat="image/gif" | |
SetFontColor | method | sets the font color | and how to use it .SetFontColor 255,3,242,4 The numbers above represent Alpha, Red, Green, and Blue respectively. | |
How to use | the SetBackColor | method | to set the font background color. | .SetBackColor 255,3,242,4 The numbers above represent Alpha, Red, Green, and Blue. |
The ShowPic | method | sends the picture to the client | . .ShowPic |
#include <Gdiplus.h> using namespace Gdiplus;
and needs to connect to the GDIPlus.lib library
#pragma comment(lib, "gdiplus.lib")
2. Declare ULONG_PTR gdiplusToken; as a global or internal member variable of the class.
3. Add to the FinalConstruct function of the implementation class:
GdiplusStartupInput gdiplusStartupInput; //Initialize GDI+ GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);4. Add:
GdiplusShutdown(gdiplusToken);
so that you can use the graphics processing functions provided by GDI+
in the FinalRelease function
.Note: Regarding the use of GDI+, you can find reference at the following URL:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/gdicpp/gdi+/gdi+.asp
http://www .codeproject.com/vcpp/gdiplus/
6 The next step is to implement the properties and methods listed in 4. A piece of code for the ShowPic method is listed below. For other codes, please view the source code
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 ();
7. Now that the component implementation is complete, we write an ASP to test the following component
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%> <html> <body> <% set Picture=Server.CreateObject("AspPic.AspPicCom") picture.Text="Can you see me? I'm from www.goodassister.com!" picture.FontName="Heold" picture.FontSize= 40 picture.ImgFormat = "image/jpeg" picture.FontStyle= 1 Picture.SetFontColor 255,3,242,4 '' represents Alpha,Red,Green,Blue Picture.SetBackColor 10,243,42,54 '' represents Alpha, Red, Green, Blue Picture.ShowPic set Picture=nothing %> </body> </html>
Note: Register the component regsvr32 AspPic.dll before use
Open this ASP web page, you will see the following picture
Now the specific ideas for implementing aspImage are finished. Now if you need more effects, you can enrich this component yourself.
Digression: This type of component has a wide range of uses. For example, when yahoo.com registers as a member, graphic words will be displayed to prevent the computer from automatically Register, because it is inefficient and not easy for computers to recognize text on pictures. This type of component is more applied to the generation of charts, such as histograms, pie charts, waveform charts, etc.
That’s it for this article. If you have any comments or think I’ve made a mistake, please tell me.
Email:[email protected]