修改时保留图像数据的最佳方法是什么? [英] what is the best way to keep image's data when modifying?

查看:66
本文介绍了修改时保留图像数据的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我必须调整图像大小,然后将其保存到文件夹中.我已经发布了许多问题,以找出哪种方法可以调整图像大小而又不会使图像失真,但是我仍然找不到最佳方法.

In my project, i have to resize the image and then save it to a folder. I have posted many questions to find out what method can resize the image without distory the image, but i still not find the best method..

为了测试该方法,我不尝试调整图像的大小,而是在resize方法中输出图像的100%大小.

For testing the method, I don't try to resize the image but output an 100% size of the image in resize method.

调整大小方法:

    public Image reduce(Image sourceImage, string size)
    {
        //for testing, i want to use the size of the source image
        //double percent = Convert.ToDouble(size) / 100;
        int width = (int)(sourceImage.Width); //sourceImage.Width * percent 
        int height = (int)(sourceImage.Height); //sourceImage.Height *percent 
        var destRect = new Rectangle(0, 0, width, height);
        var destImage = new Bitmap(width, height);

        destImage.SetResolution(sourceImage.HorizontalResolution, sourceImage.VerticalResolution);
        using (var graphics = Graphics.FromImage(destImage))
        {
            graphics.CompositingMode = CompositingMode.SourceCopy;
            graphics.CompositingQuality = CompositingQuality.HighQuality;
            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            graphics.SmoothingMode = SmoothingMode.HighQuality;
            graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
            using (var wrapMode = new ImageAttributes())
            {
                wrapMode.SetWrapMode(WrapMode.TileFlipXY);
                graphics.DrawImage(sourceImage, destRect, 0, 0, sourceImage.Width, sourceImage.Height, GraphicsUnit.Pixel, wrapMode);
            }
        }

        return destImage;
    }

使用:

//the code to get the image is omitted (in my testing, jpg format is fixed, however, other image formats are required)
//to test the size of original image
oImage.Save(Path.Combine(oImagepath), System.Drawing.Imaging.ImageFormat.Jpeg);

Image nImage = resizeClass.reduce(oImage,"100");
nImage .Save(Path.Combine(nImagepath), System.Drawing.Imaging.ImageFormat.Jpeg);

结果:

  • 图像的首次保存:fileSize: 10721KB

图像的第二次保存:fileSize: 4033KB < =应该为10721KB

The second saving of the image: fileSize: 4033KB <= it should be 10721KB

问题是,如果用户通过设置90%-100%传递10MB的图像来接受4 MB的图像,该如何接受?这太荒谬了,所以我不得不重写程序:(

The problem is how can the user accept to receive a 4 MB image if they pass a 10MB image by setting 90% - 100%? It is ridiculous, so i have to rewrite the program :(

图片:

原始: https://1drv.ms/i/s!AsdOBLg50clihVaoEQdj1wQidhdX

调整大小: https://1drv.ms/i/s!AsdOBLg50clihVV96AVKouVkI25o

推荐答案

此处的问题是您正在使用JPEG压缩保存图像.尽管JPEG确实具有无损压缩,但这是一种完全不同的算法,大多数编码器均不支持它.尝试使用无损图像格式,例如PNG.

The issue here is that you are saving the image out with JPEG compression. While JPEG does have a lossless compression it is a completely different algorithm and most encoders do not support it. Try using a lossless image format such as PNG.

来自无损压缩维基百科:

无损压缩是一类数据压缩算法,可以从压缩数据中完美地重建原始数据

Lossless compression is a class of data compression algorithms that allows the original data to be perfectly reconstructed from the compressed data

相比之下,有损压缩仅允许重建原始数据的近似值,尽管这通常会提高压缩率(并因此减小文件大小).

By contrast, lossy compression permits reconstruction only of an approximation of the original data, though this usually improves compression rates (and therefore reduces file sizes).

因此,如果您不介意在保存时丢失某些像素数据,仍可以使用JPEG,但是您需要指定一个较高的Quality值,以便在保存后保留更多存储在图像中的信息

So if you don't mind losing some of the pixel data when you save, you can still use JPEG but you will need to specify a higher Quality value in order to keep more of the information stored in the image after the save.

如slawekwin在他的评论中提到的,请在

As slawekwin mentioned in his comment, check out the following article on setting compression levels. Try using this code to set the quality to 100% thus receiving a much better quality image (but note this is still not lossless):

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

这篇关于修改时保留图像数据的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆