在 C# 中刷新 PictureBox

发布于 2024-11-04 23:36:06 字数 383 浏览 4 评论 0原文

我创建了一个矩形列表并将它们绘制在 PictureBox 中。在代码的其他地方,我们从列表中删除了一些矩形,但是当我调用 PictureBox.Refresh() 时,它显示了之前的结果:所有矩形。

我尝试创建图片的克隆,并一一重新绘制所有矩形,但它有同样的问题。

请给我一些关于如何绘制当前矩形列表的想法。

Rectangle r = lanes[i];//lanes is list of rectangles
Pen pen = new Pen(Color.Red, 2);
Graphics g = pictureBox1.CreateGraphics();
g.DrawRectangle(pen, r);

I created a list of rectangles and draw them in a PictureBox. Elsewhere in the code we remove some rectangles from the list, but when I call PictureBox.Refresh() it shows the previous result: all the rectangles.

I tried creating a clone of the picture, and repainting all the rectangles one by one, but it has the same problem.

Please, can you give me some ideas on How to paint the current rectangles-list.

Rectangle r = lanes[i];//lanes is list of rectangles
Pen pen = new Pen(Color.Red, 2);
Graphics g = pictureBox1.CreateGraphics();
g.DrawRectangle(pen, r);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

念三年u 2024-11-11 23:36:06

您没有提供足够的信息,我不知道“它没有提供正确的信息”是什么意思。

上面绘制矩形的代码应该放入控件的 Paint 事件处理程序中。而当你想让图片框重画时,调用Invalidate方法(你可能还需要调用Update方法)。

You haven't provided enough information, and I have no idea what "it does not give the correct one" means.

Your code above that draws the rectangles should go in the control's Paint event handler. And when you want the picture box to redraw, call the Invalidate method (you may also need to call the Update method).

孤独患者 2024-11-11 23:36:06

出于兴趣,我继续实施了这个,或多或少是我认为应该做的。

这是我的代码。

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 class Form1 : Form
    {
        private static List<Rectangle> rectangles = new List<Rectangle> {
            //            x,y,w,h
            new Rectangle(0,0,10,10),
            new Rectangle(10,10,10,10),
            new Rectangle(10,40,10,10),
            new Rectangle(60,20,10,10),
            new Rectangle(90,10,10,10),
        };
        private Label label1;

        private RectanglePictureBox rectPicBox1;

        public Form1() {
            InitializeComponent();
            this.rectPicBox1.Rectangles = rectangles;
        }

        private void rectPicBox1_Click(object sender, EventArgs e) {
            if ( rectangles.Count <= 0 ) {
                Console.Beep(); // nothing left to remove!
            } else {
                rectangles.RemoveAt(rectangles.Count - 1);
                rectPicBox1.Rectangles = rectangles;
            }
        }

        #region InitializeComponent (Modified Manually)

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent() {
            this.rectPicBox1 = new WindowsFormsApplication1.RectanglePictureBox();
            this.label1 = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.rectPicBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // rectPicBox1
            // 
            this.rectPicBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.rectPicBox1.BackColor = System.Drawing.SystemColors.ControlLightLight;
            this.rectPicBox1.Location = new System.Drawing.Point(1, 1);
            this.rectPicBox1.Name = "rectPicBox1";
            this.rectPicBox1.Size = new System.Drawing.Size(257, 131);
            this.rectPicBox1.TabIndex = 0;
            this.rectPicBox1.TabStop = false;
            this.rectPicBox1.Click += new System.EventHandler(this.rectPicBox1_Click);
            // 
            // label1
            // 
            this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(2, 138);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(254, 13);
            this.label1.TabIndex = 1;
            this.label1.Text = "Clicking on the picture to removes the last rectangle.";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(259, 156);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.rectPicBox1);
            this.Name = "Form1";
            this.Text = "Rectangles";
            ((System.ComponentModel.ISupportInitialize)(this.rectPicBox1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        #region Component Model

        private System.ComponentModel.IContainer components = null;

        protected override void Dispose(bool disposing) {
            if ( disposing && (components != null) ) {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #endregion


    }

    ////////////////////////////////////////////////////////////////////////////

    class RectanglePictureBox : PictureBox
    {
        public static Color[] _colors = { 
            Color.Red, Color.Green, Color.Blue, Color.Orange 
        };

        public List<Rectangle> Rectangles {
            set { Image = ImageOf(value); }
        }

        private Bitmap ImageOf(List<Rectangle> rectangles) {
            Bitmap result = new Bitmap(Size.Height, Size.Width);
            Graphics graphics = Graphics.FromImage(result);
            for ( int i = 0; i < rectangles.Count; ++i ) {
                Brush brush = new SolidBrush(_colors[i % _colors.Length]);
                graphics.FillRectangle(brush, rectangles[i]);
            }
            return result;
        }

    }


}

此代码的发布不附带任何保证(明示或暗示)。都是你的了。用它做任何你喜欢做的事。无论发生什么,这都不是我的问题!

干杯。基思.

Out of interest I went-ahead and implemented this, more or less as I think it should be done.

Here's my 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 class Form1 : Form
    {
        private static List<Rectangle> rectangles = new List<Rectangle> {
            //            x,y,w,h
            new Rectangle(0,0,10,10),
            new Rectangle(10,10,10,10),
            new Rectangle(10,40,10,10),
            new Rectangle(60,20,10,10),
            new Rectangle(90,10,10,10),
        };
        private Label label1;

        private RectanglePictureBox rectPicBox1;

        public Form1() {
            InitializeComponent();
            this.rectPicBox1.Rectangles = rectangles;
        }

        private void rectPicBox1_Click(object sender, EventArgs e) {
            if ( rectangles.Count <= 0 ) {
                Console.Beep(); // nothing left to remove!
            } else {
                rectangles.RemoveAt(rectangles.Count - 1);
                rectPicBox1.Rectangles = rectangles;
            }
        }

        #region InitializeComponent (Modified Manually)

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent() {
            this.rectPicBox1 = new WindowsFormsApplication1.RectanglePictureBox();
            this.label1 = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.rectPicBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // rectPicBox1
            // 
            this.rectPicBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.rectPicBox1.BackColor = System.Drawing.SystemColors.ControlLightLight;
            this.rectPicBox1.Location = new System.Drawing.Point(1, 1);
            this.rectPicBox1.Name = "rectPicBox1";
            this.rectPicBox1.Size = new System.Drawing.Size(257, 131);
            this.rectPicBox1.TabIndex = 0;
            this.rectPicBox1.TabStop = false;
            this.rectPicBox1.Click += new System.EventHandler(this.rectPicBox1_Click);
            // 
            // label1
            // 
            this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(2, 138);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(254, 13);
            this.label1.TabIndex = 1;
            this.label1.Text = "Clicking on the picture to removes the last rectangle.";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(259, 156);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.rectPicBox1);
            this.Name = "Form1";
            this.Text = "Rectangles";
            ((System.ComponentModel.ISupportInitialize)(this.rectPicBox1)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        #region Component Model

        private System.ComponentModel.IContainer components = null;

        protected override void Dispose(bool disposing) {
            if ( disposing && (components != null) ) {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #endregion


    }

    ////////////////////////////////////////////////////////////////////////////

    class RectanglePictureBox : PictureBox
    {
        public static Color[] _colors = { 
            Color.Red, Color.Green, Color.Blue, Color.Orange 
        };

        public List<Rectangle> Rectangles {
            set { Image = ImageOf(value); }
        }

        private Bitmap ImageOf(List<Rectangle> rectangles) {
            Bitmap result = new Bitmap(Size.Height, Size.Width);
            Graphics graphics = Graphics.FromImage(result);
            for ( int i = 0; i < rectangles.Count; ++i ) {
                Brush brush = new SolidBrush(_colors[i % _colors.Length]);
                graphics.FillRectangle(brush, rectangles[i]);
            }
            return result;
        }

    }


}

This code is published with NO warranties (explicit or implied). It's all yours. Do whatever you like with it. Just whatever happens, IT'S NOT MY PROBLEM!

Cheers. Keith.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文