我想根據css設置生成漸變。 例如:我有像這樣的css漸變-moz-linear-gradient(top,#ECFFEF 19%,#788300 83%)
或-webkit-gradient
版本。基於C#中的css漸變設置生成漸變#
我想獲得System.Drawing.Image對象,它代表與瀏覽器相同的圖像。圖像大小100x100。換句話說:我需要帶參數color1(#ECFFEF),color2(#788300),fromPercent(19%),toPercent(83),gradientMode(垂直或水平)和大小(100x100)的函數。這個函數返回System.Drawing.Image對象。
我已經寫了財產以後:
using (Bitmap bitmap = new Bitmap(100, 100))
using (Graphics graphics = Graphics.FromImage(bitmap))
using (LinearGradientBrush brush = new LinearGradientBrush(
new Rectangle(0, 0, 100, 100),
color1,
color2,
gradientMode))
{
brush.SetSigmaBellShape(0);// I am stuck at this place
graphics.FillRectangle(brush, new Rectangle(0, 0, 100, 100));
}
我停留在brush.SetSigmaBellShape(0);
什麼是漸變的形狀正確的設置?或者我可能在錯誤的地方搜索?
編輯:我找到了正確的設置。 我把它換成線brush.SetSigmaBellShape(0);
與代碼下一行:
Blend blend = new Blend();
blend.Factors = new float[] {0.0f, 0.0f, 0.5f, 1.0f, 1.0f};
blend.Positions = new float[]
{
0.0f, (float) fromPercent/100, (float) (fromPercent+ toPercent)/200,
(float) toPercent/100, 1.0f
};
brush.Blend = blend;
請將問題發佈在問題的正文中,而不是作爲代碼註釋隱藏。這太容易錯過了。 – Oded
完成,tnx諮詢! – IceN