Click here to Skip to main content
15,886,199 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.3K   424   26  
A class wrapper to write GDI+ images to the client browser using an ISAPI extension.
#include "StdAfx.h"
#include "gdipexample.h"
#include "GDIpISAPI.h"

void CGdipExample::SetSize(int iWidth,int iHeight)
{
	m_size = CSize( __max( 1, __min( 1024, iWidth)), __max( 1, __min( 1024, iHeight)) );  
}


void CGdipExample::DrawStuff(Gdiplus::Image& image)
{
	using namespace Gdiplus;
	USES_CONVERSION;
	CString str,str2;


	if (!m_pServer || !m_pCtxt)
		return;

	DWORD dwBuffer=1024;
	int w=image.GetWidth();
	int h= image.GetHeight();

	Graphics graphics(&image);

	// Create a Pen object.
	LinearGradientBrush white(
		Point(0, 0),	
		Point(w,h),
		Color(255, 255, 255),   // opaque red
		Color(220, 220, 220));  // opaque blue
	LinearGradientBrush redyellow(
		Point(0, 0),	
		Point(w,h),
		Color(0,0,255,0),   // opaque red
		Color(200, 255, 255,0));  // opaque blue

	SolidBrush blue(Color(0, 0, 50));
	Pen black(Color(0, 0, 0), 3);
	FontFamily  fontFamily(L"Tahoma");
	Font        font(&fontFamily, 14, FontStyleRegular, UnitPixel);

	graphics.FillRectangle(&white, 0,0,w, h);
	graphics.FillRectangle(&redyellow, 5,5,50, h-10);
	graphics.DrawRectangle(&black, 5,5,w-10, h-10);

	str+="This image has been generated by GDI+ (on the server)\r\n and sent to your browser using an ISAPI extension.\r\n\r\n";
	str+="Time/Date: "; str+=CTime::GetCurrentTime().Format("%b-%d-%Y, %H:%M:%S"); str+="\r\n";
	m_pCtxt->GetServerVariable(_T("REMOTE_HOST"), str2.GetBufferSetLength(dwBuffer), &dwBuffer);
	str2.ReleaseBuffer();
	str+="Client adress: "; str+=str2; str+="\r\n";
	str+="Type: "; str+=m_sImageType; str+="\r\n";
	str2.Format("%dx%d",w, h);
	str+="Size (pixels): "; str+=str2; str+="\r\n";
	str+="Method: "; str+=m_pCtxt->m_pECB->lpszMethod; str+="\r\n";
	str+="Coded by Jonathan de Halleux, dehalleux@auto.ucl.ac.be\r\n";
	LPWSTR lpwz = A2W(str);
	graphics.DrawString(lpwz, -1, &font, PointF((REAL)30, (REAL)30), &blue);
	graphics.Flush(  FlushIntentionFlush);
}


void CGdipExample::Render()
{
	CSingleLock lock(&m_mutex);
	lock.Lock();
	if (lock.IsLocked())
	{
		Gdiplus::Bitmap bitmap(m_size.cx, m_size.cy);

		DrawStuff(bitmap);
	
		CGDIpISAPI gdip(m_pServer, m_pCtxt, &bitmap);
	
		m_sImageType.MakeLower();
		if (m_sImageType=="jpeg" || m_sImageType=="jpg")
			gdip.SetImageType(CGDIpISAPI::ImageJPEG);
		else if (m_sImageType=="bmp")
			gdip.SetImageType(CGDIpISAPI::ImageBMP);
		else
			gdip.SetImageType(CGDIpISAPI::ImagePNG);
	
		gdip.WriteImage();
		
		lock.Unlock();
	}
}

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