如何在c#中旋转图像?

时间:2011-06-21 08:43:22

标签: c# image-processing

我有一张我在网站上显示的图片。这是用c#编写的。我想让我的用户能够点击旋转图像的按钮。这将旋转服务器上的实际图像,以便下次显示时,它将以正确的方式显示。

类似于facebook如何进行图像旋转?

3 个答案:

答案 0 :(得分:1)

您真的需要在服务器上旋转图像吗?为什么不存储具有存储旋转值的图像的属性,如90,180,270 ...并在每次检索图像时应用此项,并在用户旋转图像后更新/保存属性值

请参阅this教程,了解如何旋转图片或google它会发现大量样本

答案 1 :(得分: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;

答案 2 :(得分: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;
    }