I have spent pretty much time tryin to properly rotate an image around its center. The rotation angle in the example below is -61 degrees. I'm using this code to calculate image size after rotation
var rad = Math.Sin(angle * Math.PI / 180.0);
var x0 = width / 2; // rotate around center
var y0 = height / 2; // rotate around center
var x = width;
var y = height;
var newImgWidth = (float)(x0 + Math.Abs((x - x0) * Math.Cos(rad)) + Math.Abs((y - y0) * Math.Sin(rad)));
var newImgHeight = (float)(y0 + Math.Abs((x - x0) * Math.Sin(rad)) + Math.Abs((y - y0) * Math.Cos(rad)));
The result image has whitespaces above and below actual image but it should not. What am I doing wrong?
you are missing -
in the rotation equation
one of the sin therm should be -sin()
like this:
var newImgWidth = + (float)(x0 + Math.Abs((x - x0) * Math.Cos(rad)) + Math.Abs((y - y0) * Math.Sin(rad))); var newImgHeight = - (float)(y0 + Math.Abs((x - x0) * Math.Sin(rad)) + Math.Abs((y - y0) * Math.Cos(rad)));
see duplicate Rotating a square TBitmap on its center
Sorry can not link duplicate as close vote as it does not have any upvotes or accept as it was a newbie asker that has never cast any vote... and did not even till today
you need to loop through destination pixels instead of source otherwise holes will appear as your code does not have any loops I can only guess which image you loop through ...
your image is white and after rotation it is red
so you are using transparency? if yes and your image was compressed with lossy compression like JPG than the colors might be distorted causing these artifacts around edges as transparent color is no longer the same shade on whole transparent surface. To remedy this you should threshold transparent color back to single color value after decompression or use non degraded image (in BMP or PNG format)
So your problem might be any of the 3 bullets or even combined...
[Edit1]
In case TaW is right and you mean the size of the image instead of white space artifacts then that is caused by your rotation implementation. As corners of rotated image can enlarge the BBOX of the image in all directions from center of rotation. So to prevent cutting of image data the image is enlarged:
If you want to use original image size then disable the resize in your image rotation code (and handle access out of image bounds) or find BBOX after rotation and crop the image if you want some specific space around object instead.
In case you are rotating the same object a lot and want constant border around object you can speed up the process by computing OBB once and than just rotate its corners instead of computing BBOX per each rotation...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.