2009-09-01 67 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) 

這是主要的想法。希望能幫助到你。