Determining max size of Image in PictureBox (.NET 2.0)

I have a PictureBox with Docking set to Fill.  I would like to find the dimensions of the rectangle that the PictureBox "Fills".

In .NET 1.1, pictureBox1.height and pictureBox1.width properties define those dimensions.

In .NET 2.0, I find that the the height and width properties of the PictureBox defines the height and width of the image itself.  In other words, the height and width properties no longer reflects the maximum space availiable to display the image.

How do I find those dimensions in .NET 2.0   It can't be the height width of the parent control, as Anchoring instead of Docking will yield different results.  The motive for finding the max size is so I can manually resize the image while maintaining aspect ratio and not clip for an image viewer like the Windows XP image viewer.



Answer this question

Determining max size of Image in PictureBox (.NET 2.0)

  • Raphael Gabbarelli

    Actually, it seems that the Fill isn't actually filling.  I'll see what's going on.
  • vsphp

    I must be missing something... the Size, Width and Height properties really do return what you are looking for: the actual dimensions of the control itself.

    PictureBox directly derives from Control and doesn't alter the Size, Width or Height properties. The properties will eventually call SetWindowPos.

    Are you really confident this is true


  • RameshPa_MSFT

    I was subscribing to "Resize" instead of "SizeChanged".  Setting PictureBox.Image in Resize overrides the Docking Fill   I thought Resize and SizeChanged did the same thing.  It was working in .NET 1.1.

    private void pictureBox1_Resize(object sender, EventArgs e)
    {
          
    this.pictureBox1.Image = null;
    }

    vs.

    private void pictureBox1_SizeChanged(object sender, EventArgs e)
    {
          this.pictureBox1.Image = null;
    }

    To replicate what I was experiencing:
    - create a winform
    - add a picturebox and dock fill
    - set the color of picturebox to something that's different from the form color
    - subscribe to the Resize event of picturebox with the resize snipet
    - build, run, and resize the win form
    - rebuild with sizechanged

    Anyone have some insight into why this is


  • Kroamian

    Hello

    I am brand new to VB, started yesterday. But from what I read from your code and is that resize is called just before and /or during your resizing and sizechanged after you resized.

    RvA

  • -Matt-

    If this is a bug in .NET 2.0, Microsoft should look into it...
  • xBoRISx

    Resize is also called after resize has completed, not before the resize.

  • Determining max size of Image in PictureBox (.NET 2.0)