简体   繁体   English

如何获得所有笔和画笔的阵列?

[英]How do I get an array of all Pens and Brushes?

I'm working on an application which generates the representation of the Mandelbrot set. 我正在开发一个生成Mandelbrot集表示形式的应用程序。 I already got it to work, see the image below, pretty cool stuff! 我已经做好了,请看下面的图片,很酷的东西!

在此处输入图片说明

For the colors I'm using an array like this: 对于颜色,我使用这样的数组:

Pen[] pens = {
    Pens.Red,
    Pens.Green,
    Pens.Blue,
    Pens.Cyan,
    Pens.Magenta,
    Pens.Yellow
};

Further on in my code I'm using this array to fill in the pixels using the following snippet: 在我的代码中,我进一步使用此数组使用以下代码段填充像素:

while(iteration > 6)
{
    iteration -= 6;
}

graphics.DrawRectangle(pens[iteration-1], rectangle);

This gives me the right color (you can see the color patern repeats over and over) 这给了我正确的颜色(您可以看到颜色图案一遍又一遍地重复)

I want to give my code a wider color pallette and make use of all the Brushes and Pens available. 我想给我的代码一个更大的颜色调色板,并利用所有可用的BrushesPens

However, when we look at the Pens documentation ( https://msdn.microsoft.com/en-us/library/system.drawing.pens(v=vs.110).aspx ) you can see that the Pens class contains properties only. 但是,当我们查看Pens文档( https://msdn.microsoft.com/zh-cn/library/system.drawing.pens(v=vs.110).aspx )时,您会看到Pens类包含属性只要。

How do I get an array of all these Pens without the need to declare it myself color by color? 如何获得所有这些笔的阵列,而无需自己逐个颜色声明它? This would take up a huge chunck of code which in my eyes seems to be like it could be done easier. 这将占用大量的代码,在我看来,这似乎可以更轻松地完成。 Thanks! 谢谢!

As @stuartd mentioned, you will want to use Reflection to get these. 如@stuartd所述,您将需要使用Reflection来获取这些。

var pens = typeof(Pens).GetProperties(BindingFlags.Static | BindingFlags.Public)
                       .Select(p => p.GetValue(null))
                       .OfType<System.Drawing.Pen>()
                       .ToArray();

The above code will return you an array of Pen. 上面的代码将返回一个Pen数组。 Keep in mind, you will also get Transparent included which you may want to keep or remove. 请记住,您还将获得其中可能要保留或删除的Transparent

The same code above will work if you swap Pen for Brush. 如果将Pen换为Brush,则上面的相同代码将起作用。

You seem to have a misunderstanding of how things work. 您似乎对事情的运作方式有误解。

Yes, there are a bunch of pre-defined pens which generally avoid programs having to create & destroy pens. 是的,有一堆预定义的笔通常可以避免程序不得不创建和销毁笔。 However, you can make pens of any color, not only the defined ones. 但是,您可以制作任何颜色的笔,而不仅限于已定义的笔。 Thus there are 16 million possible pens (although I would be surprised if Windows didn't barf on an attempt to create that many.) 因此,可能有1600万支笔(尽管如果Windows不尝试创建那么多笔,我会感到惊讶。)

Getting all predefined pens won't get all possible pens. 获取所有预定义的笔不会得到所有可能的笔。

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

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