19,468
社区成员
发帖
与我相关
我的任务
分享
#include <gdiplus.h>
#pragma comment(lib, "gdiplus.lib")
using namespace Gdiplus;
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
}
//传入窗口句柄获取其DC来绘图
VOID SaveImage(HWND hwnd)
{
// Initialize GDI+.
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
{
HDC hdc = ::GetDC(hwnd);
Graphics graphics(hdc);
// Create an Image object based on a PNG file.
Image image(L"E:\\Mosaic.jpg");
// Draw the image.
graphics.DrawImage(&image, 10, 10);
// Construct a Graphics object based on the image.
Graphics imageGraphics(&image);
// Alter the image.
SolidBrush brush(Color(255, 0, 0, 255));
imageGraphics.FillEllipse(&brush, 20, 30, 80, 50);
// Draw the altered image.
graphics.DrawImage(&image, 200, 10);
// Save the altered image.
CLSID tifClsid;
GetEncoderClsid(L"image/tiff", &tifClsid);
image.Save(L"E:\\Mosaic2.tif", &tifClsid, NULL);
}
GdiplusShutdown(gdiplusToken);
}