如何在c#中旋转图像?
我在我的网站上显示了一个图像。这是用c写的。我想让我的用户能够点击旋转图像的按钮。这将旋转服务器上的实际图像,以便下次显示时以正确的方式显示如何在c#中旋转图像?,c#,image-processing,C#,Image Processing,我在我的网站上显示了一个图像。这是用c写的。我想让我的用户能够点击旋转图像的按钮。这将旋转服务器上的实际图像,以便下次显示时以正确的方式显示 类似于facebook的图像轮换功能?您真的需要在服务器上轮换图像吗?为什么不将属性与存储旋转值(如90、180、270)的图像一起存储呢。。。并在每次检索图像时应用该属性,并在用户旋转图像时更新/保存属性值 有关如何旋转图像的教程,请参阅,或者通过谷歌搜索,您将发现许多示例公共静态图像旋转图像(图像、大小、浮动角度) //Create Image ele
类似于facebook的图像轮换功能?您真的需要在服务器上轮换图像吗?为什么不将属性与存储旋转值(如90、180、270)的图像一起存储呢。。。并在每次检索图像时应用该属性,并在用户旋转图像时更新/保存属性值 有关如何旋转图像的教程,请参阅,或者通过谷歌搜索,您将发现许多示例
公共静态图像旋转图像(图像、大小、浮动角度)
//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;
{
if(image==null)
{
抛出新的异常(“图像”);
}
如果(尺寸.宽度<1 | |尺寸.高度<1)
{
抛出新ArgumentException(“大小必须大于零”);
}
位图tempImage=新位图(大小.宽度,大小.高度);
使用(Graphics tempGraphics=Graphics.FromImage(tempImage))
{
PointF center=新的PointF((浮点)size.Width/2F,(浮点)size.Height/2F);
tempGraphics.TranslateTransform(center.X,center.Y,MatrixOrder.Prepend);
tempGraphics.RotateTransform(角度!=180F?角度:182F/*在180的精确角度下,旋转会使图像发生小的移动,我不知道为什么!*/);
tempGraphics.TranslateTransform(-center.X,-center.Y,MatrixOrder.Prepend);
DrawImage(image,new PointF());
}
返回图像;
}
请注意,您不应该在ASP.NET服务器中执行此操作!Windows或ASP.NET服务中不支持System.Drawing.Imaging命名空间中的类。请注意,链接到的教程用于在客户端上旋转。不要在ASP.NET服务器代码中这样做!你的问题是什么?0)旋转数学1)您选择的API,即您丢失了手册2)使其在服务器上持久化3)您的图像是C#的事实?
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;
}