c# - 修改时保留图像数据的最佳方法是什么?

标签 c# image

在我的项目中,我必须调整图像大小,然后将其保存到文件夹中。我已经发布了很多问题来找出什么方法可以调整图像大小而不破坏图像,但我仍然找不到最好的方法..

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

调整大小方法:

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

结果:

  • The first saving of the image: fileSize: 10721KB

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

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

图片:

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

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

最佳答案

这里的问题是您正在使用 JPEG 压缩保存图像。虽然 JPEG 确实有 lossless compression这是一种完全不同的算法,大多数编码器不支持它。尝试使用无损图像格式,例如 PNG。

来自Lossless compression wikipedia :

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,但您需要指定更高的质量值,以便在保存后保留图像中存储的更多信息.

正如 slawekwin 在评论中提到的,请查看以下关于 setting compression levels 的文章。 尝试使用此代码将质量设置为 100%,从而接收质量更好的图像(但请注意,这仍然不是无损的):

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

关于c# - 修改时保留图像数据的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39694263/

相关文章:

c# - 如何使用 LINQ 在 C# 中以 Datetime 格式转换 SQL 中的时间戳数据类型值?

c# - 如果 IHostedService.StopAsync 方法的实现不响应取消请求,会发生什么情况?

mysql - 图片使用Postgres和Xojo

javascript - js代码在应用程序的其他区域使用/加载之前动态设置图像的宽度和高度

c# - VS Code 是否支持 ASPX 文件中的服务器端代码?

c# - C# 中的随机数猜谜游戏出现问题

image - 如何将 HttpPostedFileBase 转换为图像?

php - 如何在 Laravel 中将图片路径保存到数据库中?

image - 将图像数组 reshape 为列向量,对每个图像逐行排序

c# - "Cannot Invoke this function because the current host does not implement it"