我有我的網站上顯示的圖像。這是用C#編寫的。我想讓我的用戶能夠點擊旋轉圖像的按鈕。這將旋轉服務器上的實際圖像,以便下次顯示時以正確的方式顯示。如何在c#中旋轉圖像?
與facebook的圖像旋轉類似嗎?
我有我的網站上顯示的圖像。這是用C#編寫的。我想讓我的用戶能夠點擊旋轉圖像的按鈕。這將旋轉服務器上的實際圖像,以便下次顯示時以正確的方式顯示。如何在c#中旋轉圖像?
與facebook的圖像旋轉類似嗎?
//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;
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;
}
請注意,您不應該在ASP.NET服務器中這樣做! System.Drawing.Imaging命名空間中的類不支持在Windows或ASP.NET服務中使用。 – Niki
的Silverlight或ASP.NET的? –
你的問題是什麼? – razlebe
你的問題是什麼? ** 0)**旋轉數學** 1)**您選擇的API,即您丟失了手冊** 2)**將其保留在服務器上** 3)**您的圖像爲在C#中? –