2

这是我用于旋转的 Form1 代码及其工作图像,我可以将其旋转 360 度。问题是在旋转图像时会损失很多质量。

我尝试在那里使用一些编解码器,但我不确定如何用我的代码实现它。

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;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace AnimatedGifEditor
{
    public partial class Form1 : Form
    {
        ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
        ImageCodecInfo ici = null; 
        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);
               // pictureBox1.Image = bitmap;
            }

            trackBar1.Maximum = 360;

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

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

            return rotatedImage;
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            foreach (ImageCodecInfo codec in codecs)
            {
                if (codec.MimeType == "image/jpeg")
                    ici = codec;
            }

            EncoderParameters ep = new EncoderParameters();
            ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)100);



            pictureBox1.Image = RotateImage(bitmap, trackBar1.Value);
        }


    }
}

在表格的顶部,我添加了:

ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
ImageCodecInfo ici = null; 

在我添加的滚动事件中:

foreach (ImageCodecInfo codec in codecs)
                {
                    if (codec.MimeType == "image/jpeg")
                        ici = codec;
                }

                EncoderParameters ep = new EncoderParameters();
                ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)100);

但我从不使用这个编码代码。我该怎么做 ?

4

2 回答 2

2

在您的内部,您RotateImage应该将Graphics对象插值模式设置为高质量:

g.InterpolationMode = InterpolationMode.HighQualityBicubic;

与您的问题无关的另一件事是您没有正确处理以前的图像。

你应该开始你的RotateImage方法:

if (rotatedImage != null) rotatedImage.Dispose();
于 2012-08-20T08:50:59.920 回答
0

虚拟代码:

指定不翻转的 180 度顺时针旋转。

public static Image RotateImage(Image image)
        {
            image.RotateFlip(RotateFlipType.Rotate180FlipNone);
            return image;
        }
于 2013-09-27T22:29:03.910 回答