日期:2014-05-18  浏览次数:20978 次

请教一个关于gdi+渐变的问题
我用html5画圆,并且是渐变,然后用c#画圆时,渐变色总是不一致,代码如下:
HTML部分
HTML code

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body onload="">
    <canvas id="myCanvas" width="500" height="500">
    Your browser does not support the canvas element.
    </canvas>
    <script type="text/javascript">
        function tt()
        {
            var c = document.getElementById("myCanvas");
            var cxt = c.getContext("2d");
            var radGrad = cxt.createRadialGradient(200, 200, 0, 200, 200, 100);
            radGrad.addColorStop(0.0, 'rgba(0,0,255,1)');
            radGrad.addColorStop(1.0, 'rgba(0,0,0,0)');
            cxt.fillStyle = radGrad;
            cxt.fillRect(0, 100, 400, 200);
        }
        tt();
    </script>
</body>
</html>




后台C#部分
C# code

protected void Page_Load(object sender, EventArgs e)
        {
            Bitmap bmp = new Bitmap(200, 200);
            Graphics g = Graphics.FromImage(bmp);
            Color cStart = Color.FromArgb(40, 0, 0, 0);
            Color cEnd = Color.FromArgb(255, 0, 0, 255);
            int radius = 100;
            GraphicsPath path = new GraphicsPath();
            path.AddEllipse(0, 0, radius * 2, radius * 2);
            PathGradientBrush pthGrBrush = new PathGradientBrush(path);
            pthGrBrush.CenterColor = cEnd;
            Color[] colors = { cStart };
            pthGrBrush.SurroundColors = colors;
            g.FillEllipse(pthGrBrush, 0, 0, radius * 2, radius * 2);
            bmp.Save("d:\\1.png");
            bmp.Dispose();
            g.Dispose();

        }



求高手指导下,解决立刻给分

------解决方案--------------------
为啥不一样不知道,要不你用 LinearGradientBrush
 试试渐变色?
------解决方案--------------------
...... path的渐变色阿。。。
------解决方案--------------------
protected void Page_Load(object sender, EventArgs e)
{
Bitmap bmp = new Bitmap(200, 200);
Graphics g = Graphics.FromImage(bmp);
Color cStart = Color.FromArgb(40, 0, 0, 0);
Color cEnd = Color.FromArgb(255, 0, 0, 255);
int radius = 100;
GraphicsPath path = new GraphicsPath();
path.AddEllipse(0, 0, radius * 2, radius * 2);
PathGradientBrush pthGrBrush = new PathGradientBrush(path);
pthGrBrush.CenterColor = cEnd;
Color[] colors = { cStart };
pthGrBrush.SurroundColors = colors;
g.FillEllipse(pthGrBrush, 0, 0, radius * 2, radius * 2);
bmp.Save("d:\\1.png");
bmp.Dispose();
g.Dispose();

}




------解决方案--------------------
Color cStart = Color.FromArgb(40, 0, 0, 0); //alpha是否要为0?


探讨
或者path渐变是否能实现多种颜色的渐变?比如中心红色,中间位黄色,边上为蓝色?调试半天没有效果

------解决方案--------------------
Gradients made easy