简体   繁体   English

c#在图片框上绘制一个矩形?

[英]c# drawing a rectangle on a picturebox?

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. 我将它们的路径发送到PicturePanel类,同时使用一些坐标和宽度/高度属性来绘制矩形。 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. for循环在另一个类中,创建一个picturebox,并初始化它的x,y等。但是,它绘制并立即删除它。 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? 你能帮助我吗?

The picturebox paint method is being called whenever Windows wants you to paint your picture box. 只要Windows要您绘制图片框,就会调用图片框绘制方法。 It looks like you only draw the rectangle some of the time. 看起来你只是在某些时候绘制矩形。

if (IsRun())

Change your code to always do your drawing. 将代码更改为始终执行绘图。

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM