本文将介绍如何将一张位图旋转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); |