Click here to Skip to main content
15,745,574 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have opened an image in picturebox and want to draw a rectangle on it in Red.
Though there are no errors I cannot see the rectangle :(
Can somebody help me to understand this please?

private void button6_Click(object sender, EventArgs e)
    openFileDialog1.Filter = "JPEG IMAGES|*.jpg";
    openFileDialog1.InitialDirectory = "C:\\Users\\jason\\Documents\\IProject\\code\\imageAlign\\imageAlign\\bin\\Debug";
    openFileDialog1.Title = "Open Image";
    if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        newImage3 = (Bitmap)(System.Drawing.Image.FromFile(openFileDialog1.FileName));
        pictureBox8.Width = newImage3.Width;
        pictureBox8.Height = newImage3.Height;
        pictureBox8.Image = newImage3;

        Pen pen = new Pen(Color.Red);
        Graphics g = pictureBox8.CreateGraphics();
        g.DrawRectangle(pen, 10, 10, 30, 30);

Updated 20-Feb-13 7:08am
Richard C Bishop 20-Feb-13 12:50pm    
When debugging, what line does this code fail at?

You are doing it in the wrong place. You need to override the OnPaint()[^] method and do your drawing there.
Share this answer
Sergey Alexandrovich Kryukov 20-Feb-13 19:50pm    
Sorry, this time I did not vote even though you are totally right.

The problem is that this correct answer can be very confusing, because you are not telling to override OnPaint of what. The proof of such confusion if Solution 2. I would never believe that someone can do such pointless thing as handling Paint of PictureBox, but it happens :-). Worse, it even words, but still totally pointless.

I put my solution, most likely you know it all...

The thing is: if you are going to do anything beyond showing a static image in a PictureBox, you should never use the PictureBox control. Abusing this control is the one of the most usual mistakes. It's totally pointless, because you need to override OnPaint or handle the event Paint, anyway, so you can just draw a bitmap and whatever you want on top of it. On what control?

Certainly, not PictureBox. This control is totally redundant, only needed to easy doing the simplest thing. If you do anything else, it only presents a hassle, giving nothing in return. Instead, you should use something "empty": Panel or, in most cases, a custom control based on Control. I will explain you how:

Please see my past answers:
How do I clear a panel from old drawing[^],
draw a rectangle in C#[^],
Append a picture within picturebox[^].

For more background on rendering, please see also these answers:
Drawing Lines between mdi child forms[^],
capture the drawing on a panel[^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
How to speed up my application?[^].

Good luck,
Share this answer

Richard mentioned it: Use Paint handler to "paint". Here is a complete, runnable example. Just create a new windows forms project and replace Program.cs file's content with the following:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace BoxOnPicture
    static class Program
        class ExampleForm : Form
            PictureBox m_picturebox;
            Pen m_penRed = new Pen(Color.Red, 2f);
            Rectangle m_rectangle = new Rectangle(10, 10, 30, 30);

            public ExampleForm()
                // Add a picturebox for testing
                m_picturebox = new PictureBox();
                m_picturebox.BackgroundImage = Bitmap.FromFile("Test.png"); // Use a valid image path
                m_picturebox.Dock = DockStyle.Fill;
                // Add a Paint event handler
                m_picturebox.Paint += m_picturebox_Paint;

            void m_picturebox_Paint(object sender, PaintEventArgs e)

                e.Graphics.DrawRectangle(m_penRed, m_rectangle);


        static void Main()
            // Run example form
            Application.Run(new ExampleForm());

I hope this helps?
Share this answer
defunktlemon 20-Feb-13 15:31pm    
It certainly does work - thank you johannestler. But it tiles the images again and again. This seems to be because of:
m_picturebox.Dock = DockStyle.Fill;
I changed the .Fill to other options but none give just a single full image.
Sergey Alexandrovich Kryukov 20-Feb-13 19:47pm    
This might work, but the solution is very much pointless. It it overcomplicated, just wasteful. There is absolutely need in PictureBox, that's the main problem. Please, don't do it.
johannesnestler 21-Feb-13 8:07am    
Yes, right. Your explainations are very good. Just quickly changed the code given by op...
johannesnestler 21-Feb-13 8:05am    
Yes, filled only vor example
defunktlemon 22-Feb-13 16:57pm    
ok, I'm a bit lost now. What code have you changed johannesnestler?
I looked at your links Sergey and I don't need anything to work dynamically or interactively on these images. At least, not at this stage.
Let me explain what I am trying to do overall. I have an image which has been thresholded, using Otsu Thresholding found on this website. In this image there are different assets which when corrupted in the image will show up white, the rest of the image being black. I need to scan this image, with the assets in individually marked sections, to find the section location of the defective assets. I then need to draw on the image a box surrounding the section and write some text, probably to a file detailing what the defect is. This is all I need to do with drawing on the image. Could you please advise me on the best approach to achieve this kind of drawing. Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900