2011-02-16 44 views
1

我有一個相當大的(30MB)圖像,我想從中取出一小塊「切片」。切片需要表示原始圖像的旋轉部分。GDI +旋轉的子圖像

下面的工程,但角落是空的,它似乎是我採取原始圖像的矩形區域,然後旋轉,並繪製在未旋轉的表面上導致缺少的角落。

我想要的是原始圖像上的旋轉選擇,然後繪製在未旋轉的表面上。我知道我可以先旋轉原始圖像來完成此操作,但考慮到它的大小,這似乎效率低下。

有什麼建議嗎?謝謝,

public Image SubImage(Image image, int x, int y, int width, int height, float angle) 
{ 
    var bitmap = new Bitmap(width, height); 

    using (Graphics graphics = Graphics.FromImage(bitmap)) 
    { 
     graphics.TranslateTransform(bitmap.Width/2.0f, bitmap.Height/2.0f); 
     graphics.RotateTransform(angle); 
     graphics.TranslateTransform(-bitmap.Width/2.0f, -bitmap.Height/2.0f); 
     graphics.DrawImage(image, new Rectangle(0, 0, width, height), x, y, width, height, GraphicsUnit.Pixel); 
    } 

    return bitmap; 
} 

回答

1

你應該性能測試這個決定,最好的辦法之前,但兩個選項

  1. 正如你提到的,首先旋轉圖像,然後提取內部矩形。
  2. 提取大於所需旋轉部分但小於整個圖像的內部矩形。旋轉此新圖像,然後從旋轉的子圖像中提取內部圖像。

選項2基本上允許您在比整個圖像更小的圖像上執行旋轉。

就像我說的,我會做一些體面的性能測試,以確定與選項2相比,選項1是否真的值得。

+0

已經登陸opiton 2.沒有測試性能,但它確定似乎會更快。 – andleer