首页 > 解决方案 > GDI Draw Rectange Array,第一行矩形画不出来

问题描述

我有一个地图数据,它将绘制这样的地图:

private int[,] mapData = new int[11, 25]
{
   { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 3, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 4 },
   { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
   { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
   { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
   { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
   {  0,  1,  1,  2,  1,  3,  1,  1,  1,  1,  1,  1,  1, 3, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 3 },
   { -1, -1, -1, -1, -1,  1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
   { -1, -1, -1, -1, -1,  2, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
   { -1, -1, -1, -1, -1,  1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
   { -1, -1, -1, -1, -1,  1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
   { -1, -1, -1, -1, -1,  3,  1,  1,  1,  1,  1,  1,  1, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
};

这是我的渲染逻辑:

e.Graphics.Clear(Color.Gray);
float renderRectWidth = (float)pictureBox1.Width / (float)25;
float renderRectHeight = (float)pictureBox1.Height / (float)11;
for (int i = 0; i < 11; i++)
{
    for (int j = 0; j < 25; j++)
    {
        int element = mapData[i, j];

        Color c;
        if (element == 0)
        {
            c = Color.Black;
        }
        else if (element == 1)
        {
            c = Color.Blue;
        }
        else if (element == 2)
        {
            c = Color.Orange;
        }
        else if (element == 3)
        {
            c = Color.Green;
        }
        else if (element == 4)
        {
            c = Color.Red;
        }
        else
        {
            c = Color.AliceBlue;
        }
        e.Graphics.FillRectangle(new SolidBrush(c), startPoint.X, startPoint.Y, renderRectWidth, renderRectHeight);
        startPoint.X = startPoint.X + renderRectWidth;
    }
    startPoint.X = 0;
    startPoint.Y = (i + 1) * renderRectHeight;
}

会导致如下结果,所以我的问题是:为什么第一条线画不出来,其他的线可以画出来?

绘制结果

标签: c#graphicsgdi

解决方案


我将其重写为:

private int[,] mapData = 
{
   { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 3, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 4 },
   { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
   { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
   { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
   { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
   {  0,  1,  1,  2,  1,  3,  1,  1,  1,  1,  1,  1,  1, 3, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 3 },
   { -1, -1, -1, -1, -1,  1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
   { -1, -1, -1, -1, -1,  2, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
   { -1, -1, -1, -1, -1,  1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
   { -1, -1, -1, -1, -1,  1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
   { -1, -1, -1, -1, -1,  3,  1,  1,  1,  1,  1,  1,  1, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 },
};

private Point startPoint;
private Dictionary<Color, SolidBrush> brushes = new Dictionary<Color, SolidBrush>();
private Color[] colors = { Color.Black, Color.Blue, Color.Orange, Color.Green, Color.Red };

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    startPoint = new Point(0, 0);
    e.Graphics.Clear(Color.Gray);
    float renderRectWidth = (float)pictureBox1.Width / (float)mapData.GetLength(1);
    float renderRectHeight = (float)pictureBox1.Height / (float)mapData.GetLength(0);            
    for (int i = 0; i <= mapData.GetUpperBound(0); i++)
    {
        for (int j = 0; j <= mapData.GetUpperBound(1); j++)
        {
            int element = mapData[i, j];                    
            Color c = (element == -1) ? Color.AliceBlue : colors[element];
            if (!brushes.ContainsKey(c))
            {
                brushes.Add(c, new SolidBrush(c));
            }
            e.Graphics.FillRectangle(brushes[c], startPoint.X, startPoint.Y, renderRectWidth, renderRectHeight);
            startPoint.X = startPoint.X + (int)renderRectWidth;
        }
        startPoint.X = 0;
        startPoint.Y = startPoint.Y + (int)renderRectHeight;
    }
}

输出:

在此处输入图像描述


推荐阅读