2013-03-14 206 views
1

我有一個圖片框,大小爲710x238,我正在使用它來顯示更大的圖像。當我加載圖像時,它會加載圖像而不會扭曲它。它效果很好。但是,當我旋轉圖像,並嘗試在PictureBox中顯示該圖像時,圖像大小更改爲正方形...旋轉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以及位圖。

回答

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; 
+0

但圖片框會扭曲的形象。 ..或沒有? – Ladessa 2013-03-14 13:32:19

+0

它會將圖像剪切到PictureBox的邊界,但不會「扭曲」它(拉伸/縮放) – Kohanz 2013-03-14 13:37:19

+0

但我需要圖像在PictureBox中顯示完整,PictureBoxSizeMode.Zoom可以工作,但旋轉時會調整圖像的大小錯誤 – Ladessa 2013-03-14 13:38:24

0

你可以試試:

pictureBox1.SizeMode =PictureBoxSizeMode.StretchImage; 

如果要旋轉PictureBox你可以嘗試thisthis.

+0

我的代碼旋轉圖像。我想旋轉PictureBox – Ladessa 2013-03-14 13:45:32