圖像縮放,使用CImage實現。
[cpp]
// 用於縮放圖像
bool CDIGTLSView::myScale(float keyX, float keyY) //定義時曾將keyx,keyy定義為int,導致出錯
{
//程序編制:李立宗 [email protected]
//2012-8-6
keyX=keyX;
keyY=keyY;
//srand((unsigned)time(NULL));
//float keyX,keyY; //放大倍數
//keyX=((rand()%20+1)/10.0);
//keyY=((rand()%20+1)/10.0);
//調試好久總是出問題,後來發現開始時key的值可能出現0,所以有問題,切記!!!
//2012-8-6
// CString str;
//str.Format(TEXT("%f"),key);
//MessageBox(str);
if(!myImage2.IsNull())
myImage2.Destroy();
if(!myImage1.IsNull())
myImage1.Destroy();
if(myImage1.IsNull())
//OnOpenResourceFile();
myImage1.LoadFromResource(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_BITMAP3));
//OnOpenCustomeFile();
//myImage2.Destroy();
if(myImage2.IsNull())
myImage2.Create((int)(myImage1.GetWidth()*keyX),(int)(myImage1.GetHeight()*keyY),24,0);
COLORREF pixel;
int maxY = myImage2.GetHeight();
int maxX=myImage2.GetWidth();
//int maxX1=myImage1.GetWidth();
//int maxY1=myImage1.GetHeight();
//CString str;
//str.Format(TEXT("%d"),maxX);
//MessageBox(str);
//str.Format(TEXT("%d"),maxX1);
//MessageBox(str);
//str.Format(TEXT("%d"),maxY);
//MessageBox(str);
//str.Format(TEXT("%d"),maxY1);
//MessageBox(str);
int r,g,b,avg;
double c;
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"),pit2);
//MessageBox(str);
//str.Format(TEXT("%d"),pit2);
//MessageBox(str);
int bitCount=myImage1.GetBPP()/8;
int bitCount2=myImage2.GetBPP()/8;
//CString str;
//str.Format(TEXT("%d"),bitCount);
//MessageBox(str);
//str.Format(TEXT("%d"),bitCount2);
//MessageBox(str);
int newValue;
int tempR,tempG,tempB;
int newX,newY;
for (int y=0; y<maxY; y++) {
for (int x=0; x<maxX; x++) {
newX=(int)(x/keyX);
newY=(int)(y/keyY);
tempR=(int)(int)(*(pRealData+pit*newY+newX*bitCount));
if(bitCount==1)
{
tempG=tempR;
tempB=tempR;
}
else
{
tempG=(int)(int)(*(pRealData+pit*newY+newX*bitCount+1));
tempB=(int)(int)(*(pRealData+pit*newY+newX*bitCount+2));
}
*(pRealData2+pit2*y+x*bitCount2)=tempR;
*(pRealData2+pit2*y+x*bitCount2+1)=tempG;
*(pRealData2+pit2*y+x*bitCount2+2)=tempB;
}
}
Invalidate();
return false;
}
作者;superdont