-1

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?

Rotated image Original image

Evgeny Belov
  • 308
  • 4
  • 12
  • 1
    There is no extra whitespace around your image. [Just rotated corners](https://i.imgur.com/OS3jLP2.png). If you want to add a crop function, that's a whole different matter. Especially with the source being a jpeg file... – Nyerguds Feb 11 '18 at 00:26
  • 1
    There is no way to do this without analyzing the actual image. – TaW Feb 11 '18 at 13:34

1 Answers1

0
  1. 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)));
    
  2. 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 ...

  3. 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:

enlarge

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...

Spektre
  • 49,595
  • 11
  • 110
  • 380
  • Ok, so the background is red just to show that there is extra padding on each side of rotated image. I also tried many other solutions (including Matrix class) and they all result in those whitespace around rototed image. – Evgeny Belov Feb 10 '18 at 12:22
  • @EugeneBelov have you tried to apply all 3 bullets in my answer? to check the **#3** open your source image in paint and flood fill the blank space with red if the artifacts are there too you got wrong source image ... and can attemt to repair it manually but do not use JPG afterwards... – Spektre Feb 11 '18 at 09:07
  • @TaW why do you think they miss the point? my bet is the #3 is the case and at least #1 is valid as (s)he is using wrong formula ... #2 is only a possibility as we do not see the rotation source code ... But I smell you got another possible reason for this behavior/artefacts ... – Spektre Feb 11 '18 at 14:27
  • 1
    Despite the misleading title the question imo is only about the size of the resulting image. But since OP only calculates the the corners there will always be 'white' space around rounded shapes. No easy way to calculate the cropped size beforehand. Best to crop after the event. – TaW Feb 11 '18 at 14:50
  • 1
    @TaW added edit1 to reflect that (I was misinterpreting the question as I always saw just the white artifacts on the edges below and above of the ring which are also white...) – Spektre Feb 13 '18 at 08:18