C# 如何以任意角度旋转图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12024406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 20:06:40  来源:igfitidea点击:

How can i rotate an image by any degree?

c#

提问by Daniel Lip

I have animated gif and im using a class to parse the images(frames) from it. The class is:

我有动画 gif,我使用一个类来解析它的图像(帧)。班级是:

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Collections.Generic;
using System.IO;

public class AnimatedGif
{
    private List<AnimatedGifFrame> mImages = new List<AnimatedGifFrame>();
    public AnimatedGif(string path)
    {
        Image img = Image.FromFile(path);
        int frames = img.GetFrameCount(FrameDimension.Time);
        if (frames <= 1) throw new ArgumentException("Image not animated");
        byte[] times = img.GetPropertyItem(0x5100).Value;
        int frame = 0;
        for (; ; )
        {
            int dur = BitConverter.ToInt32(times, 4 * frame);
            mImages.Add(new AnimatedGifFrame(new Bitmap(img), dur));
            if (++frame >= frames) break;
            img.SelectActiveFrame(FrameDimension.Time, frame);
        }
        img.Dispose();
    }
    public List<AnimatedGifFrame> Images { get { return mImages; } }
}

public class AnimatedGifFrame
{
    private int mDuration;
    private Image mImage;
    internal AnimatedGifFrame(Image img, int duration)
    {
        mImage = img; mDuration = duration;
    }
    public Image Image { get { return mImage; } }
    public int Duration { get { return mDuration; } }
}

Now in form1 i loop over the frames in this case 4 and i want to rotate the animation by any degree. Now its rotating each 45 or 90 degrees. I want to add more frames(images) to the animation so if i set the rotation to 31 or to 10 degrees so i will see the animation rotating in 10 degrees.

现在在form1中,我在这种情况下循环了帧4,并且我想以任何角度旋转动画。现在它每旋转 45 或 90 度。我想向动画添加更多帧(图像),所以如果我将旋转设置为 31 或 10 度,那么我将看到动画旋转 10 度。

This is the code in Form1 wich is not working good. Im using a function for the rotation wich i didnt test yet if its any working.

这是 Form1 中的代码,效果不佳。我使用了一个旋转函数,我还没有测试它是否有效。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AnimatedGifEditor
{
    public partial class Form1 : Form
    {
        Image myImage;
        AnimatedGif myGif;
        Bitmap bitmap;

        public Form1()
        {
            InitializeComponent();

            myImage = Image.FromFile(@"D:\fananimation.gif");
            myGif = new AnimatedGif(@"D:\fananimation.gif");
            for (int i = 0; i < myGif.Images.Count; i++)
            {
                pictureBox1.Image = myGif.Images[3].Image;
                bitmap = new Bitmap(pictureBox1.Image);
                rotateImage(bitmap, 76);
                pictureBox1.Image = bitmap;
            }


        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }



private Bitmap RotateImg(Bitmap bmp, float angle, Color bkColor)
    {
        int w = bmp.Width;
        int h = bmp.Height;
        bmp.PixelFormat pf = default(bmp.PixelFormat);
        if (bkColor == Color.Transparent)
        {
            pf = bmp.Format32bppArgb;
        }
        else
        {
            pf = bmp.PixelFormat;
        }

        Bitmap tempImg = new Bitmap(w, h, pf);
        Graphics g = Graphics.FromImage(tempImg);
        g.Clear(bkColor);
        g.DrawImageUnscaled(bmp, 1, 1);
        g.Dispose();

        GraphicsPath path = new GraphicsPath();
        path.AddRectangle(new RectangleF(0f, 0f, w, h));
        Matrix mtrx = new Matrix();
        //Using System.Drawing.Drawing2D.Matrix class 
        mtrx.Rotate(angle);
        RectangleF rct = path.GetBounds(mtrx);
        Bitmap newImg = new Bitmap(Convert.ToInt32(rct.Width), Convert.ToInt32(rct.Height), pf);
        g = Graphics.FromImage(newImg);
        g.Clear(bkColor);
        g.TranslateTransform(-rct.X, -rct.Y);
        g.RotateTransform(angle);
        g.InterpolationMode = InterpolationMode.HighQualityBilinear;
        g.DrawImageUnscaled(tempImg, 0, 0);
        g.Dispose();
        tempImg.Dispose();
        return newImg;
    }
    }
}

The animated gif im using for the test can be found here:

用于测试的动画 gif 可在此处找到:

enter image description here

在此处输入图片说明

采纳答案by Omar

I didn't understand what's your problem but I think that your code could be improved. I think that you don't need to use directly the Matrixclass. There are some functions that does this work for you. Infact the only things you need are: set the point of the rotation as the center, rotate the graphics and draw on it, using some functions by the Graphicsclass. So to rotate an image you can use this simple code:

我不明白你的问题是什么,但我认为你的代码可以改进。我认为您不需要直接使用Matrix该类。有一些功能可以为您完成这项工作。事实上,您唯一需要的是:将旋转点设置为中心,旋转图形并在其上绘制,使用Graphics类的一些函数。因此,要旋转图像,您可以使用以下简单代码:

private Bitmap RotateImage(Bitmap bmp, float angle) {
     Bitmap rotatedImage = new Bitmap(bmp.Width, bmp.Height);
     rotatedImage.SetResolution(bmp.HorizontalResolution, bmp.VerticalResolution);

     using (Graphics g = Graphics.FromImage(rotatedImage)) {
        // Set the rotation point to the center in the matrix
        g.TranslateTransform(bmp.Width / 2, bmp.Height / 2);
        // Rotate
        g.RotateTransform(angle);
        // Restore rotation point in the matrix
        g.TranslateTransform(- bmp.Width / 2, - bmp.Height / 2);
        // Draw the image on the bitmap
        g.DrawImage(bmp, new Point(0, 0));
     }

     return rotatedImage;
}

回答by Rajesh

Have you tried RotateFlip?

你试过 RotateFlip 吗?

public partial class Form1 : Form
{
    Image myImage;
    AnimatedGif myGif;
    Bitmap bitmap;
    public Form1()
    {
        InitializeComponent();
        myImage = Image.FromFile(@"D:\fananimation.gif");
        bitmap = new Bitmap(myImage);
        bitmap.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipNone);
        this.pictureBox1.Image = bitmap;
    }

}

Source

来源

回答by theGD

I was using this function in VB:

我在 VB 中使用这个函数:

    Public Function RotateImage(ByRef image As Image, ByVal angle As Single) As Drawing.Bitmap
    If image Is Nothing Then
        Throw New ArgumentNullException("image")
    End If

    Dim pi2 As Single = Math.PI / 2.0
    Dim oldWidth As Single = image.Width
    Dim oldHeight As Single = image.Height

    Dim theta As Single = angle * Math.PI / 180.0
    Dim locked_theta As Single = theta

    If locked_theta < 0.0 Then locked_theta += 2 * Math.PI

    Dim newWidth, newHeight As Single
    Dim nWidth, nHeight As Integer

    Dim adjacentTop, oppositeTop As Single
    Dim adjacentBottom, oppositeBottom As Single

    If (locked_theta >= 0.0 And locked_theta < pi2) Or _
    (locked_theta >= Math.PI And locked_theta < (Math.PI + pi2)) Then
        adjacentTop = Math.Abs(Math.Cos(locked_theta)) * oldWidth
        oppositeTop = Math.Abs(Math.Sin(locked_theta)) * oldWidth

        adjacentBottom = Math.Abs(Math.Cos(locked_theta)) * oldHeight
        oppositeBottom = Math.Abs(Math.Sin(locked_theta)) * oldHeight
    Else
        adjacentTop = Math.Abs(Math.Sin(locked_theta)) * oldHeight
        oppositeTop = Math.Abs(Math.Cos(locked_theta)) * oldHeight

        adjacentBottom = Math.Abs(Math.Sin(locked_theta)) * oldWidth
        oppositeBottom = Math.Abs(Math.Cos(locked_theta)) * oldWidth
    End If



    newWidth = adjacentTop + oppositeBottom
    newHeight = adjacentBottom + oppositeTop

    nWidth = Int(Math.Ceiling(newWidth))
    nHeight = Int(Math.Ceiling(newHeight))

    Dim rotatedBmp As New Drawing.Bitmap(nWidth, nHeight)

    Dim g As Graphics = Graphics.FromImage(rotatedBmp)

    Dim points(2) As Point

    If (locked_theta >= 0.0 And locked_theta < pi2) Then

        points(0) = New Point(Int(oppositeBottom), 0)
        points(1) = New Point(nWidth, Int(oppositeTop))
        points(2) = New Point(0, Int(adjacentBottom))

    ElseIf locked_theta >= pi2 And locked_theta < Math.PI Then

        points(0) = New Point(nWidth, Int(oppositeTop))
        points(1) = New Point(Int(adjacentTop), nHeight)
        points(2) = New Point(Int(oppositeBottom), 0)

    ElseIf locked_theta >= Math.PI And locked_theta < (Math.PI + pi2) Then

        points(0) = New Point(Int(adjacentTop), nHeight)
        points(1) = New Point(0, Int(adjacentBottom))
        points(2) = New Point(nWidth, Int(oppositeTop))

    Else

        points(0) = New Point(0, Int(adjacentBottom))
        points(1) = New Point(Int(oppositeBottom), 0)
        points(2) = New Point(Int(adjacentTop), nHeight)
    End If

    g.DrawImage(image, points)

    g.Dispose()
    image.Dispose()

    Return rotatedBmp

End Function

回答by Timo Treichel

I tried the answer of @Omar myself and realized, that the original image gets cut at the sides ... i've rewritten it so it resizes the image to the new sizes:

我自己尝试了@Omar 的答案并意识到,原始图像在两侧被剪掉了......我已经重写了它,因此它将图像调整为新尺寸:

private static Bitmap RotateImage(Bitmap bmp, float angle)
{
    float alpha = angle;

    //edit: negative angle +360
    while(alpha <0) alpha +=360;

    float gamma = 90;
    float beta = 180 - angle - gamma;

    float c1 = bmp.Height;
    float a1 = (float)(c1 * Math.Sin(alpha * Math.PI / 180) / Math.Sin(gamma * Math.PI / 180));
    float b1 = (float)(c1 * Math.Sin(beta * Math.PI / 180) / Math.Sin(gamma * Math.PI / 180));

    float c2 = bmp.Width;
    float a2 = (float)(c2 * Math.Sin(alpha * Math.PI / 180) / Math.Sin(gamma * Math.PI / 180));
    float b2 = (float)(c2 * Math.Sin(beta * Math.PI / 180) / Math.Sin(gamma * Math.PI / 180));

    int width = Convert.ToInt32(b2 + a1);
    int height = Convert.ToInt32(b1 + a2);

    Bitmap rotatedImage = new Bitmap(width, height);
    using (Graphics g = Graphics.FromImage(rotatedImage))
    {
        g.TranslateTransform(rotatedImage.Width / 2, rotatedImage.Height / 2); //set the rotation point as the center into the matrix
        g.RotateTransform(angle); //rotate
        g.TranslateTransform(-rotatedImage.Width / 2, -rotatedImage.Height / 2); //restore rotation point into the matrix
        g.DrawImage(bmp, new Point((width - bmp.Width) / 2, (height - bmp.Height) / 2)); //draw the image on the new bitmap
    }
    return rotatedImage;
}

Changes of the Size at 30 Degree Radius

Changes of the Size at 30 Degree Radius

回答by Queequeg

Based on the previous answers I created this code that doesn't cut the image (the other examples were not working for me)

根据之前的答案,我创建了不剪切图像的代码(其他示例对我不起作用)

    private Bitmap RotateImage(Bitmap bmp, float angle)
    {
        float height = bmp.Height;
        float width = bmp.Width;
        int hypotenuse = System.Convert.ToInt32(System.Math.Floor(Math.Sqrt(height * height + width * width)));
        Bitmap rotatedImage = new Bitmap(hypotenuse, hypotenuse);
        using (Graphics g = Graphics.FromImage(rotatedImage))
        {
            g.TranslateTransform((float)rotatedImage.Width / 2, (float)rotatedImage.Height / 2); //set the rotation point as the center into the matrix
            g.RotateTransform(angle); //rotate
            g.TranslateTransform(-(float)rotatedImage.Width / 2, -(float)rotatedImage.Height / 2); //restore rotation point into the matrix
            g.DrawImage(bmp, (hypotenuse - width) / 2, (hypotenuse - height) / 2, width, height);
        }
        return rotatedImage;
    }