XFORM图像旋转有关问题
XFORM图像旋转问题
但是图像旋转中心不是图像几何中心,有些偏左上,如何设置为中心呢?
效果如下:
------解决方案--------------------
具体没做过
看代码
- C/C++ code
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反变换到Bitmap1 xform.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;
但是图像旋转中心不是图像几何中心,有些偏左上,如何设置为中心呢?
效果如下:
------解决方案--------------------
具体没做过
看代码
- C/C++ code
xform.eDx = (float)(cenx - cenx * cosone + ceny * sinone); //旋转中心 xform.eDy = (float)(ceny - ceny * cosone - cenx * sinone);