1

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.

Kohanz
  • 1,510
  • 16
  • 35
Ladessa
  • 985
  • 4
  • 24
  • 50

2 Answers2

1

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;
Kohanz
  • 1,510
  • 16
  • 35
  • but the PictureBox will distort the image...or no? – Ladessa Mar 14 '13 at 13:32
  • It will clip the image to the boundaries of the PictureBox, but not "distort" it (stretch/scale it) – Kohanz Mar 14 '13 at 13:37
  • but I need that the image appears full in pictureBox, the PictureBoxSizeMode.Zoom works, but when rotate it resize the image wrong – Ladessa Mar 14 '13 at 13:38
  • Ah, you want to rotate the PictureBox to fit the image. See my edit. – Kohanz Mar 14 '13 at 13:45
  • but I dont need that the pictureBox have the same size from image..The image is bigger than the picBox..I use sizeModo.Zoom for stretch the image ...but when I rotate the image...I need the same size before rotate but rotated.... – Ladessa Mar 14 '13 at 13:48
  • Zoom will stretch the image to fit the picture box, so of course it will distort the image when it rotates. After your image is rotated, it no longer has the same aspect ratio, relative to the PictureBox, that it had before rotating. – Kohanz Mar 14 '13 at 13:51
  • Can you size your PictureBox as 710x710 and use PictureBoxSizeMode.Normal? – Kohanz Mar 14 '13 at 13:52
  • How can I rotate the PictureBox...The code from my question rotate the Bitmap – Ladessa Mar 14 '13 at 14:15
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/26161/discussion-between-illdev-and-kohanz) – Ladessa Mar 14 '13 at 14:16
0

you can try :

pictureBox1.SizeMode =PictureBoxSizeMode.StretchImage;

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

Community
  • 1
  • 1
Mohammad Arshad Alam
  • 9,694
  • 6
  • 38
  • 61