[英]C# Image rotation
在asp.net中旋转图像的最佳方法是什么?
我确实使用了matrix.rotateAt但我无法让它工作所以请告诉我最好的方法是什么?
我应该写出讨厌用图像对象旋转图像。
Image myImage = Image.FromFile("myimage.png");
myImage.RotateFlip(RotateFlipType.Rotate180FlipNone);
http://msdn.microsoft.com/en-us/library/system.drawing.image.rotateflip.aspx
下面是一些示例代码(不是我写的-发现前一段时间在这里 ),对我的工作,只要你编辑的一些细节。
private Bitmap rotateImage(Bitmap b, float angle)
{
//create a new empty bitmap to hold rotated image
Bitmap returnBitmap = new Bitmap(b.Width, b.Height);
//make a graphics object from the empty bitmap
using (Graphics g = Graphics.FromImage(returnBitmap))
{
//move rotation point to center of image
g.TranslateTransform((float)b.Width / 2, (float)b.Height / 2);
//rotate
g.RotateTransform(angle);
//move image back
g.TranslateTransform(-(float)b.Width / 2, -(float)b.Height / 2);
//draw passed in image onto graphics object
g.DrawImage(b, new Point(0, 0));
}
return returnBitmap;
}
请注意,这可能无法“开箱即用” - 新位图存在一些问题。 旋转时,它可能不适合放在旧位图的矩形中(矩形边界b.Width,B.Height)。
无论如何,这只是为了给你一个想法。 如果您选择这样做,我相信您将能够计算出所有细节。 我发布了我的最终代码,但是我现在还没有它...
我建议这是最好的方法
// get the full path of image url
string path = Server.MapPath(Image1.ImageUrl) ;
// creating image from the image url
System.Drawing.Image i = System.Drawing.Image.FromFile(path);
// rotate Image 90' Degree
i.RotateFlip(RotateFlipType.Rotate90FlipXY);
// save it to its actual path
i.Save(path);
// release Image File
i.Dispose();
// Set Image Control Attribute property to new image(but its old path)
Image1.Attributes.Add("ImageUrl", path);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.