How to manually invoke PictureBox.Resize?

  • Thread starter Thread starter nvx
  • Start date Start date
N

nvx

Hello,
I experience a strange problem with PictureBox I draw onto. My
application has a fill-docked TabControl on its form and there is
PictureBox on one of the TabPages. If I resize the form (hence the
PictureBox should resize too) WITHOUT switching to the TabPage
containing the PictureBox before drawing onto it, the PictureBox does
not change its dimensions and the Bitmap of the size of PictureBox I
create before drawing has wrong dimensions as well. After switching to
the TabPage containing the PictureBox the image drawn before just
stretches/shrinks depending on whether I have enlarged or made the
form smaller.

An example:
1. At this point the PictureBox is 800x600 pixels and is placed on a
TabPage which is not selected.
2. We resize the form (maximize it) and the PictureBox should now be
approx. 1000x720 pixels.
3. Now we draw something onto the PictureBox: we take its dimensions,
which are according to PB's properties STILL 800x600 (!!!), create a
Bitmap and draw what we want. After that we flush the bitmap onto the
graphics using graphics.DrawImageUnscaled(...). IT IS CLEAR THAT THE
IMAGE WE HAVE DRAWN HAS WRONG DIMENSIONS, BUT HOW TO ENSURE THE
PICTUREBOX CHANGES ITS DIMENSIONS EVEN IF IT IS NOT VISIBLE?
4. After selecting the TabPage with PictureBox the image is
stretched...


Well, is there a way to "invoke" the Resize of PictureBox / to force
it to correct its dimensions according to dimensions of its parent
control and current dock style?

Thanks a lot for any help...

Best regards
nvx
 
Here is something you might be able to use to work around the problem.
Before trying to access the size of the picturebox, explicitly set its
tabpage to be the size of the selectedtab.

//tabPage2 holds the pictureBox
tabPage2.Size = tabControl1.SelectedTab.Size;
Size size = pictureBox1.Size;

============================
Clay Burch
Syncfusion, Inc.
 
Dear ClayB,
thank you very much for the advice, it works great! :)

Have a nice day...

Best regards
nvx
 
Why and how do you "Draw something" onto a picture box??

You should probably read the GDI+ FAQ including the article about the #1
most asked question, how and where to obtain your Graphics object and
what event driven systems are all about.
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Dear Bob,
I need to plot graphs, export them and such. This is why I use
PictureBox instead of a direct drawing onto a Form, Panel etc. The
drawing is done in pictureBox_Paint(...) method. I know you have
stated many times in your GDI+ FAQ that PictureBox is not the right
control for drawing, but I use it clearly because it (I took the
liberty of borrowing the following phrase from your FAQ...) "carries
baggage around with it" I would have to code myself if I used another
control (direct saving of the drawn graphics into a PNG/JPG/... file
and so on).

I'm aware of one other control that was more or less acceptable for my
purpose (Image), but I found PictureBox better for some reason. Please
do not ask me why, because it is a long time I was selecting the
control and I really do not remember...

Anyway, thank you for your interest... :)

Best regards
nvx
 
You could have equally used a Panel or even just the Control base-class as a
placeholder in this case.

--
--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Umm, I remember there was a minor detail that prevented me from using
the Panel, but I unfortunately can't remember which one it was.
Nevertheless, the fact that you say Panel is more than sufficient
tells me I should think about it once more... Thanks for the hint, I
will definitely try to compare these two controls again and re-code
it.

Best regards
nvx
 
Bob,
thanks again for the hint. I have used the Panel and it works just
fine... I wish I remembered the reason why I opted for the PictureBox
back then. It does not matter anyway, but I'd like to know why I did
such a stupid thing... ;)

Best regards
nvx
 
Back
Top