2011-06-21 66 views
-1

我有我的網站上顯示的圖像。這是用C#編寫的。我想讓我的用戶能夠點擊旋轉圖像的按鈕。這將旋轉服務器上的實際圖像,以便下次顯示時以正確的方式顯示。如何在c#中旋轉圖像?

與facebook的圖像旋轉類似嗎?

+0

的Silverlight或ASP.NET的? –

+0

你的問題是什麼? – razlebe

+1

你的問題是什麼? ** 0)**旋轉數學** 1)**您選擇的API,即您丟失了手冊** 2)**將其保留在服務器上** 3)**您的圖像爲在C#中? –

回答

1

你真的需要旋轉服務器上的圖像嗎?爲什麼不只是存儲一個屬性,其中存儲旋轉值的圖像像90,180,270 ...,並且每次檢索圖像時應用此屬性,並且一旦用戶旋轉圖像,就更新/保存屬性值

請參閱this教程如何旋轉圖像或谷歌它,你會發現很多樣品

+0

請注意,您鏈接的教程是用於在客戶端上旋轉的。不要在ASP.NET服務器代碼中這樣做! – Niki

1
//Create Image element 
Image rotated270 = new Image(); 
rotated270.Width = 150; 

//Create source 
BitmapImage bi = new BitmapImage(); 

//BitmapImage properties must be in a BeginInit/EndInit block 
bi.BeginInit(); 
bi.UriSource = new Uri(@"pack://application:,,/sampleImages/watermelon.jpg"); 

//Set image rotation 
bi.Rotation = Rotation.Rotate270; 
bi.EndInit(); 

//set image source 
rotated270.Source = bi; 
0
public static Image RotateImage(Image image, Size size, float angle) 
    { 
     if (image == null) 
     { 
      throw new ArgumentNullException("image"); 
     } 

     if (size.Width < 1 || size.Height < 1) 
     { 
      throw new ArgumentException("size must be larger than zero."); 
     } 

     Bitmap tempImage = new Bitmap(size.Width, size.Height); 

     using (Graphics tempGraphics = Graphics.FromImage(tempImage)) 
     { 
      PointF center = new PointF((float)size.Width/2F, (float)size.Height/2F); 

      tempGraphics.TranslateTransform(center.X, center.Y, MatrixOrder.Prepend); 

      tempGraphics.RotateTransform(angle != 180F ? angle : 182F/*at 180 exact angle the rotate make a small shift of image I don't know why!*/); 

      tempGraphics.TranslateTransform(-center.X, -center.Y, MatrixOrder.Prepend); 

      tempGraphics.DrawImage(image, new PointF()); 
     } 

     return tempImage; 
    } 
+0

請注意,您不應該在ASP.NET服務器中這樣做! System.Drawing.Imaging命名空間中的類不支持在Windows或ASP.NET服務中使用。 – Niki