简体   繁体   English

如何在C#中使用LinearGradientBrush绘制精确的渐变?

[英]How to paint a precise Gradient with LinearGradientBrush in C#?

I'm painting this object in C# using GDI and I need to have precise pixel painting. 我正在使用GDI在C#中绘制此对象,并且需要进行精确的像素绘制。 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. 但是,这不起作用...我不知道这是否重要,但是我正在绘制的对象是ToolStrip,我正在执行自定义ToolStrip渲染。

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. 我在y像素1处绘制了一条蓝色的水平线,在y像素2处绘制了一条黄色的线,而在y像素3处绘制了一条红色的线。这些不是真实的颜色,只是为了让我看得更好。

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: 然后,在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);

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. 蓝色和黄色都可以,但是红线应该是RGB = 255,0,0,实际上是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... 我不明白为什么...我玩过SmoothingMode,InterpolationMode,CompositionMode,CompositionQuality,但我做对了...

Any ideias? 有什么想法吗?

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 尝试将wrapmode设置为

brush.WrapMode = Drawing2D.WrapMode.TileFlipXY

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM