Reputation: 624
I have many images and coordinates of them with width and height. A picture is put in a picturebox and I send the coordinates to draw the rectangle on it. There are many pictureboxes on a panel.
I send their paths to a PicturePanel
class also with some coordinates and width/height properties to draw a rectangle. However, my problem is that, it draws it, and immediately deletes it. If I don't put a messagebox after each image, i don't 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);
}
}
This is in
private void PictureBox_Paint(object sender, PaintEventArgs e).
A for loop is in another class, creates a picturebox, and initializes its x, y etc. however, it draws and immediately deletes it. or sometimes it doesn't even draw.
If I don't put a messagebox after each image, I don't even see the rectangles. Can you help me?
Upvotes: 8
Views: 51873
Reputation: 34421
See code below. I added a rectangle instead of a picture just 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);
}
}
}
}
Upvotes: 1
Reputation: 5231
The picturebox paint method is being called whenever Windows wants you to paint your picture box. It looks like you only draw the rectangle some of the time.
if (IsRun())
Change your code to always do your drawing.
i.e. This code will not draw a rectangle. where Ben's example will.
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;
}
}
Upvotes: 4
Reputation: 997
I am not sure I fully understand your question but if all you want is to draw a rectangle, the following code will do it:
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);
}
}
Upvotes: 3