Draw rectangle over a picturebox problem

Posted on 2008-11-09
Last Modified: 2012-05-05
Hi all. I try to draw a rectangle over a picturebox, but I face a problem. The object (rectangle) is drawing successfully but the problem is that when I move some other picturebox which is over the first picturebox several times and it passed over the location where I have drawn the object the rectangle disappears. I use picBox.CreateGraphics().DrawRectangle(...) to draw the rectangle over the pictureBox. How can I draw something over a pictureBox so it will not disappear after some other pictureBox passed over it? Thanks a lot.
Question by:mg_miro_86
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Do NOT use CreateGraphics().

    Instead, use the graphics supplied in the PAINT Event of the PictureBox.'

    It is passed via the "e" Parameter:
    public void pictureBox1_Paint(object sender, PaintEventArgs e)
        Graphics g = e.Graphics;

    Open in new window

    LVL 16

    Accepted Solution

    Instead of getting the Graphics of the pictureBox, try to get the graphics of the image displayed in the picturebox.
    Then handle it during the Paint event.

    This link will provide more detail
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Just noticed I have a small typo in my snippet.

    Obviously, this:


    Should be:


    If you use the Paint() Event graphics then you can draw "on top" any Image currently being displayed in the PictureBox...WITHOUT permanently changing the actual image.  Whether this is desirable or not really depends on your application...  =)

    Author Comment

    Thank you CuteBug:. You solution solved my problem. Thanks and for Idle_Mind for his reply. Have a nice day. ;)

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Suggested Solutions

    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now