C# 如何创建一个包含100个点的列表,并将它们随机设置为新的黑色图像?
在表格顶部:C# 如何创建一个包含100个点的列表,并将它们随机设置为新的黑色图像?,c#,.net,winforms,C#,.net,Winforms,在表格顶部: private List<Point> points = new List<Point>(); private Bitmap newBmp = new Bitmap(512, 512); private List points=new List(); 私有位图newBmp=新位图(512); 在构造函数中: for(int x = 0; x < 100; x++) { for (int y =
private List<Point> points = new List<Point>();
private Bitmap newBmp = new Bitmap(512, 512);
private List points=new List();
私有位图newBmp=新位图(512);
在构造函数中:
for(int x = 0; x < 100; x++)
{
for (int y = 0; y < 100; y++)
{
points.Add(new Point(x, y));
}
}
for(int x=0;x<100;x++)
{
对于(int y=0;y<100;y++)
{
点。添加(新点(x,y));
}
}
这就是我目前得到的。这个循环使列表有10000个点,而我只想要100个。我也不知道该怎么做
黑色图像上的点可以是任何颜色。您只需循环
10*10次即可获得100点,如下所示:
for(int x = 0; x < 10; x++)
{
for (int y = 0; y < 10; y++)
{
points.Add(new Point(x, y));
}
}
for(int x=0;x<10;x++)
{
对于(int y=0;y<10;y++)
{
点。添加(新点(x,y));
}
}
通过x
和y
循环将仅生成该值范围内的点。如果需要随机点,则需要使用random
对象:
Random r = new Random();
for (int i = 0; i < 100; i++)
{
points.Add(
new Point(
r.Next(0, newBmp.Width),
r.Next(0, newBmp.Height)));
}
那么,如果100 x 100=10000,那么100的循环应该是什么大小?另外,点
没有颜色您需要使用对象来设置x和y的值。循环应该变为100,然后您可以再次使用“随机”创建随机颜色。确保仅使用随机对象的一个实例。。代码将创建一个实心像素块。最后你可以使用位图。SetPixel(x,y,color)你能解释一下你还需要做什么吗?这不是他写“random”时的意思,因为它是一个块。
foreach (var pt in points)
{
Color c = Color.FromArgb(
r.Next(0, 256),
r.Next(0, 256),
r.Next(0, 256));
newBmp.SetPixel(pt.X, pt.Y, c);
}