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

标签 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; 

在滚动事件中我添加了:

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

但我从来没有使用过这个编码代码。我该怎么做?

最佳答案

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

g.InterpolationMode = InterpolationMode.HighQualityBicubic;

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

您应该使用以下内容启动您的 RotateImage 方法:

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

关于c# - 如何减少图像旋转质量损失?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12031728/

相关文章:

c# - 在 Windows Phone 8.1 上触摸时如何更改矩形颜色

c# - 简单的 C# Noop 语句

c# - 如何使用反射确定属性类型?

c# - Windows 操作系统中的 Outlook 签名文件夹

c# - 如何在windows服务中发起HTTP请求?

c# - 推断类型的模板和隐式类型转换有什么问题?

c# - PredicateBuilder<True> 和 PredicateBuilder<False> 之间的区别?

c# - 像 List<string> 但保持字符串有序?

c# - Datepicker 在绑定(bind)中使用复选框禁用或启用

c# - 在 C# 中格式化 IP 地址