矩阵旋转c#
我试图用矩阵对象旋转图像,但无法正确旋转 当我旋转图像时,我得到了一个黑点,它错了一个像素,180度和270度也是一样的 90角。 此问题的图片:矩阵旋转c#,c#,matrix,C#,Matrix,我试图用矩阵对象旋转图像,但无法正确旋转 当我旋转图像时,我得到了一个黑点,它错了一个像素,180度和270度也是一样的 90角。 此问题的图片: 代码如下: public System.Drawing.Image Rotate(System.Drawing.Image image, String angle, String direction) { Int32 destW, destH; float destX, destY, rotate; destW = image.Wi
代码如下:
public System.Drawing.Image Rotate(System.Drawing.Image image, String angle, String direction)
{
Int32 destW, destH;
float destX, destY, rotate;
destW = image.Width;
destH = image.Height;
destX = destY = 0;
if (r == "90" || r == "270")
{
destW = image.Height;
destH = image.Width;
destY = (image.Width - destW) / 2;
destX = (image.Height - destH) / 2;
}
rotate = (direction == "y") ? float.Parse(angle) : float.Parse("-" + angle);
Bitmap b = new Bitmap(destW, destH, PixelFormat.Format24bppRgb);
b.SetResolution(image.HorizontalResolution, image.VerticalResolution);
Matrix x = new Matrix();
x.Translate(destX, destY);
x.RotateAt(rotate, new PointF(image.Width / 2, image.Height / 2));
Graphics g = Graphics.FromImage(b);
g.PageUnit = GraphicsUnit.Pixel;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.Transform = x;
g.DrawImage(image, 0, 0);
g.Dispose();
x.Dispose();
return b;
}
如果有人有一个好的ide为什么会发生这种情况,请告诉我
祝你有愉快的一天 我认为这一行中出现了舍入错误:
x.RotateAt(rotate, new PointF(image.Width / 2, image.Height / 2));
宽度和高度都是int属性。请尝试以下方法:
x.RotateAt(rotate, new PointF((float)Math.Floor(image.Width / 2),
(float)Math.Floor(image.Height / 2)));
(未经测试,因此不确定这是否可行。)
更新:我认为我的上述修复方案不起作用,但它可能会为您指出问题的方向。如果无法通过调整舍入来解决此问题,则可能只需将destX更改为-1,以消除黑线。此操作有效:
x、 RotateAt(旋转,新点f(image.Width/2,image.Height/2));
此“image.Width/2”返回浮点值
首先,我找出角度是多少,如果是90或270,翻转图像,使image.width=image.height和image.height=width
如果我旋转图像时遇到问题,因为图像宽度可能大于图像高度,所以我需要将图像x,y坐标重置为0,0
所以这个“destY=(image.Width-destW)/2;”计算图像到位图的偏移量
这个“x.Translate(destX,destY);”将图像x设置为位图x
但由于旋转使图片变小了1px,所以出现了一些问题
所以对于我的英语来说,我不是最好的,我希望你能阅读它,为什么:)
更多问题请发送给我,我将尝试解释我的意思。一个更简单的解决方案是:
这是主要思想。希望有帮助。注意:天使!=角度。。。天使飞翔,天使不飞翔t@lonut:没那么快-胸襟开阔的可能是一个说英语的老人:哈哈,我没看到,但我现在改变了
// h and w are the width/height
// cx and cy are the centre of the image
myBitmap = new Bitmap(w + 2, h + 2);
mygraphics = Graphics.FromImage(myBitmap);
mygraphics.TranslateTransform(cx, cy);
mygraphics.RotateTransform(angle);
mygraphics.TranslateTransform(-cx, -cy);
mygraphics.DrawImage(myimage, new Point(1, 1));
// image crop
myBitmap= myBitmap.Clone(new Rectangle(1, 1, (int)w, (int)h), myimage.PixelFormat)