|
好,多谢ruritanian大哥的指点,已经可以完成GIF/PNG/JPG图像的旋转功能了,具体代码如下:
- //JPG图片旋转功能
- void CImagingDemoDlg::OnBnClickedRotate()
- {
- // TODO: 在此添加控件通知处理程序代码
- HRESULT hr ; //返回结果
- IImagingFactory * pImagingFactory =NULL; //Image工厂接口对象
- IImage * pImage = NULL,* pNewImage = NULL; //Image接口对象;新的IImage图像
- IBitmapImage * pbmpImg = NULL,* pNewbmpImg = NULL; //GIF/PNG/JPG图像;旋转后的图像
- IBasicBitmapOps * pbmpOp = NULL; //旋转操作
- ImageInfo imageInfo;
- FLOAT m_RAngle = 90;
- LPCTSTR szFileName = L"\\Saturn&Jupiter.gif"; //图象文件notice.png//Windows Embedded.jpg//test.jpg
- //初始化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;
- }
- //加载图象文件到IImage接口对象中
- hr = pImagingFactory->CreateImageFromFile(szFileName,&pImage);
- if (FAILED(hr))
- {
- TRACE(L"IMAGE LOAD ERROR");
- goto finish;
- }
- //获得原图片的基本信息
- pImage-> GetImageInfo(&imageInfo);
- //加载图像文件到IImage接口对象中
- pImagingFactory->CreateBitmapFromImage(pImage,imageInfo.Width,imageInfo.Height,imageInfo.PixelFormat,InterpolationHintDefault,&pbmpImg);
- //查询某个组件是否支持某个特定的接口,若支持则QueryInterface则返回一个指向特定接口的指针;若不支持则返回一个错误代码
- hr = pbmpImg->QueryInterface(IID_IBasicBitmapOps,(void **)&pbmpOp);
- //顺时针旋转
- pbmpOp->Rotate(m_RAngle,InterpolationHintDefault,&pNewbmpImg);
- //IBitmapImage对象->IImage对象
- hr = pNewbmpImg->QueryInterface(IID_IImage,(void **)&pNewImage);
- //开始绘制图象
- //得到设备环境
- CDC *pDC =GetDC();
- //定义绘制尺寸
- RECT rect;
- GetClientRect(&rect);
- //绘制图片
- //pImage->Draw(pDC->m_hDC, &rect, NULL);
- pNewImage->Draw(pDC->m_hDC, &rect, NULL);
- //释放设备环境
- ReleaseDC(pDC);
- finish:
- //释放IImage接口对象
- if (pImage)
- pImage->Release();
-
- //释放IImagingFactory接口对象
- if (pImagingFactory)
- pImagingFactory->Release();
- //释放程序占用的COM资源
- CoUninitialize();
- }
复制代码
|
|