Draw Rectangle onto Picture in PictureBox
-
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
-
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
Are you calling
Invalidate()
after finished drawing rectangle? If not, callthis->pictureBox1->Invalidate()
which will force the picture box control to redraw.Navaneeth How to use google | Ask smart questions
-
Are you calling
Invalidate()
after finished drawing rectangle? If not, callthis->pictureBox1->Invalidate()
which will force the picture box control to redraw.Navaneeth How to use google | Ask smart questions
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!
-
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!
I am not sure why
Refresh
didn't worked for you. When you callInvalidate()
, it just invalidates the client area. It will not repaint immediately. It will be repainted when the control receives aWM_PAINT
message. SoInvalidate()
is not synchronous.Refresh
just calls other overloadInvalidate(true)
andControl.Update
to force paint on the control. This is synchronous and you can see the effect immediately. I tried your sample andRefresh
worked just fine on my machine. :)Navaneeth How to use google | Ask smart questions