IImage *pIImage;
IImagingFactory *pIImgFactory;
ImageInfo imageInfo;
HBITMAP hResult;
INT nImgWidth = GetSystemMetrics(SM_CXSCREEN);
INT nImgheight = GetSystemMetrics(SM_CYSCREEN);
CoInitializeEx(NULL,COINIT_MULTITHREADED);
if (!SUCCEEDED(CoCreateInstance(CLSID_ImagingFactory,NULL,CLSCTX_INPROC_SERVER,IID_IImagingFactory,(void **)&pIImgFactory)))
{
return;
}
if (!SUCCEEDED(pIImgFactory-> CreateImageFromFile(_T( "1.jpg "),&pIImage)))
{
return;
}
程序运行到CreateImageFromFile()出错,
找到原因
在PB5.0的Help里
Windows CE Features > Graphics and Multimedia Technologies > Graphics > Imaging > Imaging Reference > Imaging Interfaces > IImagingFactory
可以找到你要的API信息
我在定制系统时加入Still Image Decoders文件夹里和Still Image Encoders文件夹里的组件,问题得以解决,所以运行到CreateImageFromFile,运行不下去返回出错信息的原因在定制系统时缺少组件不是代码问题。
void CTestSaveJPGDlg::Convertbmp()
{
// TODO: 在此添加控件通知处理程序代码
//用于将test.jpg转换成test.gif
HRESULT hr;
IImagingFactory * pImagingFactory = NULL ; //Image工厂接口对象
IStream *pStream = NULL; // 流接口对象
IImageSink *pImageSink = NULL; //Image Sink接口对象
IImageDecoder *pImageDecoder = NULL; //解码器接口对象
IImageEncoder *pImageEncoder = NULL; //编码器接口对象
CLSID clsidEncoder; //编码器CLSID
TCHAR *tszInFileName, *tszOutFileName, *tszMime;
tszInFileName = L"\\Storage Card\\1234.bmp"; //指定待转换的图象文件
tszOutFileName = L"\\Storage Card\\Test111.png"; //指定转换后的图象文件
tszMime = L"image/png"; //指定转换后,图象文件的格式
//初始化COM环境
if (FAILED(hr = CoInitializeEx(NULL, COINIT_MULTITHREADED)))
{
TRACE(L"COINIT_MULTITHREADED ERROR");
return;
}
//得到Image工厂接口对象
hr = CoCreateInstance(CLSID_ImagingFactory,
NULL,
CLSCTX_INPROC_SERVER,
IID_IImagingFactory,
(void**) &pImagingFactory);
if (FAILED(hr))
{
TRACE(L"IMAGE FACTORY CREATED ERROR");
goto finish;
}
//将文件内容加载到流中
if (FAILED(hr = CreateStreamOnFile(tszInFileName, &pStream)))
{
goto finish;
}
//根据编码器类型名称得到编码器CLSID
if (!GetEnCodecCLSID(pImagingFactory,tszMime, &clsidEncoder ))
{
goto finish;
}
//创建编码器接口,并关联到输出文件中
if (FAILED(hr = pImagingFactory->CreateImageEncoderToFile(&clsidEncoder, tszOutFileName, &pImageEncoder)))
{
goto finish;
}
//创建解码器接口
if (FAILED(hr = pImagingFactory->CreateImageDecoder(pStream, DecoderInitFlagBuiltIn1st, &pImageDecoder)))
{
goto finish;
}
//得到编码器接口的sink对象。此ImageSink接口作为一个槽或者管道来理解;
//是用于负责pImageEncoder和pImageDecoder之间的传输
if (FAILED(hr = pImageEncoder->GetEncodeSink(&pImageSink)))
{
goto finish;
}
//开始解码
if (FAILED(hr = pImageDecoder->BeginDecode(pImageSink, NULL)))
{
goto finish;
}
//循环解码,直到结束
for(;;)
{
//解码
hr = pImageDecoder->Decode();
//继续解码后面的部分
if (E_PENDING == hr)
{
Sleep(500);
}
//失败
else if (FAILED(hr))
{
//终止解码
pImageDecoder->EndDecode(hr);
goto finish;
}
else
{
//解码成功
break;
}
}
//结束解码
pImageDecoder->EndDecode(hr);
//释放pImageSink对象
pImageSink->Release();
pImageSink = NULL;
//结束编码,此时就已经完成了文件格式的转换
pImageEncoder->TerminateEncoder();
finish:
//释放pStream对象
if (pStream)
pStream->Release();
//释放pImageSink对象
if (pImageSink)
pImageSink->Release();
//释放pImageDecoder对象
if (pImageDecoder)
pImageDecoder->Release();
//释放pImageEncoder对象
if (pImageEncoder)
pImageEncoder->Release();
//释放IImagingFactory接口对象
if (pImagingFactory)
pImagingFactory->Release();
//释放程序占用的COM资源
CoUninitialize();
}