如何使用负值旋转图像
我在服务器端使用下面的代码来旋转我的图像,它对于像 5,10,20..360 这样的角度效果很好。在同一个项目中,我必须旋转角度已从 Java 应用程序保存在数据库中的图像。它包含 -1、-1.23、1.4543545 等值,并且它不随我的下面的函数旋转。
更新/工作代码
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) / -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;
}
}
}
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) / -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;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来负值以弧度为单位。
首先转换为度数:
度数 = 弧度 * (180/pi);
然后将 360 添加到结果中,这应该会给你一个正角度。
将其输入到您的方法中。
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.