本文將介紹如何將一張位圖旋轉90度。向工程添加一個Timage控件,取名為Image1。
工作原理是:創建一個位圖緩沖區用於存儲中間量,將原位圖的每一行的像素轉換為每一列然後存放在我們創建的位圖緩沖區中。最後,將旋轉後的位圖從緩沖區存回原位圖。
//定義緩沖位圖並剪切圖形區域
Graphics::Tbitmap *bufferbitmap=new Graphics::Tbitmap();
bufferbitmap->Width=Image1->Height;
bufferbitmap->Height=Image1->Width;
static Trect sourcepix,destpix,fullbufferimage,fulldestimage;
fullbufferimage.Left= 0;
fullbufferimage.Top= bufferbitmap->Height;
fullbufferimage.Right= bufferbitmap->Width;
fullbufferimage.Bottom= 0;
//旋轉並逐像素地拷貝原位圖到緩沖位圖
for (int y=0; y<Image1->Height; y++)
{
for (int x=0; x<Image1->Width; x++)
{
sourcepix.Left= x;
sourcepix.Top= y+1;
sourcepix.Right= x+1;
sourcepix.Bottom= y;
destpix.Left=y;
destpix.Top=bufferbitmap->Height-x;
destpix.Right=y+1;
destpix.Bottom=bufferbitmap->Height-x-1;
bufferbitmap->Canvas->CopyRect(destpix,Image1->Canvas,sourcepix);
}
}
//調整原位圖的尺寸並拷貝旋轉後的緩沖位圖到原位圖
Image1->Width=bufferbitmap->Width;
Image1->Height=bufferbitmap->Height;
Image1->Picture->Bitmap->Width=bufferbitmap->Width;
Image1->Picture->Bitmap->Height=bufferbitmap->Height;
Image1->Canvas->CopyRect(fullbufferimage,bufferbitmap->Canvas,fullbufferimage);