2016-01-23 100 views
0

我正在嘗試使用AForge旋轉圖像。下面是代碼(Paint事件方法的圖片框):Aforge圖像旋轉導致紅色X

int radius = 10; 
Image img = new Bitmap("file_path.png"); 
Bitmap image = new Bitmap(img, radius, radius); 

//arbitrary angle of 67 
RotateBilinear ro = new RotateBilinear(67, true); 
image = ro.Apply(image); 
Graphics g = e.Graphics; 
g.DrawImage(image, 100, 100); 

如果我拿出RotateBilinear線,並繪製原始圖像ro.Apply線就好了,但是當我嘗試將其旋轉,表單的背景變成白色,並出現圍繞表單的紅色輪廓的紅色x。任何人都知道問題?

回答

0

試着圍繞你的代碼嘗試一下。

int radius = 10; 
Image img = new Bitmap("file_path.png"); 
Bitmap image = new Bitmap(img, radius, radius); 
//arbitrary angle of 67 

try 
{ 
    image = AForge.Imaging.Image.Clone(image, PixelFormat.Format8bppIndexed); 
    RotateBilinear ro = new RotateBilinear(67, true); 
    image = ro.Apply(image); 
} 
catch (Exception e) 
{ 
    Debug.WriteLine(string.Format("Caught Exception {0}\n{1}", e.GetType(), e.Message); 
} 

Graphics g = e.Graphics; 
g.DrawImage(image, 100, 100); 

如果我的懷疑是正確的,你將有紅色的X消失,圖像將呈現非旋轉。並且您將在Visual Studio的輸出窗口中顯示一些跟蹤語句,告訴您異常類型和異常消息。這些信息應該讓你瞭解它出錯的原因。

+0

我收到了錯誤消息 - >「過濾器不支持源像素格式」。不知道我需要改變我的PNG才能使其工作。 – Liger

+0

做一個快速谷歌建議您可能必須創建一個適當的PixelFormat選項的圖像。我已經在try子句中添加了相應的代碼 - 從來沒有使用AForge庫,我不能爲確切的PixelFormat枚舉器使用保證,但是您應該能夠通過查看文檔找出可以使用哪一個。 –

+0

好的謝謝你的幫助! – Liger

0

當代碼生成異常錯誤時,您會看到紅色的X.

我看不到你在哪裏分配了agentargent.image,但你使用了這些變量/屬性。我懷疑你有一些空值或類似的在那裏和你真正的意思是寫:

image = ro.Apply(image); 

因爲你設置image最初並在DrawImage呼叫時使用。

+0

我其實在這裏寫錯了,但我修好了,我用這個方法設置的圖像,但它仍然不起作用。 – Liger