I have an image I display on my website. Which is written in c#. I want to give my user the ability to click on a button whic开发者_运维技巧h rotates the image. This will rotate the actual image on the server so next time it is displayed it is displayed the correct way.
Similar to how facebook has image rotation?
Do you really need to rotate the image on the server? Why not just store a property with the image which stores the rotation value like 90, 180, 270... and apply this every time image is retrieved and update/save the property value once user rotates the image
see this tutorial for how to rotate an image or google it you will find a lot of samples
//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;
}
精彩评论