在C#中调整JPEG图像的大小会降低其分辨率

时间:2012-01-18 07:22:08

标签: c# .net image-processing jpeg

我有什么:

   A JPEG image with 96dpi, size: 540 X 700

我想要的:        JPEG图像,300dpi,大小:771 X 1000

问题:        当我首先调整图像大小然后尝试更改分辨率时,通过以下代码它不起作用

       /// <summary>
    /// Changes the resolution of the image
    /// </summary>
    /// <param name="imgPath">Image Path</param>
    /// <param name="xResolution">x Resolution</param>
    /// <param name="yResolution">y Resolution</param>
    /// <returns>Modified Image Path</returns>
    private string ChangeResolution(string imgPath, int xResolution, int yResolution)
    {
        string fullFileName = Path.GetFileNameWithoutExtension(imgPath);
        string extension = Path.GetExtension(imgPath);
        string tmpFileSavedPath = outputDir + "\\" + fullFileName + "_." + extension + "_tmp";

        Image original = Bitmap.FromFile(imgPath);
        original.Save(tmpFileSavedPath);

        Bitmap bmSmall = new Bitmap(tmpFileSavedPath);
        bmSmall.SetResolution(xResolution, yResolution);
        string modifiedOverLayImagePath = tmpFileSavedPath.TrimEnd("_tmp".ToCharArray());
        bmSmall.Save(modifiedOverLayImagePath);
        bmSmall.Dispose();

        //Deleting temp file
        System.IO.File.Delete(tmpFileSavedPath);
        return modifiedOverLayImagePath;
    }

意味着它对图像没有任何作用,分辨率保持不变,如果我走另一条路,即先改变分辨率然后改变大小,令人惊讶的是大小会改变,但分辨率会降低到96dpi。

以下是调整大小代码:

public static System.Drawing.Bitmap ResizeImage(System.Drawing.Image image, int width, int height)
    {
        //a holder for the result
        Bitmap result = new Bitmap(width, height);

        //use a graphics object to draw the resized image into the bitmap
        using (Graphics graphics = Graphics.FromImage(result))
        {
            //set the resize quality modes to high quality
            graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            //draw the image into the target bitmap
            graphics.DrawImage(image, 0, 0, result.Width, result.Height);
        }

        //return the resulting bitmap
        return result;
    }

任何人都可以帮助我,我想知道771 X 1000是否支持300dpi分辨率,但是当我在photoshop中这样做时它完美无缺,谢谢

以下是我的主要功能,我首先更改分辨率并随后调整大小:

string imgPath = @"D:\abc\background.jpg";

        string newResPath = ChangeResolution(imgPath, 300, 300);

        Image oldImage = Bitmap.FromFile(newResPath);
        //Image newImage = ImageResize.ConstrainProportions(oldImage, 771, ImageResize.Dimensions.Width);
        Image newImage = ImageUtilities.ResizeImage(oldImage, 771, 1000);
        string savedPath = "D:\\abc\\saved.jpg";

        try
        {
            newImage.Save(savedPath, ImageFormat.Jpeg);
        }
        catch
        {
            MessageBox.Show("error");
        }
        newImage.Dispose();

3 个答案:

答案 0 :(得分:2)

private static Bitmap _resize(Image image, int width, int height)
{
    Bitmap newImage = new Bitmap(width, height);
    //this is what allows the quality to stay the same when reducing image dimensions
    newImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
    using (Graphics gr = Graphics.FromImage(newImage))
    {
        gr.SmoothingMode = SmoothingMode.HighQuality;
        gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
        gr.PixelOffsetMode = PixelOffsetMode.HighQuality;
        gr.DrawImage(image, new Rectangle(0, 0, width, height));
    }
    return newImage;
}

答案 1 :(得分:1)

JPEG实际分辨率是一定宽度的高度像素 - 这才是真正重要的。因此,对于更新的解决方案,真正重要的一步是ImageUtilities.ResizeImage

DPI分辨率字段仅供参考,它提供了一种原始像素最大位置的提示。因此,在调整像素大小后,您需要将DPI字段更新为您认为应该的任何值。

答案 2 :(得分:0)

简而言之,在这种情况下,DPI并不重要

很好地解释了this answer

DPI代表Dots Per Inch。关于96dpi重置的一个线索来自维基百科:

  自从20世纪80年代开始编写许多Windows软件程序以假设屏幕提供96 PPI

实际上,您的屏幕将以其分辨率显示图像,DPI(或PPI,每英寸像素数)由您的屏幕尺寸决定。 DPI在打印图像时才真正发挥作用。

取决于您在DPI上打印的设备会有所不同。