首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> VC >> 關於VC++

ASPImage 組件實作全紀錄:基於 ATL 與 GDI+ 的動態圖片生成

     編輯:關於VC++

前置需求

  • 熟悉 GDI+ 圖形程式設計
  • 掌握 ATL 組件開發基礎
  • 了解 STL 容器使用
  • 知道 ASP 如何呼叫 COM 組件

參考商業組件 aspImage (ServerObjects),功能強大。本文不講使用方法,專注於如何自行實作同類組件。GDI+ 非唯一選擇,開發者可嘗試其他圖形庫。

步驟一:建立 ATL 專案

建立 ATL 專案精靈

步驟二:新增 ATL Active Server Page 組件

新增 ATL ASP 組件

步驟三:命名類別並選擇內建物件

類別名:AspPicCom,勾選 Response 內建物件。
類別命名與內建物件選擇

步驟四:定義介面屬性與方法

名稱類別說明調用範例
FontName屬性字體名稱 (字串).FontName = "宋體"
FontSize屬性字體大小 (整數).FontSize = 40
FontStyle屬性字體樣式 (整數旗標)Regular=0, Bold=1, Italic=2, BoldItalic=3, Underline=4, Strikeout=8
.FontStyle = 8
ImgFormat屬性輸出圖片格式 (MIME)"image/gif", "image/jpeg", "image/bmp"...
.ImgFormat = "image/gif"
SetFontColor方法設定字體顏色 (ARGB).SetFontColor 255,3,242,4 (Alpha,Red,Green,Blue)
SetBackColor方法設定背景顏色 (ARGB).SetBackColor 255,3,242,4
ShowPic方法輸出圖片至客戶端.ShowPic

步驟五:整合 GDI+

1. Stdafx.h 引入標頭與庫


#include 
using namespace Gdiplus;
#pragma comment(lib, "gdiplus.lib")

2. 宣告 GDI+ Token


ULONG_PTR gdiplusToken; // 全域或類別成員

3. FinalConstruct 初始化 GDI+


GdiplusStartupInput gdiplusStartupInput;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

4. FinalRelease 關閉 GDI+


GdiplusShutdown(gdiplusToken);

參考資源:MSDN GDI+、CodeProject GDI+ 專區

步驟六:核心方法實作 —— 以 ShowPic 為例


// 建立 1x1 像素位圖用於測量文字範圍
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 *= 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);

// 取得編碼器 CLSID
int result;
CLSID pngClsid;
result = GetCodecClsid(OLE2W(m_btrImgFormat), &pngClsid);

// 建立記憶體串流
HRESULT hr;
HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, 0);
CComPtr pStm;
if (FAILED(hr = CreateStreamOnHGlobal(hGlobal, TRUE, &pStm))) return hr;

// 位圖存入串流
bm.Save(pStm, &pngClsid, NULL);

// 讀取串流資料至 SAFEARRAY
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);

// 封裝為 VARIANT 並輸出
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();

關鍵技術點:MeasureString 精確測量、CreateStreamOnHGlobal 記憶體串流、BinaryWrite 直接輸出二進制圖片數據、Response.ContentType 設定正確 MIME。

步驟七:ASP 測試頁面


<%@ LANGUAGE="VBSCRIPT" CODEPAGE="936" %>

<%
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
%>

使用前請先註冊: regsvr32 AspPic.dll

執行結果:
ASP 測試輸出圖片

應用場景延伸

  • 驗證碼圖片:如 Yahoo 註冊頁防機器人,圖形文字識別困難。
  • 動態圖表生成:長條圖、餅圖、波形圖等商業報表。
  • 水印/簽名合成:批次處理圖片加浮水印。

總結

本文完整記錄了從 ATL 專案建立、介面設計、GDI+ 整合、核心繪圖邏輯到 ASP 測試的全過程。核心難點在於 GDI+ 繪圖流程與 ASP Response.BinaryWrite 的串流處理。掌握此架構,可擴展實現各類動態圖片生成需求。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved