C# 图像旋转

发布于 2024-08-03 22:01:23 字数 109 浏览 3 评论 0原文

在asp.net中旋转图像的最佳方法是什么

我确实使用了matrix.rotateAt,但我无法让它工作,所以请告诉我最好的方法是什么?

我应该写出讨厌用图像对象旋转图像。

What is the best way to rotate a image in asp.net

I did use matrix.rotateAt but i can't get it to work so please tell me what is the best way?

I should write out that hate to rotate a image with the image object.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

z祗昰~ 2024-08-10 22:01:23
Image myImage = Image.FromFile("myimage.png");
myImage.RotateFlip(RotateFlipType.Rotate180FlipNone);

http://msdn.microsoft.com/en-us /library/system.drawing.image.rotateflip.aspx

Image myImage = Image.FromFile("myimage.png");
myImage.RotateFlip(RotateFlipType.Rotate180FlipNone);

http://msdn.microsoft.com/en-us/library/system.drawing.image.rotateflip.aspx

尴尬癌患者 2024-08-10 22:01:23

这是一些示例代码(不是我写的 - 前段时间发现的 这里 )这对我有用,只要你编辑一些细节。

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)。

无论如何,这只是为了给您一个想法。如果您选择这样做,我相信您能够解决所有细节。我会发布我的最终代码,但是我现在没有它......

Here is some sample code (not written by me - found some time ago here ) that worked for me, as long as you edit some details.

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;
} 

Please note, that this may not work "out of the box" - there are some issues with the new bitmap. When you rotate it, it may not fit comfortably in the rectangle of the old bitmap (rectangle bounds b.Width, B.Height).

Anyway this is just to give you an idea. If you choose to do it this way, I'm sure you will be able to work out all the details. I'd post my final code, however I don't have it on me right now...

薄荷→糖丶微凉 2024-08-10 22:01:23

我建议这是最好的方法

    // 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);

了解更多

I would suggest this is the best way

    // 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);

for more

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文