Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# image
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 Frown | :(
Can somebody help me to understand this please?
thanks
 
     
        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);
 
            }
        }
Posted 20-Feb-13 6:47am
Edited 20-Feb-13 7:08am
v2
Comments
richcb at 20-Feb-13 12:50pm
   
When debugging, what line does this code fail at?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You are doing it in the wrong place. You need to override the OnPaint()[^] method and do your drawing there.
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 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...
 
Cheers,
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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 vb.net application?[^].
 
Good luck,
—SA
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi,
 
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;
                Controls.Add(m_picturebox);
                // 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);
            }
 
        }
 
        [STAThread]
        static void Main()
        {
            // Run example form
            Application.Run(new ExampleForm());
        }
    }
}
 
I hope this helps?
  Permalink  
Comments
defunktlemon at 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 at 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.
—SA
johannesnestler at 21-Feb-13 8:07am
   
Yes, right. Your explainations are very good. Just quickly changed the code given by op...
johannesnestler at 21-Feb-13 8:05am
   
Yes, filled only vor example
defunktlemon at 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.
johannesnestler at 25-Feb-13 5:27am
   
I mean, I changed your code to create the example. But Sergey is right, the picturebox control itself is needless. If you draw a rectangle in a paint method, you can also draw the image there. (you can use any control's paint event for that). Anyway, the above code should give you the idea - and works...
defunktlemon at 25-Feb-13 5:48am
   
ahh! I understand. Thank you johanne.
I have actually decided to go with the panel option, which has worked out well. At least, until I added some code to get the pixel values which created another problem. But that's another story - one found at the link below, if you're interested ;)
http://www.codeproject.com/Questions/551825/namingplusbackgroundpluspanelplusimage?cmt=403438#cmt2_551825

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



Advertise | Privacy | Mobile
Web04 | 2.8.141022.2 | Last Updated 20 Feb 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100