如何更改 GDI 的颜色+线性渐变画笔?

发布于 2024-09-11 08:32:16 字数 485 浏览 6 评论 0原文

我必须编写几个小的垂直渐变(在循环上),所以我认为重新使用现有的 LinearGradientBrush 会更快(正确吗?)

但这不是我期望发生的事情......

  Drawing2D.LinearGradientBrush myBrush = new Drawing2D.LinearGradientBrush(new Rectangle(0, 0, 200, 200), Color.Red, Color.Black, Drawing2D.LinearGradientMode.Vertical);
  myBrush.LinearColors[1] = Color.Blue;
  MsgBox(myBrush.LinearColors[1].ToString); //Returns black

那么,是否有错误在上面的代码中,或者在循环上获得多个垂直渐变的更好方法,或者改变 LinearGradientBrush 颜色的不同方法?

谢谢 :)

I have to write several small vertical gradients (on a loop) and so I think it's faster to re-use an existing LinearGradientBrush (correct?)

But this isn't what I expected to happen...

  Drawing2D.LinearGradientBrush myBrush = new Drawing2D.LinearGradientBrush(new Rectangle(0, 0, 200, 200), Color.Red, Color.Black, Drawing2D.LinearGradientMode.Vertical);
  myBrush.LinearColors[1] = Color.Blue;
  MsgBox(myBrush.LinearColors[1].ToString); //Returns black

So, is there either an error on the above code, or a better way to get several vertical gradients on a loop, or a different way to change the LinearGradientBrush's colors?

Thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏至、离别 2024-09-18 08:32:16

与实际使用画笔绘制内容相比,制作画笔几乎不需要花费任何成本。

另外,尝试设置整个数组而不是替换单个元素。

myBrush.LinearColors = new Color[2] { Color.Blue, Color.Whatever };

Constructing a brush costs almost nothing compared to the work done to actually draw something with the brush.

Also, try setting the entire array instead of replacing a single element.

myBrush.LinearColors = new Color[2] { Color.Blue, Color.Whatever };
流年已逝 2024-09-18 08:32:16

这也许是学术性的(也许其中大部分在回想起来是显而易见的!),但是更改单一颜色不起作用的原因是颜色是在呈现给您之前从非托管代码中提取的 - 您会得到一份副本颜色,这就是你要改变的。或者,更正式地说,语句中的左值是按值传递的,并且没有更新原始值的机制。

当您更改整个渐变数组时,数组的属性设置器会将更改写回非托管对象。

This is perhaps academic (and perhaps much of it is obvious in retrospect!), but the reason changing a single color doesn't work is that the colors are extracted from non-managed code before being presented to you - you are given a copy of the color and that is what you are changing. Or, to put it more formally, the l-value in your statement is passed by value and there is no mechanism for updating the original.

When you change the whole gradient array the property setter for the array writes the changes back to the unmanaged object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文