C# graphics flickering

后端 未结 4 2011
慢半拍i
慢半拍i 2020-12-08 22:46

I am working on kind of drawing program but I have a problem with flickering while moving a mouse cursor while drawing a rubberband line. I hope you can help me to remove th

4条回答
  •  悲哀的现实
    2020-12-08 23:34

    Fixed and working code.

    public partial class Form1 : Form
    {
        int x1, y1, x2, y2;
        bool drag = false;
    
        Bitmap bm = new Bitmap(1000, 1000);
        Graphics bmg;
    
    
        public Form1()
        {
            InitializeComponent();
        }
    
        private void Form1_Load(object sender, EventArgs e)
        {
            bmg = Graphics.FromImage(bm);
        }
    
        private void pictureBox_MouseDown(object sender, MouseEventArgs e)
        {
            drag = true;
            x1 = e.X;
            y1 = e.Y;
        }
    
        private void pictureBox_MouseUp(object sender, MouseEventArgs e)
        {
            drag = false;
    
            bmg.DrawLine(Pens.Black, x1, y1, e.X, e.Y);
            pictureBox.Invalidate();
        }
    
        private void pictureBox_MouseMove(object sender, MouseEventArgs e)
        {
            if (drag)
            {
                x2 = e.X;
                y2 = e.Y;
                pictureBox.Invalidate();
            }
        }
    
        private void pictureBox_Paint(object sender, PaintEventArgs e)
        {
            if (drag) {
                e.Graphics.DrawImage(bm, 0, 0);
                e.Graphics.DrawLine(Pens.Black, x1, y1, x2, y2);            
            }
            else {
                e.Graphics.DrawImage(bm, 0, 0);
            }
        }
    }
    

提交回复
热议问题