C#如何在PictureBox上绘制矩形?

8
我有许多图片及其宽度和高度的坐标。将图片放入picturebox中,然后发送坐标以在其上绘制矩形。一个面板上有许多pictureboxes。
我还向PicturePanel类发送它们的路径以及一些坐标和宽度/高度属性来绘制矩形。但是,我的问题是它会绘制出来,然后立即删除。如果我不在每个图像后面放置一个messagebox,我就看不到矩形。以下是代码;
if (IsRun())
{
    MessageBox.Show("rontool true");

    Rectangle ee = drawARectangle(xCoor, yCoor, MainScreen.tempR.wid / ratioOfx, MainScreen.tempR.heig / ratioOfy); // I wrote this, it only creates and returns the rectangle.
    //MessageBox.Show("x : " + xCoor + " y: " + yCoor + " width : " + (MainScreen.tempR.wid / ratioOfx) + " height: " + (MainScreen.tempR.heig / ratioOfy));
    using (Pen pen = new Pen(Color.Red, 2))
    {
        pictureBox.CreateGraphics().DrawRectangle(pen, ee);
       // e.Graphics.DrawRectangle(pen, ee);
    }
}

这是在

private void PictureBox_Paint(object sender, PaintEventArgs e). 

一个for循环在另一个类中创建了一个picturebox,并初始化它的x、y等,但它会绘制并立即删除它。有时甚至根本没有绘制。

如果我不在每个图像后面放置一个消息框,我甚至看不到矩形。你能帮我吗?

3个回答

4

每当Windows要求你绘制picturebox时,将调用picturebox paint方法。看起来你只有在某些时间才会绘制矩形。

if (IsRun())

将您的代码更改为始终进行绘图。
例如,这段代码不会绘制一个矩形,而Ben的示例会。
private bool _once = true;

        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (_once)
            {
                Rectangle ee = new Rectangle(10, 10, 30, 30);
                using (Pen pen = new Pen(Color.Red, 2))
                {
                    e.Graphics.DrawRectangle(pen, ee);
                }
                _once = false;
            }
        }

3

我不确定我完全理解你的问题,但如果你只想画一个矩形,下面的代码可以实现:

  Private void pictureBox_Paint(object sender, PaintEventArgs e) {
        Rectangle ee = new Rectangle(10, 10, 30, 30);           
        using (Pen pen = new Pen(Color.Red, 2)) {
            e.Graphics.DrawRectangle(pen, ee);
        }
  }

我在for循环中调用它,这样矩形就会消失。 - Ada
你需要展示比你在问题中展示的代码更多。更具体地说,那个循环中发生了什么。 - Gichamba

1
请看下面的代码。我添加了一个矩形来演示代码,而不是图片:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        const int ROWS = 3;
        const int COLUMNS = 4;
        const int WIDTH = 10;
        const int HEIGHT = 20;
        const int SPACE = 10;
        public Form1()
        {
            InitializeComponent();
            Panel panel = new Panel();
            panel.Width = COLUMNS * (WIDTH + SPACE);
            panel.Height = ROWS * (HEIGHT + SPACE);
            this.Controls.Add(panel);
            for (int rows = 0; rows < ROWS; rows++)
            {
                for (int cols = 0; cols < COLUMNS; cols++)
                {
                    PictureBox newPictureBox = new PictureBox();
                    newPictureBox.Width = WIDTH;
                    newPictureBox.Height = HEIGHT;
                    newPictureBox.Top = rows * (HEIGHT + SPACE);
                    newPictureBox.Left = cols * (WIDTH + SPACE);
                    panel.Controls.Add(newPictureBox);
                    newPictureBox.Paint +=new PaintEventHandler(pictureBox_Paint);

                }
            }
        }
        private void pictureBox_Paint(object sender, PaintEventArgs e) {
            Rectangle ee = new Rectangle(0, 0, WIDTH, HEIGHT);           
            using (Pen pen = new Pen(Color.Red, 2)) {
                e.Graphics.DrawRectangle(pen, ee);
            }
        }
     }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接