1
I am "translating" a program written in c# for Android, and I had a problem during the rotation of an image. The code works normally, but returns an image of size different from the original, I already understood why the new image has a different size, but how can I make this image back in the same size as the original, leaving out the tips of the image?
The rotated image is not displayed for the user, it is only used internally to perform some calculations.
public static Bitmap Rotate(Bitmap source, int anchorX, int anchorY, float angle)
{
Matrix matrix = new Matrix();
matrix.postRotate(angle, anchorX, anchorY);
return = Bitmap.createBitmap(source, 0, 0, source.getWidth(),
source.getHeight(), matrix, true);
}
Original code of C#:
public static Bitmap Rotate(Bitmap image, Point offset, float angle)
{
if (image == null) return null;
Bitmap rotatedBmp = new Bitmap(image.Width, image.Height);
rotatedBmp.SetResolution(image.HorizontalResolution, image.VerticalResolution);
Graphics g = Graphics.FromImage(rotatedBmp);
g.TranslateTransform(offset.X, offset.Y);
g.RotateTransform(angle);
g.TranslateTransform(-offset.X, -offset.Y);
g.DrawImage(image, new PointF(0, 0));
return rotatedBmp;
}
What is the original code in C#?
– brazilianldsjaguar
public Static Bitmap Rotate(Bitmap image, Point offset, float Angle) { if (image == null) Return null; Bitmap rotatedBmp = new Bitmap(image.Width, image.Height); rotatedBmp.Setresolution(image.Horizontalresolution, image.Verticalresolution); Graphics g = Graphics.Fromimage(rotatedBmp);&#xTo; g.Translatetransform(offset.X, offset.Y); g.Rotatetransform(Angle); g.Translatetransform(-offset.X, -offset.Y); g.Drawimage(image, new Pointf(0, 0)); Return rotatedBmp; }
– Christian Beregula