所以我要做的就是从不同颜色的面板中创建一个随机的图像。用户可以选择他想要的面板(即像素)的数量和不同颜色的数量,然后程序自动生成该图像。我真的很喜欢使用面板,因为我稍后会用到这张图片,并且需要修改每个像素。因为我对面板很满意,所以我想保留它们,而不使用其他任何东西。
下面是我用来创建这个面板的代码:
//Creates two lists of panels
//Add items to list so that these places in the list can be used later.
//nudSizeX.Value is the user-chosen number of panels in x-direction
for (int a = 0; a < nudSizeX.Value; a++)
{
horizontalRows.Add(null);
}
//nudSizeY.Value is the user-chosen number of panels in y-direction
for (int b = 0; b < nudSizeY.Value; b++)
{
allRows.Add(null);
}
for (int i = 0; i < nudSizeY.Value; i++)
{
for (int j = 0; j < nudSizeX.Value; j++)
{
// new panel is created, random values for background color are assigned, position and size is calculated
//pnlBack is a panel used as a canvas on whoch the other panels are shown
Panel pnl = new Panel();
pnl.Size = new System.Drawing.Size((Convert.ToInt32(pnlBack.Size.Width)) / Convert.ToInt32(nudSizeX.Value), (Convert.ToInt32(pnlBack.Size.Height) / Convert.ToInt32(nudSizeY.Value)));
pnl.Location = new Point(Convert.ToInt32((j * pnl.Size.Width)), (Convert.ToInt32((i * pnl.Size.Height))));
//There are different types of panels that vary in color. nudTypesNumber iis the user-chosen value for howmany types there should be.
int z = r.Next(0, Convert.ToInt32(nudTypesNumber.Value));
//A user given percentage of the panels shall be free, i.e. white.
int w = r.Next(0, 100);
if (w < nudPercentFree.Value)
{
pnl.BackColor = Color.White;
}
//If a panel is not free/white, another rendom color is assigned to it. The random number determinig the Color is storede in int z.
else
{
switch (z)
{
case 0:
pnl.BackColor = Color.Red;
break;
case 1:
pnl.BackColor = Color.Blue;
break;
case 2:
pnl.BackColor = Color.Lime;
break;
case 3:
pnl.BackColor = Color.Yellow;
break;
}
}
//Every panel has to be added to a list called horizontal rows. This list is later added to a List<List<Panel>> calles allRows.
horizontalRows[j] = (pnl);
//The panel has also to be added to the "canvas-panel" pnl back. The advantage of using the canvas panel is that it is easier to determine the coordinates on this panel then on the whole form.
pnlBack.Controls.Add(pnl);
}
allRows[i] = horizontalRows;
}正如您可能想象的那样,当创建一个99x99的棋盘时,这是非常慢的,因为程序必须在这个过程中循环近10000次。
你想做些什么来提高性能?我说我想继续使用面板,因为我对它们很满意,但如果使用面板比我想象的更愚蠢,我对其他选择持开放态度。程序创建的面板越多,速度就越慢。我猜这是因为列表中的内容越来越多了吧?
现在的输出是这样的:

这是我稍后想要对我的“图片”做的:我基本上想做Schellings模型。该模型显示了当不同的人群(即不同的颜色)想要在他们周围有一定比例的人属于他们的群体时,他们是如何区分的。这意味着稍后我必须能够检查每个面板/像素的邻居是什么,并且必须能够单独更改每个像素的颜色。
我不想要一个现成的解决方案,我只是希望得到一些如何提高图片创建过程的速度的提示。
非常感谢
发布于 2013-05-18 16:48:01
好吧,我建议你使用GDI+代替,你可以将你的颜色存储在一个二维数组中,这样你就可以基于它绘制所需的图像,你也可以循环它们进行进一步的处理,看看这段代码和演示项目:
正如你提到的,你不熟悉gdi+,有一个演示项目,你可以自己检查一下,看看它是如何在gdi+中完成的:
演示项目:ColorsTableDemoProject
Color[,] colorsTable;
Bitmap b;
Graphics g;
int size = 80; // size of table
int pixelWidth = 5; // size of each pixel
Random r = new Random();
int rand;
// CMDDraw is my Form button which draws the image
private void CMDDraw_Click(object sender, EventArgs e)
{
colorsTable = new Color[size, size];
pictureBox1.Size = new Size(size * pixelWidth, size * pixelWidth);
b = new Bitmap(size * pixelWidth, size * pixelWidth);
g = Graphics.FromImage(b);
for (int y = 0; y < size; y++)
{
for (int x = 0; x < size; x++)
{
rand = r.Next(0, 4);
switch (rand)
{
case 0: colorsTable[x, y] = Color.White; break;
case 1: colorsTable[x, y] = Color.Red; break;
case 2: colorsTable[x, y] = Color.Blue; break;
case 3: colorsTable[x, y] = Color.Lime; break;
default: break;
}
g.FillRectangle(new SolidBrush(colorsTable[x, y]), x * pixelWidth, y * pixelWidth, pixelWidth, pixelWidth);
}
}
pictureBox1.Image = b;
} 发布于 2013-05-18 16:26:53
不要使用Panels,而是使用矩阵来存储您需要的颜色和其他信息。在OnPaint事件中,使用此矩阵通过GDI+绘制矩形。
如果你有一个包含颜色的矩阵,下面是一个如何绘制10x10“像素”的例子:
private void myPanel_Paint(object sender, PaintEventArgs e)
{
for (var y=0; y < matrix.GetUpperBound(0); y++)
for (var x=0; x < matrix.GetUpperBound(1); x++)
{
var Brush = new SolidBrush(matrix[y,x]);
e.Graphics.FillRectangle(Brush, new Rectangle(x*10, y*10, 10, 10));
}
}发布于 2013-05-18 16:43:17
使用picturebox完成您的绘图。您已经获得了查看每个面板的位置的代码,只需将其更改为在每个位置绘制一个矩形即可。这样,您将只在板上绘制几个矩形,而不是使用10.000个GUI对象。
哦,保持你的模型/逻辑和视图分离。保留一个包含所有信息的矩阵,只需使用“绘制方法”来绘制它。
您的模型可能如下所示:
MyPanel[,] panels;
class MyPanel
{
Color color;
}这样很容易检查面板的所有邻居,只需检查面板矩阵即可。
你的视图应该这样做:
class View
{
Paint(MyPanel[,] panels)
{
//Draw
}
}https://stackoverflow.com/questions/16622105
复制相似问题