我有什麼:調整大小在C#中的JPEG圖像降低其
A JPEG image with 96dpi, size: 540 X 700
我想: 具有300dpi的JPEG圖像,尺寸:771×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();
在重繪它確認DPI也是重要的輸入/輸出寬度和高度,因此,如果你想畫一個位圖在另一個具有原點高度和寬度的位圖上重新創建位圖,但分辨率不同,因此起始位圖不適合新容器內部......儘管理論上分辨率只應該影響DPI – deadManN 2017-12-05 10:11:02