c# - 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;
}
会导致如下结果,所以我的问题是:为什么第一条线画不出来,其他的线可以画出来?
解决方案
我将其重写为:
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;
}
}
输出:
推荐阅读
- node.js - 如何将坐标从 node.js 服务器发送到 Leaflet.js 客户端
- android - 我将套接字设置为对象类,我如何在另一个片段中调用它?
- typescript - cytoscape.js - 节点之间的间距
- php - 如何在 PHP Lumen 中简写模型的导入
- java - 使用 JodaTime 将本地时间转换为特定国家/地区时间
- python - 如何在 html 中运行 python 文件
- ios - 将来自 JSON 字典字典的 http 请求中的数据转换为 Swift 中的一个字典数组
- node.js - 如何在 PowerShell 中并行编写等效于节点的异步?
- python - Python 3.6 的 re 模块中 re.finditer() 函数和 re.DOTALL 标志的错误?
- linux - 嵌入式Linux USB CDC COM口改名问题