C # drawing a rectangle in a picture?

I have many images and their coordinates with width and height. The image is placed in the image box, and I send the coordinates to draw a rectangle on it. The panel has a lot of images.

I send my paths to the class PicturePaneland with some coordinates and width / height properties to draw a rectangle. However, my problem is that she draws it and immediately removes it. If I do not put a message after each image, I do not see the rectangles. Here is the code:

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);
    }
}

It is in

private void PictureBox_Paint(object sender, PaintEventArgs e). 

A for the loop is in another class, creates a graphic box and initializes it x, y, etc. However, he draws and immediately removes it. or sometimes doesn’t even draw.

, . ?

+5
3

Paintbox paint , Windows , . , .

if (IsRun())

, .

. . .

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);
        }
  }
+3

See code below. I added a rectangle instead of an image to demonstrate the code:

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);
            }
        }
     }
}
0
source

All Articles