读书人

JPG转换成BMP格式的有关问题

发布时间: 2012-03-24 14:00:46 作者: rapoo

JPG转换成BMP格式的问题
用VC++6.0怎么编程实现JPG转换成BMP,求代码,论坛有过类似代码,不过#include <Gdiplus.h>
#include <GdiPlusEnums.h> 两个头文件打不开,不知道是在什么编译器下运行的,我要的是VC++6.谢谢

[解决办法]
//jpeglib.h
BOOL Jpeg2Bmp(LPCWSTR pJpgFileName, LPCWSTR pBmpFileName);
BOOL Bmp2Jpeg(LPCWSTR pBmpFileName, LPCWSTR pJpgFileName);

//jpeglib.cpp
#include "StdAfx.h"
#include "JpegLib.h"

#ifndef ULONG_PTR
#define ULONG_PTR unsigned long*
#endif

#include <Gdiplus.h>
#include <GdiPlusEnums.h>
using namespace Gdiplus;

#pragma comment(lib, "gdiplus.lib")

int GetCodecClsid(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));
pImageCodecInfo = (ImageCodecInfo*)(new BYTE[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;
return j; // Success
}
} // for

return -1; // Failure

} // GetCodecClsidTop

BOOL Jpeg2Bmp(LPCWSTR pJpgFileName, LPCWSTR pBmpFileName)
{
CLSID codecClsid;
EncoderParameters encoderParameters;
long quality;
Status stat;

// Get an image from the disk.
Image image(pJpgFileName);

// Get the CLSID of the JPEG codec.
GetCodecClsid(L"image/bmp", &codecClsid);

// Before we call Image::Save, we must initialize an
// EncoderParameters object. The EncoderParameters object
// has an array of EncoderParameter objects. In this
// case, there is only one EncoderParameter object in the array.
// The one EncoderParameter object has an array of values.
// In this case, there is only one value (of type LONG)
// in the array. We will set this value to 0, 50, and 100.

encoderParameters.Count = 1;
encoderParameters.Parameter[0].Guid = EncoderQuality;
encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
encoderParameters.Parameter[0].NumberOfValues = 1;

// Save the image as a JPEG with quality level 0.
quality = 0;
encoderParameters.Parameter[0].Value = &quality;
stat = image.Save(pBmpFileName, &codecClsid, &encoderParameters);

return stat == Ok;
}

BOOL Bmp2Jpeg(LPCWSTR pBmpFileName, LPCWSTR pJpgFileName)
{
CLSID codecClsid;
EncoderParameters encoderParameters;
long quality;
Status stat;

// Get an image from the disk.
Image image(pBmpFileName);

// Get the CLSID of the JPEG codec.
GetCodecClsid(L"image/jpeg", &codecClsid);

// Before we call Image::Save, we must initialize an
// EncoderParameters object. The EncoderParameters object
// has an array of EncoderParameter objects. In this
// case, there is only one EncoderParameter object in the array.
// The one EncoderParameter object has an array of values.
// In this case, there is only one value (of type LONG)
// in the array. We will set this value to 0, 50, and 100.

encoderParameters.Count = 1;
encoderParameters.Parameter[0].Guid = EncoderQuality;
encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
encoderParameters.Parameter[0].NumberOfValues = 1;

// Save the image as a JPEG with quality level 0.
quality = 0;
encoderParameters.Parameter[0].Value = &quality;
stat = image.Save(pJpgFileName, &codecClsid, &encoderParameters);

return stat == Ok;
}

///////////////
it's okay

读书人网 >VC/MFC

热点推荐