我有一个尺寸为710x238的图片框,我用它来显示更大的图像。当我加载图像时,它会加载图像而不会扭曲图像。它很棒。但是当我旋转图像并尝试在PictureBox中显示图像时,图像大小会变为正方形......
如何将PictureBox与图像一起旋转,以使旋转的图像不会失真?
这是我的代码
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom; //property for load without distorting
和旋转代码(此代码旋转图像......我想旋转图片框)
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;
如何旋转PictureBox?我想旋转PictureBox和Bitmap。
答案 0 :(得分:1)
好吧,我想我终于明白了你的需要。将SizeMode保持为缩放并尝试:
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom; // or PictureBoxSizeMode.StretchImage
...
int height = pictureBox1.Height;
int width = pictureBox1.Width;
pictureBox1.Width = height;
pictureBox1.Height= width;
pictureBox1.Image = newBitmap;
答案 1 :(得分:0)