c#图像缩放质量在开发环境和生产环境之间有所不同 [英] c# Image scaling quality different between dev and production environment

查看:89
本文介绍了c#图像缩放质量在开发环境和生产环境之间有所不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个功能,该功能可以调整图片大小和裁剪,然后保存生成的图像文件.在我的开发环境中,它工作得很好,但是在生产中,最终的图像是颗粒状的".您可以在此处 http://test.powersport.it/canc2.aspx

i have created a function that resizes and crops a picture and then saves the resulting image file. In my development environment it works just great, but on production the resulting image is "grainy". You can see the different quality here http://test.powersport.it/canc2.aspx

这是生成调整大小和裁剪图像的代码

Here is the code that generates the resized and cropped image

    // width: width of cropped img - height: height of cropped img
    System.Drawing.Bitmap thumbnail = new Bitmap(width, height);
    // image: original System.Drawing.Image containing full size img
    thumbnail.SetResolution(image.HorizontalResolution, image.VerticalResolution);
    // size[0]: width of resized img - size[1]: height of resized image
    System.Drawing.Image mini = new Bitmap(image, size[0], size[1]);
    System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(thumbnail);

    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
    g.SmoothingMode = SmoothingMode.HighQuality;
    g.PixelOffsetMode = PixelOffsetMode.HighQuality;
    g.CompositingQuality = CompositingQuality.HighQuality;

    g.DrawImage(mini, ((width - size[0]) / 2), ((height - size[1]) / 2), size[0], size[1]);

    EncoderParameters encoderParameters;
    encoderParameters = new EncoderParameters(1);
    ImageCodecInfo[] info = ImageCodecInfo.GetImageEncoders();

    // img: original file name
    switch (Path.GetExtension(img).ToLower())
    {
        case ".png": //       info[4]
            thumbnail.Save(dest, System.Drawing.Imaging.ImageFormat.Png);
            break;
        case ".bmp": //       info[0]
            thumbnail.Save(dest, System.Drawing.Imaging.ImageFormat.Bmp);
            break;
        case ".tiff": //      info[3]
            encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionLZW);
            thumbnail.Save(dest, info[3], encoderParameters);
            break;
        case ".tif": //      info[3]
            encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionLZW);
            thumbnail.Save(dest, info[3], encoderParameters);
            break;
        default: //jpeg      info[1]
            encoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, compression);
            thumbnail.Save(dest, info[1], encoderParameters);
            break;
    }

感谢您的帮助.谢谢.

更新:我已经尝试过PNG,BMP和TIFF,但是它们存在相同的问题

UPDATE: i've tried with PNG, BMP and TIFF but they have the same problem

推荐答案

您假设每台机器在完全相同的索引处都具有完全相同的编码器.

很可能每台计算机实际上都以不同的格式压缩图像,尽管有文件扩展名.

You're assuming that each machine has exactly the same encoders at exactly the same indexes.

It's likely each machine is actually compressing images in a different format, depspite the file extension.

WS2008和WS2008 R2具有不同的图形子系统.在后者中,GDI +只是WIC编码器的包装,WIC编码器具有不同的怪癖.

WS2008 and WS2008 R2 have different graphics subsystems. In the latter, GDI+ is just a wrapper for the WIC encoders, which have a different sent of quirks.

我真的希望您决定使用可为您处理这些差异的库.服务器端映像既不简单也不直观,并且有无数的陷阱清单避免.除了您在这里遇到的编码问题之外,.NET似乎实际上会从 System.Drawing 命名空间中垃圾收集类.不会,除非每个引用都在 using(){} 子句内,否则您将遇到一些严重的稳定性问题.

I really hope you decide to use a library that handles these differences for you. Server-side imaging is neither simple nor intuitive, and has an endless list of pitfalls to avoid. Aside from the encoding issues you're having here, it looks like you're under the impression .NET will actually garbage collect classes from the System.Drawing namespace. It won't, and you're going to have some serious stability issues unless every reference is inside a using(){} clause.

这篇关于c#图像缩放质量在开发环境和生产环境之间有所不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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