成像大小调整会导致C#中的粒状输出

时间:2012-09-01 07:29:40

标签: c#

我正在尝试调整通过MVC3应用上传的图片的大小。下面的代码可以根据指定的参数根据目标尺寸调整图像大小,但最终结果是粒状图像。我在stackoverflow和msn示例中尝试了很多不同的解决方案但是所有内容都返回了一个颗粒状的图像。

static public Image ResizeFile(HttpPostedFileBase file, int targeWidth, int targetHeight)
    {
        Image originalImage = null;
        bool IsImage = true;
        Bitmap bitmap = null;

        try
        {
            originalImage = Image.FromStream(file.InputStream, true, true);
        }
        catch
        {
            IsImage = false;
        }

        if (IsImage)
        {
            //accept an image only if it is less than 3mb(max)
            if (file.ContentLength <= 3145728)
            {
                var newImage = new MemoryStream();
                Rectangle origRect = new Rectangle(0, 0, originalImage.Width, originalImage.Height);

                // if targets are null, require scale. for specified sized images eg. Event Image, or Profile photos.
                int newWidth = 0;
                int newHeight = 0;

                //if the width is greater than height
                if (originalImage.Width > originalImage.Height)
                {
                    newWidth = targeWidth;
                    newHeight = targetHeight;
                    bitmap = new Bitmap(newWidth, newHeight);
                }

                //if the size of image is larger than either one of the target size
                else if (originalImage.Width > targeWidth || originalImage.Height > targetHeight)
                {
                    newWidth = targeWidth;
                    newHeight = targetHeight;
                    bitmap = new Bitmap(newWidth, newHeight);
                }
                //reuse old dimensions
                else
                {
                    bitmap = new Bitmap(originalImage.Width, originalImage.Height);
                }

                try
                {
                    using (Graphics g = Graphics.FromImage((Image)bitmap))
                    {
                        g.SmoothingMode = SmoothingMode.AntiAlias;
                        g.PixelOffsetMode = PixelOffsetMode.HighQuality;
                        g.CompositingQuality = CompositingQuality.HighQuality;
                        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        g.DrawImage(originalImage, new Rectangle(0, 0, newWidth, newHeight),
                            0,
                            0,           // upper-left corner of source rectangle
                            originalImage.Width,       // width of source rectangle
                            originalImage.Height,      // height of source rectangle
                            GraphicsUnit.Pixel);
                        bitmap.Save(newImage, ImageFormat.Jpeg);
                    }
                }
                catch (Exception ex)
                {   // error before IDisposable ownership transfer
                    if (bitmap != null)
                        bitmap.Dispose();

                    logger.Info("Domain/Utilities/ImageResizer->ResizeFile: " + ex.ToString());

                    throw new Exception("Error resizing file.");
                }
            }                
        }

        return (Image)bitmap;
    }

更新1 删除了参数和编码器,目前保存在.jpeg中。但仍会产生颗粒状图像。

1 个答案:

答案 0 :(得分:0)

一些建议

1)确保您没有放大图像。

2)尝试使用g.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias代替HighQuality

如果不能解决上述问题,除了上述内容之外,我将删除所有其他选项设置,然后只需使用保存文件;

bitmap.Save(newImage, Imaging.ImageFormat.Jpeg);

我发现上述作品完美无缺。