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

时间:2011-05-09 20:13:53

标签: c#

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

我将他们的路径发送到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循环在另一个类中,创建一个picturebox,并初始化它的x,y等。但是,它会绘制并立即删除它。或者有时它甚至不会画画。

如果我没有在每张图片后面放一个留言箱,我甚至看不到矩形。你能救我吗?

3 个答案:

答案 0 :(得分:4)

只要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;
            }
        }

答案 1 :(得分: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);
        }
  }

答案 2 :(得分:1)

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

m = mqtt.Client("clientid", 120, "8266test", "password")

m:lwt("/lwt", "offline", 0, 0) 

function main(client) 
    print("connected - at top of main")

m:publish("someval",12345,1,0, function(client)  
    rtctime.dsleep(SLEEP_USEC)      
    end)
end

m:on("connect", main)
m:on("offline", function(client) is_connected = false print ("offline") end)

m:connect(MQQT_SVR, 1883, 0, mainloop, 
                         function(client, reason) print("failed reason: "..reason) end)
相关问题