如何减少图像旋转质量损失?

时间:2012-08-20 02:39:39

标签: c#

这是我用于旋转的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; 

在scroll事件中我添加了:

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

但我从不使用此编码代码。我该怎么办?

2 个答案:

答案 0 :(得分:2)

RotateImage内你应该将Graphics物体插值模式设置为高质量:

g.InterpolationMode = InterpolationMode.HighQualityBicubic;

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

您应该使用以下内容开始RotateImage方法

if (rotatedImage != null) rotatedImage.Dispose();

答案 1 :(得分:0)

虚拟代码:

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

public static Image RotateImage(Image image)
        {
            image.RotateFlip(RotateFlipType.Rotate180FlipNone);
            return image;
        }