2012-03-08 72 views
0

我想使用圖形對象加載圖像,然後旋轉它(縱向或橫向),然後將其顯示在面板中(而不是圖片框)。使用圖形加載圖像並將其旋轉到面板

我該如何在面板中加載圖形?此外,在圖形對象上進行橫向或縱向旋轉最簡單的方法是什麼?

GDI必須用於旋轉和處理圖像,我需要一種方法來獲取圖形對象到面板中。

+1

的WinForms,asp.net,WPF,...? – Msonic 2012-03-08 18:55:05

+0

Gtk-Sharp?開羅? XWT? OpenGL的? WebkitSharp?你需要更多的標籤 – IanNorton 2012-03-08 18:56:00

+0

更新,謝謝指出。 – JamieB 2012-03-08 18:59:35

回答

2

使用Paint事件小組:

private void panel1_Paint(object sender, PaintEventArgs e) 
    { 
     int angle = 90; 
     Graphics g = e.Graphics; 
     Image i = new Bitmap(@"C:\Jellyfish.jpg"); 
     g.TranslateTransform((float)i.Width/2, (float)i.Height/2); 
     g.RotateTransform(angle); 
     g.TranslateTransform(-(float)i.Width/2, -(float)i.Height/2); 
     g.DrawImage(i, new Point(0,0)); 

    } 
+0

這是不是旋轉時裁剪原始圖像? – 2013-05-03 04:51:13

1

既然你是在談論一個面板,它是C#,我會猜測你指的是WinForms。

您可以使用RotateFlip方法旋轉任何Image實例,並且可以使用面板的BackgroundImage作爲Image。工作示例:

Bitmap bitmap = new Bitmap(@"D:\word.png"); 
bitmap.RotateFlip(RotateFlipType.Rotate90FlipNone); 
Form form = new Form() { Height = 400, Width = 600 }; 
Panel p = new Panel() { Height = 400, Width = 600, Left = 0, Top = 0}; 
form.Controls.Add(p); 
p.BackgroundImage = bitmap; 
form.Show(); 
+0

謝謝,儘管使用GDI來完成圖像的工作非常重要:) – JamieB 2012-03-08 19:02:18

+0

您是否因爲某些原因而無法使用RotateFlip方法? – driis 2012-03-08 19:04:46

+0

不,我目前正在使用您建議的方法,但是我需要使用GDI來操作圖像。旋轉後(縱向或橫向)它需要加載到面板中。 GDI代替RotateFlip方法我的意思是說。 – JamieB 2012-03-08 19:06:24