c# - 为什么 DrawRectangle 在我的 PictureBox 中绘制一个十字

标签 c# graphics multidimensional-array drawrectangle

我试图绘制 10 个矩形,但是当我使用 g.DrawRectangle() 时,它绘制了一个十字,如下所示:

Drawing cross

我正在创建包含 getRectangle() 函数的 Vertex 对象,该函数返回该顶点的 Rectangle 对象。

我希望创建这些对象并在 pictureBox 上将它们显示为矩形。

这是我的代码

    private System.Drawing.Graphics g;
    private System.Drawing.Pen pen1 = new System.Drawing.Pen(Color.Blue, 2F);

    public Form1()
    {
        InitializeComponent();

        pictureBox.Dock = DockStyle.Fill;
        pictureBox.BackColor = Color.White;
    }

    private void paintPictureBox(object sender, PaintEventArgs e)
    {
        // Draw the vertex on the screen
        g = e.Graphics;

        // Create new graph object
        Graph newGraph = new Graph();

        for (int i = 0; i <= 10; i++)
        {
           // Tried this code too, but it still shows the cross
           //g.DrawRectangle(pen1, Rectangle(10,10,10,10);

           g.DrawRectangle(pen1, newGraph.verteces[0,i].getRectangle());
        }
    }

Vertex 类代码

class Vertex
{
    public int locationX;
    public int locationY;
    public int height = 10;
    public int width = 10;

    // Empty overload constructor
    public Vertex()
    {
    }

    // Constructor for Vertex
    public Vertex(int locX, int locY)
    {
        // Set the variables
        this.locationX = locX;
        this.locationY = locY;
    }

    public Rectangle getRectangle()
    {
        // Create a rectangle out of the vertex information
        return new Rectangle(locationX, locationY, width, height);

    }
}

Graph 类代码

class Graph
{
    //verteces;
    public Vertex[,] verteces = new Vertex[10, 10];

    public Graph()
    {

        // Generate the graph, create the vertexs
        for (int i = 0; i <= 10; i++)
        {
            // Create 10 Vertexes with different coordinates
            verteces[0, i] = new Vertex(0, i);
        }
    }

}

最佳答案

看起来像是绘图循环中的异常

最后一次调用:

newGraph.verteces[0,i]

失败并显示 OutOfRangeException 你不应该重复i <= 10 ,但是到i < 10

关于c# - 为什么 DrawRectangle 在我的 PictureBox 中绘制一个十字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9717315/

相关文章:

c# - 在图像为 16bpp 灰度时从位图中获取彩色图像

r - 根据 hist 中的值绘制条件颜色

c# - 将大量 (~1000-2000) Jpeg 图像合并为一个

Java 2D 图形 BufferedImage FillRect 问题

java - 是否可以指向数组内的多个数组?

c# - 无法在 SqlParameterCollection 上使用 FirstOrDefault

c# - 在启动 Windows 服务时,启动线程。我怎样才能做到这一点?

c# - Task.Delay() 挂起几分钟

r - 用m维数组选择N维数组的子集?

java - java中比较二维数组