Error in .net 3.5 PictureBox

Diamonddrake 0 Tallied Votes 197 Views Share

If you drop a picturebox control onto a form and set its SizeMode to zoom you will notice that no matter what you change the padding property to, it won't change the output.

The code for drawing the image in zoomed mode doesn't take in account the deflated rectangle it took the time out to create in the first place. just a simple change of scaling by the deflatedRectangle fixes the problem.

This method is called ImageRectangleFromSizeMode(SizeMode Mode);
create a new class inherit it from PictureBox, give it a constructor, override this method, and create a local version of the DeflateRect method and everything should work just fine. Although I did track down the problem, I actually created a new PictureBox control base with just these 2 methods from the original and much much more, Or else I would have just posted a working class.

Felt everyone should know about that error.

//Original Picturebox onPaint

  Rectangle rectangle = DeflateRect(base.ClientRectangle, this.Padding);
            if (_Image != null)
            {
                switch (mode)
                {
                    case PictureBoxSizeMode.Normal:
                    case PictureBoxSizeMode.AutoSize:
                        rectangle.Size = this._Image.Size;
                        return rectangle;

                    case PictureBoxSizeMode.StretchImage:
                        return rectangle;

                    case PictureBoxSizeMode.CenterImage:
                        rectangle.X += (rectangle.Width - _Image.Width) / 2;
                        rectangle.Y += (rectangle.Height - _Image.Height) / 2;
                        rectangle.Size = _Image.Size;
                        return rectangle;

                    case PictureBoxSizeMode.Zoom:
                        {
                            Size size = _Image.Size;
                            float num = Math.Min((float)(((float)base.ClientRectangle.Width) / ((float)size.Width)), (float)(((float)base.ClientRectangle.Height) / ((float)size.Height)));
                            rectangle.Width = (int)(size.Width * num);
                            rectangle.Height = (int)(size.Height * num);
                            rectangle.X = (base.ClientRectangle.Width - rectangle.Width) / 2;
                            rectangle.Y = (base.ClientRectangle.Height - rectangle.Height) / 2;
                            return rectangle;
                        }
                }

//Correct code

      public Rectangle ImageRectangleFromSizeMode(PictureBoxSizeMode mode)
        {
             Rectangle rectangle = DeflateRect(base.ClientRectangle, this.Padding);
            if (_Image != null)
            {
                switch (mode)
                {
                    case PictureBoxSizeMode.Normal:
                    case PictureBoxSizeMode.AutoSize:
                        rectangle.Size = this._Image.Size;
                        return rectangle;

                    case PictureBoxSizeMode.StretchImage:
                        return rectangle;

                    case PictureBoxSizeMode.CenterImage:
                        rectangle.X += (rectangle.Width - _Image.Width) / 2;
                        rectangle.Y += (rectangle.Height - _Image.Height) / 2;
                        rectangle.Size = _Image.Size;
                        return rectangle;

                    case PictureBoxSizeMode.Zoom:
                        {
                            Size size = _Image.Size;
                            float num = Math.Min((float)(((float)rectangle.Width) / ((float)size.Width)), (float)(((float)rectangle.Height) / ((float)size.Height)));
                            rectangle.Width = (int)(size.Width * num);
                            rectangle.Height = (int)(size.Height * num);
                            rectangle.X = (base.ClientRectangle.Width - rectangle.Width) / 2;
                            rectangle.Y = (base.ClientRectangle.Height - rectangle.Height) / 2;
                            return rectangle;
                        }
                }
            }
            return rectangle;
        }

//Here is the deflateRect method from Layout utilities.
        public static Rectangle DeflateRect(Rectangle rect, Padding padding)
        {
            rect.X += padding.Left;
            rect.Y += padding.Top;
            rect.Width -= padding.Horizontal;
            rect.Height -= padding.Vertical;
            return rect;

        }