目前就是按这种流程来走,一下是代码:
HRESULT hr;
if(m_pImagingFactory==NULL){
return;
}
IImage* pImage=NULL;
CDC *pPicDC=NULL;
CDC rotatedDC,memDC;
HBITMAP oldMemBitMap,oldRotateBitMap;
try{
CString pFileFullName=m_vfileList.at(m_iPicIndex);
hr=m_pImagingFactory->CreateImageFromFile(pFileFullName,&pImage);
if(FAILED(hr)){
goto finish;
}
RECT rectDraw;
GetClientRect(&rectDraw);
ImageInfo imageInfo;
pImage->GetImageInfo(&imageInfo);
RECT rectMem={0,0,imageInfo.Width,imageInfo.Height};
pPicDC =GetDC();
memDC.CreateCompatibleDC(pPicDC);
BITMAPINFO memBmi;
//memset(&memBmi, 0, sizeof(BITMAPINFO));
memBmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
memBmi.bmiHeader.biWidth = imageInfo.Width;
memBmi.bmiHeader.biHeight = imageInfo.Height;
memBmi.bmiHeader.biPlanes = 1;
memBmi.bmiHeader.biBitCount = 24;
memBmi.bmiHeader.biCompression = BI_RGB;
if ( m_hBitmapMem == NULL)
{
m_hBitmapMem =CreateDIBSection(memDC.GetSafeHdc(), &memBmi, DIB_RGB_COLORS, (void **)&m_lpMemData, NULL, 0);
}
oldMemBitMap = (HBITMAP)memDC.SelectObject(m_hBitmapMem);
pImage->Draw(memDC,&rectMem,NULL);
pImage->Release();
pImage=NULL;
RECT rectRotated;//旋转后rect
rotatedDC.CreateCompatibleDC(pPicDC);
Invalidate();
UpdateWindow();
BITMAPINFO rotatedBmi;
//memset(&rotatedBmi, 0, sizeof(BITMAPINFO));
rotatedBmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
rotatedBmi.bmiHeader.biWidth = imageInfo.Height;
rotatedBmi.bmiHeader.biHeight = imageInfo.Width;
rotatedBmi.bmiHeader.biPlanes = 1;
rotatedBmi.bmiHeader.biBitCount = 24;
rotatedBmi.bmiHeader.biCompression = BI_RGB;
//旋转
switch(m_iAngle){
case 90:{//90度旋转
rectRotated.left=0;
rectRotated.top=0;
rectRotated.right=imageInfo.Height;
rectRotated.bottom=imageInfo.Width;
m_hBitmapRotate = CreateDIBSection(rotatedDC.GetSafeHdc(), &rotatedBmi, DIB_RGB_COLORS, (void **)&m_lpDstData, NULL, 0);
//oldRotateBitMap=(HBITMAP)rotatedDC.SelectObject(m_hBitmapRotate);
int memStride = WIDTHBYTES(imageInfo.Width*memBmi.bmiHeader.biBitCount);
int rotateStride = WIDTHBYTES(imageInfo.Height*rotatedBmi.bmiHeader.biBitCount);
int index;
int index2;
for(int i=0;i<(int)imageInfo.Height;i++){
for (int j=0;j<(int)imageInfo.Width;j++)
{
index = i*memStride+j*3;
index2= ((int)imageInfo.Width-j)*rotateStride+i*3;
BYTE R=0,B=0,G=0;
R=m_lpMemData[index];
B=m_lpMemData[index+1];
G=m_lpMemData[index+2];
m_lpDstData[index2] = R;
m_lpDstData[index2+1]= B;
m_lpDstData[index2+2]= G;
}
}
m_iAngle+=90;
oldRotateBitMap=(HBITMAP)rotatedDC.SelectObject(m_hBitmapRotate);
break;
}
case 180:{//180度旋转
//rectRotated.left=0;
//rectRotated.top=0;
//rectRotated.right=imageInfo.Width;
//rectRotated.bottom=imageInfo.Height;
//BITMAPINFO rotatedBmi;
////memset(&rotatedBmi, 0, sizeof(BITMAPINFO));
//rotatedBmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
//rotatedBmi.bmiHeader.biWidth = imageInfo.Width;
//rotatedBmi.bmiHeader.biHeight = imageInfo.Height;
//rotatedBmi.bmiHeader.biPlanes = 1;
//rotatedBmi.bmiHeader.biBitCount = 24;
//rotatedBmi.bmiHeader.biCompression = BI_RGB;
//m_hBitmapRotate = ::CreateDIBSection(rotatedDC.GetSafeHdc(), &rotatedBmi, DIB_RGB_COLORS, (void **)&m_lpDstData, NULL, 0);
//oldRotateBitMap=(HBITMAP)rotatedDC.SelectObject(m_hBitmapRotate);
//int memStride = WIDTHBYTES(imageInfo.Width*memBmi.bmiHeader.biBitCount);
//int rotateStride = WIDTHBYTES(imageInfo.Width*rotatedBmi.bmiHeader.biBitCount);
//int index;
//int index2;
//for(int i=0;i<(int)imageInfo.Height;i++){
// for (int j=0;j<(int)imageInfo.Width;j++)
// {
// index = i*memStride+j*3;
// index2= (imageInfo.Height-i)*rotateStride+(imageInfo.Width-j)*3;
// m_lpDstData[index2] = m_lpMemData[index];
// m_lpDstData[index2+1]= m_lpMemData[index+1];
// m_lpDstData[index2+2]= m_lpMemData[index+2];
// }
//}
//m_iAngle+=90;
break;
}
case 270:{//180度旋转
m_iAngle+=90;
break;
}
case 360:{//180度旋转
m_iAngle=90;
break;
}
}
//居中
if(rectDraw.bottom>rectRotated.bottom){
rectDraw.top=rectDraw.top+(rectDraw.bottom-rectDraw.top-rectRotated.bottom)/2;
rectDraw.bottom = rectDraw.top+rectRotated.bottom;
}
if(rectDraw.right>rectRotated.right){
rectDraw.left = rectDraw.left+(rectDraw.right-rectDraw.left-rectRotated.right)/2;
rectDraw.right=rectDraw.left+rectRotated.right;
}
//StretchDIBits(pPicDC->GetSafeHdc(),rectDraw.left,rectDraw.top,(rectDraw.right-rectDraw.left),(rectDraw.bottom-rectDraw.top),0,0,rectRotated.right,rectRotated.bottom,m_lpDstData, &rotatedBmi,DIB_RGB_COLORS,SRCCOPY);
pPicDC->StretchBlt(rectDraw.left,rectDraw.top,(rectDraw.right-rectDraw.left),(rectDraw.bottom-rectDraw.top),&rotatedDC,0,0,rectRotated.right,rectRotated.bottom,SRCCOPY);
}
catch(...){
printf("sdf");
goto finish;
}
finish:
memDC.SelectObject(oldMemBitMap);
rotatedDC.SelectObject(oldRotateBitMap);
DeleteObject(m_hBitmapMem);
m_hBitmapMem = NULL;
DeleteObject(m_hBitmapRotate);
rotatedDC.DeleteDC();
memDC.DeleteDC();
if(pPicDC!=NULL){
ReleaseDC(pPicDC);
}
if(pImage!=NULL){
pImage->Release();
pImage=NULL;
}
if(m_lpDstData!=NULL){
//free(lpDstData);
m_lpDstData=NULL;
}
if(m_lpMemData!=NULL){
//free(lpDstData);
m_lpMemData=NULL;
}
}