如何更改 GDI 的颜色+线性渐变画笔?
我必须编写几个小的垂直渐变(在循环上),所以我认为重新使用现有的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
与实际使用画笔绘制内容相比,制作画笔几乎不需要花费任何成本。
另外,尝试设置整个数组而不是替换单个元素。
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.
这也许是学术性的(也许其中大部分在回想起来是显而易见的!),但是更改单一颜色不起作用的原因是颜色是在呈现给您之前从非托管代码中提取的 - 您会得到一份副本颜色,这就是你要改变的。或者,更正式地说,语句中的左值是按值传递的,并且没有更新原始值的机制。
当您更改整个渐变数组时,数组的属性设置器会将更改写回非托管对象。
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.