2013-09-27 35 views
4

目前,我成功地使用Graphics class繪製一個非矩形截取圖像(內龜):繪製剪裁的圖像時可能有抗鋸齒功能嗎?

enter image description here

我的代碼看起來是這樣的:

using (var g = Graphics.FromImage(image)) 
{ 
    g.InterpolationMode = InterpolationMode.HighQualityBicubic; 

    using (var gfxPath = new GraphicsPath()) 
    { 
     gfxPath.AddEllipse(r); 

     using (var region = new Region(r)) 
     { 
      region.Exclude(gfxPath); 

      g.ExcludeClip(region); 

      g.DrawImage(turtleImage, r, r2, GraphicsUnit.Pixel); 
     } 
    } 
} 

這一切工作正如預期的那樣。我不知道如何解決的是使圖像邊界反鋸齒。

圖像縮放的樣子:

enter image description here

即圖像結束的邊界和圖像的透明「背景」開始是粗略的切割,而不是平滑的alpha混合。

我的問題是:

是否有可能夾繪製圖像,並具有抗混疊活躍?

+0

剛剛發現[文章](http://danbystrom.se/2008/08/24/soft-edged-images-in-gdi/),我認爲這將是要走的路。現在就試試。 –

回答

3

如果你想要去的完全成熟羽化你應該考慮考慮看看這篇文章:

http://danbystrom.se/2008/08/24/soft-edged-images-in-gdi/

如果你想有一個快速簡便的解決方案,你也許可以先畫出圖像,然後繪製使用帶抗鋸齒的純色白色刷子在其頂部使用GraphicsPath。你會做這樣的事情:

Rectangle outerRect = ClientRectangle; 
Rectangle rect = Rectangle.Inflate(outerRect, -20, -20); 

using (Image img = new Bitmap("test.jpg")) 
{ 
    g.DrawImage(img, outerRect); 

    using (SolidBrush brush = new SolidBrush(Color.White)) 
    using (GraphicsPath path = new GraphicsPath()) 
    { 
     g.SmoothingMode = SmoothingMode.AntiAlias; 

     path.AddEllipse(rect); 
     path.AddRectangle(outerRect); 

     g.FillPath(brush, path); 
    } 
} 
+0

謝謝,這就是我上面評論的:-) –

+0

鏈接的解決方案完美地工作。 –

相關問題