I was working on an image processing code in C#. Had to rotate an image by few degrees around a specified point. This can be done either by transformation or matrix method.
private Bitmap rotateImage(Bitmap b, int x, int y, float angle)
{
//create a new empty bitmap to hold rotated image
Bitmap returnBitmap = new Bitmap(b.Width, b.Height);
//make a graphics object from the empty bitmap
Graphics g = Graphics.FromImage(returnBitmap);
Matrix m = new Matrix();
m.RotateAt((float)angle, new PointF(x,y));
g.Transform = m;
g.DrawImage(b, 0, 0);
return returnBitmap;
}
However this wont work properly. It rotates the image, but strangely enough at the same time scales it down too (on larger images).
After some digging into MSDN, found this page, http://msdn.microsoft.com/en-us/library/1bttkazd.aspx, which says:
GDI+ may automatically scale an image as you draw it, which would decrease performance. Alternatively, you can control the scaling of the image by passing the dimensions of the destination rectangle to the DrawImage method.So to avoid scaling, the above DrawImage line should be changed to
g.DrawImage(b, 0, 0, b.Width, b.Height);
I have no idea why any function would have "automatic" behaviour. But this is how it works. Weird.
That was awesome. You surely are very particular with C#. Will that works in other language? I love to try that, maybe you could help me embedding it to php? What do you think?
ReplyDeleteComputer support
setting image rotation in C#
ReplyDelete