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

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

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

最佳答案

只要 Windows 要您绘制图片框,就会调用图片框绘制方法。看起来您有时只绘制矩形。

if (IsRun())

更改您的代码以始终进行绘图。

即此代码不会绘制矩形。 Ben 的例子会在哪里。

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

关于c# 在图片框上绘制一个矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5941979/

相关文章:

c# - System.IO.FileNotFoundException : On InitializeComponent caused by own control

c# - 如何通过 SOAP 使用 WCF 服务将大型 Zip 文件 (50MB) 传输到任何客户端?

c# - asp.net core 2.2 不使用 WebApp Azure 配置中设置的 ConnectionString

c# - WCF 的不可序列化数据成员

c# - 从大字符串中提取数据

c# - MonoTouch.Dialog 并排放置两列

c# - FileStream 和创建文件夹

c# - 我应该重复使用点和矩形还是创建新的?

c# - Gzip压缩asp.net c#

c# - 最小起订量一个类中只有一个方法