请教一个关于gdi+渐变的问题

s000rd 2012-02-24 03:04:00
我用html5画圆,并且是渐变,然后用c#画圆时,渐变色总是不一致,代码如下:
HTML部分

<!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#部分

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();

}


求高手指导下,解决立刻给分
...全文
314 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
s000rd 2012-04-13
  • 打赏
  • 举报
回复
自己搞定,定义模板,然后根据透明度重新绘制整个图片即可。
nonocast 2012-02-27
  • 打赏
  • 举报
回复
gomoku 2012-02-27
  • 打赏
  • 举报
回复
Color cStart = Color.FromArgb(40, 0, 0, 0); //alpha是否要为0?


[Quote=引用 5 楼 s000rd 的回复:]
或者path渐变是否能实现多种颜色的渐变?比如中心红色,中间位黄色,边上为蓝色?调试半天没有效果
[/Quote]

pthGrBrush.InterpolationColors = new ColorBlend(3)
{
Colors = new[] { Color.Blue, Color.Yellow, Color.Red },
Positions = new[] { 0f, .5f, 1f },
};
s000rd 2012-02-27
  • 打赏
  • 举报
回复
自己顶起来
s000rd 2012-02-27
  • 打赏
  • 举报
回复
自己顶
s000rd 2012-02-27
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 nonocast 的回复:]

Gradients made easy
[/Quote]
我现在实现了就是这种效果,但看了下html5中的效果,貌似是在这个基础上,有中心向两边透明渐变。但在gdi+中,如果第二种渐变色为透明,则显示的效果如上图,只有一种颜色。
s000rd 2012-02-27
  • 打赏
  • 举报
回复
gdi


html
s000rd 2012-02-27
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 gomoku 的回复:]

Color cStart = Color.FromArgb(40, 0, 0, 0); //alpha是否要为0?


引用 5 楼 s000rd 的回复:
或者path渐变是否能实现多种颜色的渐变?比如中心红色,中间位黄色,边上为蓝色?调试半天没有效果

C# code

pthGrBrush.InterpolationColors = new ColorBlend(3)
{
……
[/Quote]
为0的话,效果为蓝色渐变,看不出黑色的效果
s000rd 2012-02-24
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 kong19 的回复:]

...... path的渐变色阿。。。
[/Quote]
或者path渐变是否能实现多种颜色的渐变?比如中心红色,中间位黄色,边上为蓝色?调试半天没有效果
jyg2494236 2012-02-24
  • 打赏
  • 举报
回复
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();

}



s000rd 2012-02-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 kong19 的回复:]

为啥不一样不知道,要不你用 LinearGradientBrush
试试渐变色?
[/Quote]
LinearGradientBrush好像无法填充圆形
kong19 2012-02-24
  • 打赏
  • 举报
回复
...... path的渐变色阿。。。
kong19 2012-02-24
  • 打赏
  • 举报
回复
为啥不一样不知道,要不你用 LinearGradientBrush
试试渐变色?

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧