GDI+ BitMap save 保存图像

一、显示图像并保存

CImage image1;//不知道为什么,没有这句不能显示图像
image1.Load(L"");//不知道为什么,没有这句不能显示图像
CDC* pDC = GetDC();
Graphics graph(pDC->GetSafeHdc());
Bitmap *image = new Bitmap(_T("d://2.jpg"));
graph.DrawImage(image, 0, 0);
CLSID pngClsid;
GetEncoderClsid(L"image/bmp", &pngClsid);
image->Save(L"d:\\Mosaic2.bmp", &pngClsid, NULL);

二、函数 GetEncoderClsid

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
}

参考一:https://stackoverflow.com/questions/1584202/gdi-bitmap-save-problem

参考二:https://msdn.microsoft.com/en-us/library/windows/desktop/ms533843(v=vs.85).aspx

你可能感兴趣的:(c++)