开发者

On Image Drawing Save

I have created a small application in C#, in which I draw a rectangle when the mouse is moved.

However, when the form is minimized or maximized, the drawing is erased. Also, when I draw a second time, the drawing of the first rectangle is erased.

How can I solve this proble开发者_运维问答m? Here is the code that I currently have:

   int X, Y;
    Graphics G;
    Rectangle Rec;
    int UpX, UpY, DwX, DwY;


    public Form1()
    {
        InitializeComponent();
        //G = panel1.CreateGraphics();
        //Rec = new Rectangle(X, Y, panel1.Width, panel1.Height);
    }


    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        UpX = e.X;
        UpY = e.Y;
        //Rec = new Rectangle(e.X, e.Y, 0, 0);
        //this.Invalidate();
    }

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        DwX = e.X;
        DwY = e.Y;

        Rec = new Rectangle(UpX, UpY, DwX - UpX, DwY - UpY);
        Graphics G = pictureBox1.CreateGraphics();
        using (Pen pen = new Pen(Color.Red, 2))
        {
            G.DrawRectangle(pen, Rec);
        }
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {            
        if (e.Button == MouseButtons.Left)
        {
            // Draws the rectangle as the mouse moves
            Rec = new Rectangle(UpX, UpY, e.X - UpX, e.Y - UpY);
            Graphics G = pictureBox1.CreateGraphics();

            using (Pen pen = new Pen(Color.Red, 2))
            {
                G.DrawRectangle(pen, Rec);
            }
            G.Save();
            pictureBox1.SuspendLayout();
            pictureBox1.Invalidate();

        }

    }

    private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        pictureBox1.Update();            
    }


The reason why your drawings are getting erased is because you're drawing into a Graphics object you obtained by calling the CreateGraphics method. In particular, this line of your code is incorrect:

 Graphics G = pictureBox1.CreateGraphics();

As you've discovered, whenever the form is repainted (which happens when it is maximized, minimized, covered by another object on the screen, or in a number of other possible situations), everything that you've drawn into that temporary Graphics object is lost. The form completely repaints itself with its internal painting logic; it's completely forgotten about what you temporarily drew on top of it.

The correct way to draw persistent images in WinForms is to override the OnPaint method of the control that you want to draw onto (or, you could also handle the Paint event). So, if you wanted to paint onto your form, you would place your drawing code into the following method:

protected override void OnPaint(PaintEventArgs e)
{
    // Call the base class first
    base.OnPaint(e);

    // Then insert your custom drawing code
    Rec = new Rectangle(UpX, UpY, DwX - UpX, DwY - UpY);
    using (Pen pen = new Pen(Color.Red, 2))
    {
        e.Graphics.DrawRectangle(pen, Rec);
    }
}

And to trigger a re-paint, you just call the Invalidate method in any of the mouse events (MouseDown, MouseMove, and MouseUp):

this.Invalidate();

Note, however, that there's absolutely no reason to call the Update method in the Paint event handler. All that calling the Update method does is force the control to repaint itself. But that is already happening when the Paint event gets raised!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜