2016-09-26 171 views
0

我試圖找到繪製在C#中的多邊形與邊緣逐漸融入背景色的最佳途徑。我正在繪製多邊形到位圖,所以目前我使用System.Drawing中的Graphics類。多邊形融合邊

多邊形將是一個混合面膜,我沒有問題提請polgons黑色和白色。但是我希望它們能在兩種顏色之間逐漸過渡到一定數量的像素,比如50像素(應該指定這個尺寸)。

我遇到了PathGradiantBrush,但我無法找到一個方法來指定過渡區的大小。使用該畫筆,過渡似乎取決於多邊形的大小和形狀,而不是固定的大小。

什麼得出這樣的多邊形的最好方法?

+1

您是否實際上使用Microsoft Blend按照您添加的標記描述? – Sayse

+0

你可以發佈一個示例圖像來清除事情嗎? – TaW

回答

1

正如你可以在對方的回答看,漸變畫筆用居中的漸變填充路徑或多邊形;你可以設置中心點,但他們還沒有真正遵循多邊形邊緣:

enter image description here

您可以通過創建一個ColorBlendTransparent,適合Positions,像我一樣相互影響色帶的相對寬度對於上述結果,但egdes朝向中心點的角度以及它們與邊界矩形的距離仍將決定它們的絕對寬度。對於muliticolor漸變畫筆例如see here!


所以,除非您的多邊形是近圓形,你需要以不同的方式去做。

這裏是一個解決方案,這將遵循邊緣:

enter image description here

使用GraphicsPath path(或簡稱爲Point陣列)與Graphics對象g它首先在背景顏色填充,然後繪製該筆的寬度和透明度均增長。爲了保持外邊緣不透明,我設置了Pen.Alignment = PenAlignment.Inset。當然你也可以玩數字。:

g.FillPath(Brushes.MediumSeaGreen, path); 

int ew = 8; // edge width 

for (int i = 0; i < ew ; i++) 
    using (Pen pen = new Pen(Color.FromArgb(255 - i * 255/ew, Color.DarkSlateBlue), i)) 
    { 
     pen.Alignment = PenAlignment.Inset; 
     g.DrawPath(pen, path); 
    } 

請注意,左邊看起來有點厚,但它確實不是;只是一個錯覺..

+0

謝謝,這種在形狀邊緣繪製不同線條的方法可以用於我想要做的事情。我會試試看。 –

0

我看了一下MSDN上的路徑漸變畫筆並發現了the FocusScales property我相信試圖解決你面對的問題。

這裏是this page顯示使用FocusScales的例子:

// Create a path that consists of a single ellipse. 
GraphicsPath path; 
path.AddEllipse(0, 0, 200, 100); 

// Create a path gradient brush based on the elliptical path. 
PathGradientBrush pthGrBrush(&path); 
pthGrBrush.SetGammaCorrection(TRUE); 

// Set the color along the entire boundary to blue. 
Color color(Color(255, 0, 0, 255)); 
INT num = 1; 
pthGrBrush.SetSurroundColors(&color, &num); 

// Set the center color to aqua. 
pthGrBrush.SetCenterColor(Color(255, 0, 255, 255)); 

// Use the path gradient brush to fill the ellipse. 
graphics.FillPath(&pthGrBrush, &path); 

// Set the focus scales for the path gradient brush. 
pthGrBrush.SetFocusScales(0.3f, 0.8f); 

// Use the path gradient brush to fill the ellipse again. 
// Show this filled ellipse to the right of the first filled ellipse. 
graphics.TranslateTransform(220.0f, 0.0f); 
graphics.FillPath(&pthGrBrush, &path); 

和輸出的例子:

Example of FocusScales