How to rotate image with negative values
I am using the below code in server side to rotate my images ,it works fine for the angle like 5,10,20..360. In the same project I have to rotate images whose angle are already saved in database from Java application. It contains values like -1,-1.23,1.4543545 etc. and its not rotating with my below function.
UPDATED/WORKING CODE
public static Bitmap rotateCenter(string imagepath,Bitmap bmpSrc, float theta)
{
Unit Width = 0;
Unit Height = 0;
if (imagepath.Contains("Images\\streets"))
{
if (imagepath.Contains("streets\\circle-4-way.png") || imagepath.Contains("streets\\circle_street.png"))
{
Width = Unit.Pixel(220);
Height = Unit.Pixel(220);
theta = theta + 10;
}
}
else
{
Width = Unit.Pixel(50);
Height = Unit.Pixel(30);
}
Double newdeg = theta * (180.0 / Math.PI);
theta = (float)newdeg;
Matrix mRotate = new Matrix();
mRotate.Translate(Convert.ToInt32(Width.Value) / -2, Convert.ToInt32(Height.Value) /开发者_JAVA技巧 -2, MatrixOrder.Append);
mRotate.RotateAt(theta, new Point(0, 0), MatrixOrder.Append);
using (GraphicsPath gp = new GraphicsPath())
{ // transform image points by rotation matrix
gp.AddPolygon(new Point[] { new Point(0, 0), new Point(Convert.ToInt32(Width.Value), 0), new Point(0, Convert.ToInt32(Height.Value)) });
gp.Transform(mRotate);
PointF[] pts = gp.PathPoints;
// create destination bitmap sized to contain rotated source image
Rectangle bbox = boundingBox(bmpSrc, mRotate);
Bitmap bmpDest = new Bitmap(bbox.Width, bbox.Height);
using (Graphics gDest = Graphics.FromImage(bmpDest))
{ // draw source into dest
Matrix mDest = new Matrix();
mDest.Translate(bmpDest.Width / 2, bmpDest.Height / 2, MatrixOrder.Append);
gDest.Transform = mDest;
gDest.DrawImage(bmpSrc, pts);
gDest.DrawRectangle(Pens.Transparent, bbox);
//drawAxes(gDest, Color.Red, 0, 0, 1, 100, "");
return bmpDest;
}
}
}
It looks like the negative values are in radians. First convert to degrees: DEGREES = RADIANS * (180/pi);
Then add 360 to the result, that should give you a positive angle.
Feed that to your method.
精彩评论