Generate gradient based on css gradient settings in c#
I want to generate gradient based on css settings.
Example: I have css gradient like this -moz-linear-gradient(top,#ECFFEF 19%,#788300 83%)
or in -webkit-gradient
version.
I want to get System.Drawing.Image object which represents the same image as it displays browser. Image size 100x100.
In other words: I need function with parameters - color1(#ECFFEF), color2(#788300), fromPercent(19%), toPercent(83),gradientMode(Vertical or Horizontal) and size(100x100). This functions returns System.Drawing.Image object.
I've wrote somthing:
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));
}
I am stuck at brush.SetSigmaBellShape(0);
what is correct settings of gradient's shape? Or maybe I am searching at the wrong place?
EDIT: I've found correct settings.
I've replaced line brush.SetSigmaBellShape(0);
with next lines of code:
Blend blend = new Blend();
blend.Factors = new float[] {0.0f, 0.0f, 0.开发者_Go百科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;
精彩评论