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

3

我的现有情况:

   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个回答

3
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
很好的回答,但如果您添加更详细的解释,它将大大改善。 - user1131435
这些质量的东西是什么? - Hassan Faghihi

1

JPEG 的实际分辨率是宽度和高度像素确定的 - 这才是真正重要的。因此,真正重要的步骤是使用 ImageUtilities.ResizeImage 将其调整为新的分辨率。

DPI 分辨率字段仅提供信息,并为原始像素的大小提供了一种提示。因此,在调整像素大小后,您需要将 DPI 字段更新为您认为合适的任何值。


谢谢,你有任何想法如何更新DPI字段吗?因为Bitmap.SetResolution(300, 300);不起作用,我希望这个字段是300,这样当它在Photoshop中打开时显示300dpi,谢谢。 - shabby

0

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

这个答案中已经很好地解释了。

DPI代表每英寸点数。从维基百科可以得到一个线索:96dpi的重置源自80年代以来许多Windows软件程序的默认屏幕提供96 PPI的假设。

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

根据您打印的设备不同,DPI会有所不同。


在重新绘制时,确认 DPI 对于输入/输出的宽度和高度也很重要。因此,如果您想在另一个具有原始高度和宽度的位图上绘制位图,并为此重新创建位图,但分辨率不同,则起始位图将无法适合新容器内...尽管理论上分辨率只应影响 DPI。 - Hassan Faghihi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接