2009-09-01 68 views
1

我试着来旋转用矩阵对象的图像并不能得到正确矩阵rotateAt C#

当我转动我有一个黑点的图像,这是一个像素错的,它与180的角度和270相同角度。

90角ex。 这个问题的一个画面: http://www.spasm-design.com/rotate/onePixelWrong.jpg


这里是代码:

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为什么发生这种情况,请告诉我。

祝您有美好的一天!

+1

注:天使=角...天使飞,角度不 – 2009-09-01 14:24:09

+1

@lonut:没那么快 - 视野开阔可能是古英语音箱:http://dictionary.reference.com/browse/angle – MusiGenesis 2009-09-01 14:32:07

+0

哈哈我没有看到,但我现在改变它。 :) – Broadminded 2009-09-01 14:39:21

回答

0

我认为你只是在这条线得到一个舍入误差:

x.RotateAt(rotate, new PointF(image.Width/2, image.Height/2)); 

宽度和高度都诠释的性质。试试这个:

x.RotateAt(rotate, new PointF((float)Math.Floor(image.Width/2), 
    (float)Math.Floor(image.Height/2))); 

(未测试,所以不知道这是否会工作)

更新:我不认为我上面的修补程序将正常工作,但它可能指向您在问题的方向。如果您无法通过调整舍入来修复它,则可能只需将destX更改为-1即可摆脱黑线。

0

这个作品: x.RotateAt(rotate,new PointF(image.Width/2,image.Height/2)); 这种“image.Width/2”返回浮动

首先,我发现角是什么,如果是90或270翻转图像,这样image.width = image.height和image.height =宽度

如果这样做,我得到一个问题,当我旋转图像的图像宽度可以更大然后图像的高度,然后我需要重置图像x,y坐标为0,0

因此,这个“destY = (image.Width - destW)/ 2;“计算图像到位图 和此“x.Translate(destX,destY)”的偏移量;设置图像x相当于位图x

但是,旋转出现问题使图片1px变小。

所以,我的英语,但我不是最好的,我希望你能阅读它的任何原因:)

更多的问题,请给我的,我要去尝试解释一下我的意思。

0

一个更简单的解决方案是:

  1. 添加一个像素显示图像。
  2. 旋转图像。
  3. 删除一个像素。

代码:

// 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) 

这是主要的想法。希望能帮助到你。