图像旋转使用CImage实现,此处旋转30度。具体实现如下:
void CDIGTLSView::OnTestTest() { //程序编制:李立宗 lilizong@gmail.com //2012-8-5 if(myImage1.IsNull()) OnOpenResourceFile(); if(myImage2.IsNull()){ myImage2.Create(myImage1.GetWidth(),myImage1.GetHeight(),24,0); } COLORREF pixel; int maxY = myImage1.GetHeight(); int maxX=myImage1.GetWidth(); byte* pRealData; byte* pRealData2; pRealData=(byte*)myImage1.GetBits(); pRealData2=(byte*)myImage2.GetBits(); int pit=myImage1.GetPitch(); int pit2=myImage2.GetPitch(); //需要注意,pit和pit2的值并不一样,所以如果使用一个值,会导致不同的结果出现 //CString str; //str.Format(TEXT("%d"),pit); //MessageBox(str); //str.Format(TEXT("%d"),pit2); //MessageBox(str); int bitCount=myImage1.GetBPP()/8; int bitCount2=myImage2.GetBPP()/8; int tempR,tempG,tempB; float const1,const2; float fCosa=cos(30*3.14/180); float fSina=sin(30*3.14/180); const1=(float)(-0.5*(maxX-1)*fCosa-0.5*(maxY-1)*fSina+0.5*(maxX-1)); const2=(float)(0.5*(maxX-1)*fSina-0.5*(maxY-1)*fCosa+0.5*(maxY-1)); int tempX,tempY; //说明:将生产的图像作为24位图处理。 for (int y=0; y<maxY; y++) { for (int x=0; x<maxX; x++) { tempY=-(float)x*fSina+(float)y*fCosa+const2+0.5; tempX=(float)x*fCosa+(float)y*fSina+const1+0.5; if(tempY>=0&&tempY+2<=maxY&&tempX>=0&&tempX+2<=maxX) { tempR=(int)(int)(*(pRealData+pit*tempY+tempX*bitCount)); if(bitCount==1) {tempG=tempR; tempB=tempR;} else { tempG=(int)(int)(*(pRealData+pit*tempY+tempX*bitCount+1)); tempB=(int)(int)(*(pRealData+pit*tempY+tempX*bitCount+2)); tempG=0; tempB=0; } } else { tempR=255; tempG=0; tempB=0; } *(pRealData2+pit2*y+x*bitCount2)=tempR; *(pRealData2+pit2*y+x*bitCount2+1)=tempG; *(pRealData2+pit2*y+x*bitCount2+2)=tempB; } } Invalidate(); }