Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Draw Rectangle onto Picture in PictureBox

Draw Rectangle onto Picture in PictureBox

Scheduled Pinned Locked Moved Managed C++/CLI
graphicshelpquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cherrymotion
    wrote on last edited by
    #1

    Hi all, I know, the question sounds simple, but I do not get it at the moment. Drawing a rectangle into a picturebox is no problem. But as soon as I loaded an image (here: a bitmap) into it, the image covers my rectangle and it is not to be seen anymore. I tried so much things now, getting a the Graphics-Handle of the image itself seemed to be most logic for me, like:

    Graphics ^g = e->Graphics->FromImage(this->pictureBox1);
    g->DrawRectangle(gcnew Pen(Color::Black,3),*m_AreaRoi[i].AreaRect);

    Failed! Another try with this:

    <pre>e->Graphics->DrawRectangle(gcnew Pen(Color::Black,3),*m_AreaRoi[i].AreaRect);

    inside the OnPaint()-Function also failed. The Project in this case is a Forms Control Library with a picturebox in it, as additional information. I just want to draw a "region of interest"-rectangle onto the image loaded in the picturebox, it can't be that difficult... Please help me! Thanks

    N 1 Reply Last reply
    0
    • C cherrymotion

      Hi all, I know, the question sounds simple, but I do not get it at the moment. Drawing a rectangle into a picturebox is no problem. But as soon as I loaded an image (here: a bitmap) into it, the image covers my rectangle and it is not to be seen anymore. I tried so much things now, getting a the Graphics-Handle of the image itself seemed to be most logic for me, like:

      Graphics ^g = e->Graphics->FromImage(this->pictureBox1);
      g->DrawRectangle(gcnew Pen(Color::Black,3),*m_AreaRoi[i].AreaRect);

      Failed! Another try with this:

      <pre>e->Graphics->DrawRectangle(gcnew Pen(Color::Black,3),*m_AreaRoi[i].AreaRect);

      inside the OnPaint()-Function also failed. The Project in this case is a Forms Control Library with a picturebox in it, as additional information. I just want to draw a "region of interest"-rectangle onto the image loaded in the picturebox, it can't be that difficult... Please help me! Thanks

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      Are you calling Invalidate() after finished drawing rectangle? If not, call this->pictureBox1->Invalidate() which will force the picture box control to redraw.

      Navaneeth How to use google | Ask smart questions

      C 1 Reply Last reply
      0
      • N N a v a n e e t h

        Are you calling Invalidate() after finished drawing rectangle? If not, call this->pictureBox1->Invalidate() which will force the picture box control to redraw.

        Navaneeth How to use google | Ask smart questions

        C Offline
        C Offline
        cherrymotion
        wrote on last edited by
        #3

        Great, it worked! Thank you!

        Graphics ^g = this->pictureBox1->CreateGraphics();
        g->DrawRectangle(gcnew Pen(Color::Black,3),*m_AreaRoi[i].AreaRect);
        this->pictureBox1->Invalidate();

        That's the way it does right. Instead of Invalidate I used pictureBox->Refresh() and the rectangle always just twinkled a short time and has gone away. I don't understand this behaviour, why does Refresh() after calling the functioning code above also "delete" the rectangle? So what are the Differences between Invalidate and Refresh? Thanks a lot!

        N 1 Reply Last reply
        0
        • C cherrymotion

          Great, it worked! Thank you!

          Graphics ^g = this->pictureBox1->CreateGraphics();
          g->DrawRectangle(gcnew Pen(Color::Black,3),*m_AreaRoi[i].AreaRect);
          this->pictureBox1->Invalidate();

          That's the way it does right. Instead of Invalidate I used pictureBox->Refresh() and the rectangle always just twinkled a short time and has gone away. I don't understand this behaviour, why does Refresh() after calling the functioning code above also "delete" the rectangle? So what are the Differences between Invalidate and Refresh? Thanks a lot!

          N Offline
          N Offline
          N a v a n e e t h
          wrote on last edited by
          #4

          I am not sure why Refresh didn't worked for you. When you call Invalidate(), it just invalidates the client area. It will not repaint immediately. It will be repainted when the control receives a WM_PAINT message. So Invalidate() is not synchronous. Refresh just calls other overload Invalidate(true) and Control.Update to force paint on the control. This is synchronous and you can see the effect immediately. I tried your sample and Refresh worked just fine on my machine. :)

          Navaneeth How to use google | Ask smart questions

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups