在线求助VC6下添加GDI+出错
我的目的是bmp转jpg.
已经包含了相关的h和lib,dll也拷贝了。
我是照着这个博客(http://blog.csdn.net/lingang_/article/details/6186644)来弄的,下面是代码,就一个win32工程(一个cpp文件)。
出错提示如下:
Compiling...
main.cpp
e:\vctest\bmptojpg\main.cpp(56) : error C2664: 'GetImageCLSID' : cannot convert parameter 1 from 'char [11]' to 'const unsigned short *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.
BmpToJpg.exe - 1 error(s), 0 warning(s)
代码如下:
#include <afxwin.h>
#include <afxext.h>
#include <afxdisp.h>
#include <afxdtctl.h>
#ifndef ULONG_PTR
#define ULONG_PTR unsigned long*
#include "GdiPlus.h"
using namespace Gdiplus;
#pragma comment(lib, "gdiplus.lib")
#include <atlconv.h>
#endif
#include <iostream>
using namespace std;
MBmpToMImage(CFile& cbfBmp, CFile& cbfImage, CString strType);
GetImageCLSID(const WCHAR* format, CLSID* pCLSID);
GdiplusStartupInput m_gdiplusStartupInput;
ULONG_PTR m_gdiplusToken;
int main()
{
// 初始化gdi+
// GdiplusStartup(&m_gdiplusToken, &m_gdiplusStartupInput, NULL);
// GdiplusShutdown(m_gdiplusToken);
return 0;
}
MBmpToMImage(CFile& cbfBmp, CFile& cbfImage, CString strType)
{
int iBmpSize = cbfBmp.GetLength();
HGLOBAL hMemBmp = GlobalAlloc(GMEM_FIXED, iBmpSize);
if (hMemBmp == NULL) return FALSE;
IStream* pStmBmp = NULL;
CreateStreamOnHGlobal(hMemBmp, FALSE, &pStmBmp);
if (pStmBmp == NULL)
{
GlobalFree(hMemBmp);
return FALSE;
}
BYTE* pbyBmp = (BYTE *)GlobalLock(hMemBmp);
cbfBmp.SeekToBegin();
cbfBmp.Read(pbyBmp, iBmpSize);
Image* imImage = NULL;
imImage = Image::FromStream(pStmBmp, FALSE);
if (imImage == NULL)
{
GlobalUnlock(hMemBmp);
GlobalFree(hMemBmp);
return FALSE;
}
USES_CONVERSION;
CLSID clImageClsid;
GetImageCLSID(_T("image/jpeg"), &clImageClsid);
HGLOBAL hMemImage = GlobalAlloc(GMEM_MOVEABLE, 0);
if (hMemImage == NULL)
{
pStmBmp->Release();
GlobalUnlock(hMemBmp);
GlobalFree(hMemBmp);
if (imImage != NULL) delete imImage;
return FALSE;
}
IStream* pStmImage = NULL;
CreateStreamOnHGlobal(hMemImage, TRUE, &pStmImage);
if (pStmImage == NULL)
{
pStmBmp->Release();
GlobalUnlock(hMemBmp);
GlobalFree(hMemBmp);
GlobalFree(hMemImage);
if (imImage != NULL) delete imImage;
return FALSE;
}
imImage->Save(pStmImage, &clImageClsid);
if (pStmImage == NULL)
{
pStmBmp->Release();
pStmImage->Release();
GlobalUnlock(hMemBmp);
GlobalFree(hMemBmp);
GlobalFree(hMemImage);
if (imImage != NULL) delete imImage;
return FALSE;
}
LARGE_INTEGER liBegin = {0};
pStmImage->Seek(liBegin, STREAM_SEEK_SET, NULL);
BYTE* pbyImage = (BYTE *)GlobalLock(hMemImage);
cbfImage.SeekToBegin();
cbfImage.Write(pbyImage, GlobalSize(hMemImage));
if (imImage != NULL) delete imImage;
pStmBmp->Release();
pStmImage->Release();
GlobalUnlock(hMemBmp);
GlobalUnlock(hMemImage);
GlobalFree(hMemBmp);
GlobalFree(hMemImage);
return TRUE;
}
GetImageCLSID(const WCHAR* format, CLSID* pCLSID)
{
UINT num = 0;
UINT size = 0;
ImageCodecInfo* pImageCodecInfo = NULL;
GetImageEncodersSize(&num, &size);
if(size == 0){
return FALSE;
}
pImageCodecInfo = (ImageCodecInfo *)(malloc(size));
if(pImageCodecInfo == NULL)
return FALSE;
GetImageEncoders(num, size, pImageCodecInfo);
// Find for the support of format for image in the windows
for(UINT i = 0; i < num; ++i)
{
//MimeType: Depiction for the program image
if( wcscmp(pImageCodecInfo[i].MimeType, format) == 0)
{
*pCLSID = pImageCodecInfo[i].Clsid;
free(pImageCodecInfo);
return TRUE;
}
}
free(pImageCodecInfo);
return FALSE;
}
[解决办法]
bmp转jpg
这个用CImage不就可以了么