C# rotate bitmap 90 degrees

前端 未结 3 829
误落风尘
误落风尘 2020-12-03 06:10

I\'m trying to rotate a bitmap 90 degrees using the following function. The problem with it is that it cuts off part of the image when the height and width are not equal.

3条回答
  •  心在旅途
    2020-12-03 06:52

    The bug is in your first call to TranslateTransform:

    g.TranslateTransform((float)b.Width / 2, (float)b.Height / 2);
    

    This transform needs to be in the coordinate space of returnBitmap rather than b, so this should be:

    g.TranslateTransform((float)b.Height / 2, (float)b.Width / 2);
    

    or equivalently

    g.TranslateTransform((float)returnBitmap.Width / 2, (float)returnBitmap.Height / 2);
    

    Your second TranslateTransform is correct, because it will be applied before the rotation.

    However you're probably better off with the simpler RotateFlip method, as Rubens Farias suggested.

提交回复
热议问题