// TestGdiPlus.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <Windows.h>
#include <string>
using namespace std;
#include <GdiPlus.h>
#pragma comment(lib, "gdiplus.lib")
using namespace Gdiplus;
//获取特定图片类型的编码
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
UINT num = 0, size = 0;
Gdiplus::GetImageEncodersSize(&num, &size);
if(size == 0)
return -1; // Failure
Gdiplus::ImageCodecInfo* pImageCodecInfo = (Gdiplus::ImageCodecInfo*)(malloc(size));
Gdiplus::GetImageEncoders(num, size, pImageCodecInfo);
bool found = false;
for (UINT ix = 0; !found && ix < num; ++ix)
{
if (_wcsicmp(pImageCodecInfo[ix].MimeType, format) == 0)
{
*pClsid = pImageCodecInfo[ix].Clsid;
found = true;
break;
}
}
free(pImageCodecInfo);
return found;
}
bool SavePngToPngFile(LPCTSTR lpszOldPicFileName, LPCTSTR lpszPicFileName)
{
if (lpszOldPicFileName == NULL || lpszPicFileName == NULL )
{
return false;
}
Gdiplus::Bitmap* pBitmap = NULL;
Gdiplus::Bitmap* pWrapBitmap = NULL;
do
{
pWrapBitmap = Gdiplus::Bitmap::FromFile( lpszOldPicFileName );
if (!pWrapBitmap)
{
break;
}
Gdiplus::BitmapData bitmapData;
Gdiplus::Rect rcImage(0, 0, pWrapBitmap->GetWidth(), pWrapBitmap->GetHeight());
pWrapBitmap->LockBits(&rcImage, Gdiplus::ImageLockModeRead, pWrapBitmap->GetPixelFormat(), &bitmapData);
DWORD *pStart = (DWORD*)bitmapData.Scan0;
for ( unsigned int i = 0; i < bitmapData.Width; i ++ )
{
for ( unsigned int j = 0; j < bitmapData.Height; j ++ )
{
DWORD& dwPix = pStart[ j * bitmapData.Stride / 4 + i];
DWORD dwRgb = dwPix & 0x00FFFFFF;
if ( dwRgb > 0x00888888 )
{
// 符合某些规则的颜色值做修改
dwPix = dwPix & 0xFF000000 | 0x0000FF00;
}
}
printf("\n" );
}
pBitmap = new (Gdiplus::Bitmap)(bitmapData.Width, bitmapData.Height, bitmapData.Stride, PixelFormat32bppARGB, (BYTE*)bitmapData.Scan0);
pWrapBitmap->UnlockBits(&bitmapData);
CLSID encoderCLSID;
GetEncoderClsid(_T("image/png"), &encoderCLSID);
Gdiplus::Status st= pBitmap->Save(lpszPicFileName ,&encoderCLSID, NULL);
if( st != Gdiplus::Ok )
break;
} while (false);
if (pBitmap)
{
delete pBitmap;
}
if (pWrapBitmap)
{
delete pWrapBitmap;
}
return true;
}
int _tmain(int argc, _TCHAR* argv[])
{
if ( argc != 3 )
{
wprintf( L"Usage:\n%s source.png target.png\n", argv[0] );
::system( "pause" );
return 1;
}
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
SavePngToPngFile( argv[1], argv[2] );
GdiplusShutdown(gdiplusToken);
return 0;
}