利用GDI+在内存中绘制图片

#include "GdiPlus.h"
using namespace Gdiplus;

//获取CLassID的函数
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
	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 draw()
{
        //初始化GDI+对象
	GdiplusStartupInput gdiplusStartupInput;
	ULONG_PTR gdiplusToken;
	GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
	{
                //创建内存BITMAP
		Bitmap bitmap(100,100,PixelFormat24bppRGB);
                //Graphics绘图对象
		Graphics* graph = Graphics::FromImage(&bitmap);
		Pen newPen( Color( 255, 0, 0 ), 3 );
                //绘制椭圆
		graph->DrawEllipse(&newPen,0,0,60,60);
		graph->Save();
		CLSID tifClsid;
                //获取类ID并保存成图片
		GetEncoderClsid(L"image/bmp", &tifClsid);
		bitmap.Save(TEXT("abce.bmp"),&tifClsid);
	}
	//关闭GDI+
	GdiplusShutdown(gdiplusToken);
}



你可能感兴趣的:(内存,绘制,GDI+)