【发布时间】:2021-12-01 15:28:59
【问题描述】:
我构建了这个示例来快速将图像旋转 90 度,但我总是在侧面切掉图像。经过多次测试,不幸的是我仍然不明白问题的原因。
void rotate()
{
Graphics::TBitmap *SrcBitmap = new Graphics::TBitmap;
Graphics::TBitmap *DestBitmap = new Graphics::TBitmap;
SrcBitmap->LoadFromFile("Crayon.bmp");
DestBitmap->Width=SrcBitmap->Width;
DestBitmap->Height=SrcBitmap->Height;
SetGraphicsMode(DestBitmap->Canvas->Handle, GM_ADVANCED);
double myangle = (double)(90.0 / 180.0) * 3.1415926;
int x0=SrcBitmap->Width/2;
int y0=SrcBitmap->Height/2;
double cx=x0 - cos(myangle)*x0 + sin(myangle)*y0;
double cy=y0 - cos(myangle)*y0 - sin(myangle)*x0;
xForm.eM11 = (FLOAT) cos(myangle);
xForm.eM12 = (FLOAT) sin(myangle);
xForm.eM21 = (FLOAT) -sin(myangle);
xForm.eM22 = (FLOAT) cos(myangle);
xForm.eDx = (FLOAT) cx;
xForm.eDy = (FLOAT) cy;
SetWorldTransform(DestBitmap->Canvas->Handle, &xForm);
BitBlt(DestBitmap->Canvas->Handle,
0,
0,
SrcBitmap->Width,
SrcBitmap->Height,
SrcBitmap->Canvas->Handle,
0,
0,
SRCCOPY);
DestBitmap->SaveToFile("Crayon2.bmp");
delete DestBitmap;
delete SrcBitmap;
}
【问题讨论】:
-
旋转矩形具有更大的 AABB,因为您将目标位图设置为与逻辑上无法容纳的源相同大小并且它的剪切。您必须将目标位图大小设置为正方形,大小等于原始位图外接圆的直径(对角线大小),所以
a = sqrt(width^2 + height^2) -
谢谢,我也尝试过这个建议,但是:对于 400x3000 像素的图像,如果我设置它,会夸大:DestBitmap-> Width = 6000;目标位图-> 高度 = 6000;我得到一张 6000x6000 像素的图像,但图像的“有用”区域变为 3500x3000 像素。在实践中,图像的宽度被压缩了 500 像素,图像看起来被压扁了。
-
“我总是在侧面剪掉图像” 这很难理解。你可以用一张图片来证明这个问题。你没有改变宽度/高度,所以除了图像会被裁剪。
-
旋转后:link
标签: c++ windows c++builder