Click here to Skip to main content
15,896,726 members
Articles / Desktop Programming / MFC

Dynamically generating images in ISAPI extension using GDI+ with live demo

Rate me:
Please Sign up or sign in to vote.
3.88/5 (8 votes)
3 Oct 20022 min read 65.4K   424   26  
A class wrapper to write GDI+ images to the client browser using an ISAPI extension.
#include "StdAfx.h"
#include "gdipisapi.h"

int CGDIpISAPI::GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
	using namespace Gdiplus;
	UINT  num = 0;          // number of image encoders
	UINT  size = 0;         // size of the image encoder array in bytes

	ImageCodecInfo* pImageCodecInfo = NULL;

	GetImageEncodersSize(&num, &size);
	if(size == 0)
		return -1;  // Failure

	pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
	if(pImageCodecInfo == NULL)
		return -1;  // Failure

	GetImageEncoders(num, size, pImageCodecInfo);

	for(UINT j = 0; j < num; ++j)
	{
		if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
		{
			*pClsid = pImageCodecInfo[j].Clsid;
			free(pImageCodecInfo);
			return j;  // Success
		}    
	}

	free(pImageCodecInfo);
	return -1;  // Failure
}

void CGDIpISAPI::WriteContext(LPCTSTR pszFormat,...)
{
	static TCHAR szBuffer[1024];
	
	if (!m_pCtxt)
		return;

	va_list arg_ptr;
	va_start(arg_ptr, pszFormat);
	vsprintf(szBuffer,pszFormat, arg_ptr);
	va_end(arg_ptr);

	*m_pCtxt<<szBuffer;
}

void CGDIpISAPI::DumpError(LPCTSTR pszFormat, ...)
{
	static TCHAR szBuffer[2048];

	if (!m_pCtxt)
		return;

	va_list arg_ptr;
	va_start(arg_ptr, pszFormat);
	vsprintf(szBuffer,pszFormat, arg_ptr);
	va_end(arg_ptr);

	CString str("<p>A error message has occured:</p>\r\n<p><b>");
	str+=szBuffer;
	str+="</b></p>\r\n";

	DWORD dwSize=(DWORD)str.GetLength();

	m_pServer->StartContent(m_pCtxt);
	*m_pCtxt<<str;
	m_pServer->EndContent(m_pCtxt);
}

void CGDIpISAPI::WriteImage()
{
	using namespace Gdiplus;
	USES_CONVERSION;

	IStream* pIStream = NULL;
	HGLOBAL hGlobal;
	HRESULT hResult;
	CString str;
	CString strHeader;
	SIZE_T size;
	EncoderParameters encoderParameters;
 
	if (!m_pCtxt)
		return;
	if (!m_pServer)
		return;
	if (!m_pImage)
		return;

	hGlobal = ::GlobalAlloc( GHND , 0);
	if (hGlobal == NULL)
	{
		DumpError("Could not allocate global handle\r\n");
		goto Exit;
	}

	hResult=::CreateStreamOnHGlobal(
		hGlobal,         //Memory handle for the stream object
		true,   //Whether to free memory when the 
                           // object is released
		&pIStream          //Address of output variable that 
                           // receives the IStream interface pointer
	);
	if (FAILED(hResult))
	{
		ISAPIASSERT(FALSE);
		goto Exit;
	}

	CLSID imgClsid;
	if (GetEncoderClsid(A2BSTR(GetImageTypeString()), &imgClsid) == -1)
	{
		ISAPIASSERT(FALSE);
		goto Exit;
	}

	if (m_eImageType == ImagePNG || m_eImageType == ImageJPEG)
	{
		encoderParameters.Count = 1;
		encoderParameters.Parameter[0].Guid = EncoderQuality;
		encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
		encoderParameters.Parameter[0].NumberOfValues = 1;
		encoderParameters.Parameter[0].Value = &m_ulQuality;

		// Save to IStream...
		m_pImage->Save(pIStream, &imgClsid, &encoderParameters);
	}
	else
		m_pImage->Save(pIStream, &imgClsid);

	if (m_pImage->GetLastStatus() != Gdiplus::Ok)
	{
		ISAPIASSERT(FALSE);
		goto Exit;
	}

	// getting size of hGlobal
	size = ::GlobalSize(hGlobal);
	// formulate a proper header
	m_pCtxt->m_bSendHeaders=FALSE;
	strHeader.Format(_T("HTTP/1.0 200 OK\r\n%s\r\nContent-length: %d\r\n\r\n"), GetImageTypeString(),size);
	m_pServer->AddHeader(m_pCtxt, strHeader);
	ISAPIVERIFY( m_pCtxt->m_pECB->WriteClient(
		m_pCtxt->m_pECB->ConnID, ::GlobalLock(hGlobal), &size, 0) );
	::GlobalUnlock(hGlobal);

Exit:
	// Cleaning..	
	if (pIStream)
		pIStream->Release();
	if (hGlobal)
		::GlobalFree(hGlobal);
}

LPCTSTR CGDIpISAPI::GetImageTypeString() const
{
	switch(m_eImageType)
	{
	case ImagePNG:
		return "image/png";
	case ImageJPEG:
		return "image/jpeg";
	case ImageBMP:
		return "image/bmp";
	default:
		return "text/plain";
	}
}

void CGDIpISAPI::WriteInfo()
{
	using namespace Gdiplus;
	USES_CONVERSION;

	IStream* pIStream = NULL;
	HGLOBAL hGlobal;
	HRESULT hResult;
	CString str;
	CString strHeader;
	SIZE_T size;
	EncoderParameters encoderParameters;

	if (!m_pCtxt)
		return;
	if (!m_pServer)
		return;
	if (!m_pImage)
		return;

	hGlobal = ::GlobalAlloc( GHND , 0);
	if (hGlobal == NULL)
	{
		DumpError("Could not allocate global handle\r\n");
		goto Exit;
	}

	hResult=::CreateStreamOnHGlobal(
		hGlobal,         //Memory handle for the stream object
		true,   //Whether to free memory when the 
                           // object is released
		&pIStream          //Address of output variable that 
                           // receives the IStream interface pointer
	);
	if (FAILED(hResult))
	{
		ISAPIASSERT(FALSE);
		goto Exit;
	}

	CLSID imgClsid;

	if (GetEncoderClsid(A2BSTR(GetImageTypeString()), &imgClsid) == -1)
	{
		ISAPIASSERT(FALSE);
		goto Exit;
	}


	if (m_eImageType == ImagePNG || m_eImageType == ImageJPEG)
	{
		encoderParameters.Count = 1;
		encoderParameters.Parameter[0].Guid = EncoderQuality;
		encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
		encoderParameters.Parameter[0].NumberOfValues = 1;
		encoderParameters.Parameter[0].Value = &m_ulQuality;

		// Save to IStream...
		m_pImage->Save(pIStream, &imgClsid, &encoderParameters);
	}
	else
		m_pImage->Save(pIStream, &imgClsid);

	if (m_pImage->GetLastStatus() != Gdiplus::Ok)
	{
		ISAPIASSERT(FALSE);
		goto Exit;
	}

	// getting size of hGlobal
	size = ::GlobalSize(hGlobal);
		
	m_pServer->StartContent(m_pCtxt);
	WriteContext("<table>\r\n");
	WriteContext("<tr><td>Image Type</td><td>%s</td></tr>\r\n",GetImageTypeString());
	WriteContext("<tr><td>Size</td><td>%dx%d</td></tr>\r\n", m_pImage->GetWidth(), m_pImage->GetHeight());
	WriteContext("<tr><td>Size (Bytes)</td><td>%d</td></tr>\r\n", size);
	WriteContext("</table>\r\n");
	m_pServer->EndContent(m_pCtxt);

Exit:
	// Cleaning..	
	if (pIStream)
		pIStream->Release();
	if (hGlobal)
		::GlobalFree(hGlobal);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Engineer
United States United States
Jonathan de Halleux is Civil Engineer in Applied Mathematics. He finished his PhD in 2004 in the rainy country of Belgium. After 2 years in the Common Language Runtime (i.e. .net), he is now working at Microsoft Research on Pex (http://research.microsoft.com/pex).

Comments and Discussions