如何在 C# 中使用 LinearGradientBrush 绘制精确的渐变?
我正在使用 GDI 在 C# 中绘制这个对象,并且我需要进行精确的像素绘制。但是,这不起作用...我不知道这是否重要,但我正在绘制的对象是 ToolStrip,我正在执行自定义 ToolStrip 渲染。
我在 y 像素 1 处画了一条水平蓝线,在 y 像素 2 处画了一条黄色线,在 y 像素 3 处画了一条红色线。这些不是真实的颜色,只是为了让我看得更清楚。
像这样:
Color cTop1 = Color.FromArgb(255, Color.Blue);
Color cTop2 = Color.FromArgb(255, Color.Yellow);
Color cTop3 = Color.FromArgb(255, Color.Red);
g.DrawLine(new Pen(cTop1), 0, 1, aBounds.Width, 1);
g.DrawLine(new Pen(cTop2), 0, 2, aBounds.Width, 2);
g.DrawLine(new Pen(cTop3), 0, 3, aBounds.Width, 3);
然后,在 y 像素 4 处,我开始用 LinearGradientBrush 填充一个矩形,如下所示:
Rectangle rTop = new Rectangle(0, 3, aBounds.Width, (aBounds.Height / 2) - 4);
Color cTop = Color.FromArgb(245, Color.White);
Color cBottom = Color.FromArgb(222, Color.White);
LinearGradientBrush tGradient = new LinearGradientBrush(rTop, cTop, cBottom, LinearGradientMode.Vertical);
g.FillRectangle(tGradient, rTop);
问题是渐变绘制在红线上方。蓝色和黄色都可以,但是红线应该是RGB=255,0,0,实际上是RGB=255,111,111。
我不明白为什么...我已经在 SmoothingMode、InterpolationMode、CompositionMode、CompositionQuality 中玩过,但我就是无法使其正确...
有什么想法吗?
I'm painting this object in C# using GDI and I need to have precise pixel painting. However, this is not working... I don't know if it matters but the object I'm painting is a ToolStrip, I'm doing a custom ToolStrip render.
I drew a horizontal blue line at y-pixel 1, than a yellow one at y-pixel 2 than a red one at y-pixel 3. These are not real colors it's just for me to see better.
Like this:
Color cTop1 = Color.FromArgb(255, Color.Blue);
Color cTop2 = Color.FromArgb(255, Color.Yellow);
Color cTop3 = Color.FromArgb(255, Color.Red);
g.DrawLine(new Pen(cTop1), 0, 1, aBounds.Width, 1);
g.DrawLine(new Pen(cTop2), 0, 2, aBounds.Width, 2);
g.DrawLine(new Pen(cTop3), 0, 3, aBounds.Width, 3);
Than, at y-pixel 4 I started filling a rectangle with the LinearGradientBrush like this:
Rectangle rTop = new Rectangle(0, 3, aBounds.Width, (aBounds.Height / 2) - 4);
Color cTop = Color.FromArgb(245, Color.White);
Color cBottom = Color.FromArgb(222, Color.White);
LinearGradientBrush tGradient = new LinearGradientBrush(rTop, cTop, cBottom, LinearGradientMode.Vertical);
g.FillRectangle(tGradient, rTop);
The problem is that the gradient is painting above the red line. The blue and yellow ones are ok, but the red line, which should be RGB=255,0,0, it's in fact RGB=255,111,111.
I can't understand why... I've played in SmoothingMode, InterpolationMode, CompositionMode, CompositionQuality but I just can't make it right...
Any ideias?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
画笔可能被包裹,而您看到的是相反的颜色“渗出”。
尝试将包裹模式设置为
It's possible that the brush is being wrapped and what you are seeying is the oposite colour "bleeding" in.
Try and set the wrapmode to