【问题标题】:c# drawing a rectangle on a picturebox?c# 在图片框上画一个矩形?
【发布时间】:2011-05-09 20:13:53
【问题描述】:

我有许多图像和它们的宽度和高度坐标。将图片放入图片框中,然后我发送坐标以在其上绘制矩形。面板上有许多图片框。

我将它们的路径发送到PicturePanel 类,还带有一些坐标和宽度/高度属性来绘制一个矩形。但是,我的问题是,它绘制它并立即将其删除。如果我不在每张图片后放置一个消息框,我就看不到矩形。这是代码;

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 循环在另一个类中,创建一个图片框,并初始化它的 x、y 等。然而,它绘制并立即删除它。或者有时它甚至不绘制。

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

【问题讨论】:

    标签: c#


    【解决方案1】:

    每当 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;
                }
            }
    

    【讨论】:

      【解决方案2】:

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

        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 循环中调用它,这样矩形就会消失。
      • 您将需要显示比您在问题中所做的更多的代码。更具体地说,该循环中发生了什么。
      【解决方案3】:

      见下面的代码。我添加了一个矩形而不是图片只是为了演示代码:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-01-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-21
        相关资源
        最近更新 更多