我的现有情况:
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();