Ladessa
Ladessa

Reputation: 1003

Rotate PictureBox change size

I have a picture box, size 710x238 that I'm using to display an image that is much larger. When I load the image, It loads the image without distorting it. It works great. But when I rotate the image and try to display that in the PictureBox, the image size change to a square...

How do I rotate the PictureBox along with the image so that the rotated image is not distorted?

Here is my code

pictureBox1.SizeMode = PictureBoxSizeMode.Zoom; //property for load without distorting

and the code for rotate (this code rotate the Image...I wanna rotate the pictureBox)

Bitmap oldBitmap = (Bitmap)pictureBox1.Image;
float angle = 90;
var newBitmap = new Bitmap(oldBitmap.Width, oldBitmap.Height);

var graphics = Graphics.FromImage(newBitmap);
graphics.TranslateTransform((float)oldBitmap.Width / 2, (float)oldBitmap.Height / 2);
graphics.RotateTransform(angle);
graphics.TranslateTransform(-(float)oldBitmap.Width / 2, -(float)oldBitmap.Height / 2);
graphics.DrawImage(oldBitmap, new Point(0, 0));
pictureBox1.Image = newBitmap;

How can I rotate the PictureBox? I wanna rotate the PictureBox along with the Bitmap.

Upvotes: 1

Views: 2062

Answers (2)

Kohanz
Kohanz

Reputation: 1562

Alright, I think I finally understand what you need. Keep the SizeMode as Zoom and try this:

pictureBox1.SizeMode = PictureBoxSizeMode.Zoom; // or PictureBoxSizeMode.StretchImage

...

int height = pictureBox1.Height;
int width = pictureBox1.Width;
pictureBox1.Width = height;
pictureBox1.Height= width;
pictureBox1.Image = newBitmap;

Upvotes: 1

Mohammad Arshad Alam
Mohammad Arshad Alam

Reputation: 9862

you can try :

pictureBox1.SizeMode =PictureBoxSizeMode.StretchImage;

if you want to rotate PictureBox you can try this and this.

Upvotes: 0

Related Questions