Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have an application which uses the mouse to free-draw a rectangle on a picbox image. However the rectangle only shows up behind the picbox, rather than on top of it. Is there a property i can set which can fix this? (show rect on top of picbox image rather than behind it). Here is the code:

   System.Drawing.Graphics picboxGraphics;
    bool mDown = false;
    int mouseX;
    int mouseY;

    private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
    {
        mDown = true;
        mouseX = e.X;
        mouseY = e.Y;
    }

    private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        if (mDown == true)
        {
            this.Refresh();
            Pen drawPen = new Pen(Color.Red, 5);
            int width = e.X - mouseX, height = e.Y - mouseY;
            Rectangle rect = new Rectangle(mouseX, mouseY, width * Math.Sign(width), height * Math.Sign(height));
            picboxGraphics = this.CreateGraphics();
            picboxGraphics.DrawRectangle(drawPen, rect);
        }
    }

    private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
    {
        mDown = false;
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
355 views
Welcome To Ask or Share your Answers For Others

1 Answer

You creating graphics from form which is behind the picbox you can create graphics from picbox's image and draw someting. But if you want layer system you can draw your thins on an transparent image and combine them. with this vay you can make an undo or delete layer system.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...