BCB中XFORM旋转图像问题
我使用gdi中XFORM旋转图像,图像旋转中心点偏移图像中心位置,下面是过程,我已经使用了坐标转换以原图中心点为旋转中心,但是效果不行,总是偏。请大家帮忙看看如何将旋转中心调到图像中心,谢谢了。
代码如下:
Graphics::TBitmap *bitmap1 = new Graphics::TBitmap();Graphics::TBitmap *bitmap2 = new Graphics::TBitmap();bitmap1 -> Assign(Image1 -> Picture -> Bitmap);float angle = -(TrackBar2 -> Position); //TrackBar2当前位置表示选择角度Label3 -> Caption = IntToStr(TrackBar2 -> Position) + "°";float RADIAN = (PI * angle) / 180 ; //均转为弧度float cosone = (float)cos(RADIAN);float sinone = (float)sin(RADIAN);float point1x = (-bitmap1 -> Height * sinone); //计算旋转后的点float point1y = (bitmap1 -> Height * cosone);float point2x = (bitmap1 -> Width * cosone - bitmap1 -> Height * sinone);float point2y = (bitmap1 -> Height * cosone + bitmap1 -> Width * sinone);float point3x = (bitmap1 -> Width * cosone);float point3y = (bitmap1 -> Width * sinone);float minx = min((float)0, min(point1x, min(point2x, point3x)));float miny = min((float)0, min(point1y, min(point2y, point3y)));float maxx = max((float)0, max(point1x, max(point2x, point3x)));float maxy = max((float)0, max(point1y, max(point2y, point3y)));int pstW = (int)ceil(maxx - minx); //计算bitmap2的长和宽int pstH = (int)ceil(maxy - miny);bitmap2 -> Height = pstH;bitmap2 -> Width = pstW;SetGraphicsMode(bitmap2 -> Canvas -> Handle, GM_ADVANCED); //改变模式XFORM xform;float cenx = (bitmap1 -> Width) / 2; //原图中心float ceny = (bitmap1 -> Height) / 2;xform.eM11 = cosone; //从Bitmap2反变换到Bitmap1xform.eM12 = sinone;xform.eM21 = -sinone;xform.eM22 = cosone;xform.eDx = (float)(cenx - cenx * cosone + ceny * sinone); //旋转中心xform.eDy = (float)(ceny - ceny * cosone - cenx * sinone);SetWorldTransform(bitmap2 -> Canvas -> Handle, &xform);BitBlt(bitmap2 -> Canvas -> Handle, //从Bitmap1搬迁数据到Bitmap2 0, 0, bitmap2 -> Width, bitmap2 -> Height, bitmap1 -> Canvas -> Handle, 0, 0, SRCCOPY);Image2 -> Picture -> Bitmap = bitmap2;//Image2 -> Left = (Image2 -> Width) / 2; //在Image2中居中显示//Image2 -> Top = (Image2 -> Height) / 2;delete bitmap1;delete bitmap2;
XFORM xform; //创建xform,进行旋转float oldcenx = (bitmap1 -> Width) / 2; //原图中心点作为旋转中心float oldceny = (bitmap1 -> Height) / 2;float newcenx = (bitmap2 -> Width) / 2;float newceny = (bitmap2 -> Height) / 2;xform.eM11 = cosone; //角度旋转余弦(水平缩放分量,水平反射分量)xform.eM12 = sinone; //角度旋转正弦(水平修剪比例常量)xform.eM21 = -sinone; //角度旋转正弦取负(垂直修剪比例常量)xform.eM22 = cosone; //角度旋转余弦(垂直缩放分量,垂直反射分量)xform.eDx = (float)(newcenx - oldcenx * cosone + oldceny * sinone); //逻辑单位下水平平移分量xform.eDy = (float)(newceny - oldceny * cosone - oldcenx * sinone); //逻辑单位下垂直平移分量