我正在使用bitmap.GetThumbnailImage()
调整图像大小,但我似乎失去了图像质量/分辨率。例如,原始图像的分辨率为300dpi,而调整大小的1则小得多。减少图像尺寸,但保持最高质量/分辨率?
如何在调整大小时保留图像分辨率?
我正在使用bitmap.GetThumbnailImage()
调整图像大小,但我似乎失去了图像质量/分辨率。例如,原始图像的分辨率为300dpi,而调整大小的1则小得多。减少图像尺寸,但保持最高质量/分辨率?
如何在调整大小时保留图像分辨率?
下面是我用它来降低我的图像的功能的图像完成这一点。质量很好。
private static Image ResizeImage(Image imgToResize, Size size)
{
int sourceWidth = imgToResize.Width;
int sourceHeight = imgToResize.Height;
float nPercent = 0;
float nPercentW = 0;
float nPercentH = 0;
nPercentW = ((float)size.Width/(float)sourceWidth);
nPercentH = ((float)size.Height/(float)sourceHeight);
if (nPercentH < nPercentW)
nPercent = nPercentH;
else
nPercent = nPercentW;
int destWidth = (int)(sourceWidth * nPercent);
int destHeight = (int)(sourceHeight * nPercent);
Bitmap b = new Bitmap(destWidth, destHeight);
Graphics g = Graphics.FromImage((Image)b);
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
g.Dispose();
return (Image)b;
}
下面是我如何使用它:
int length = (int)stream.Length;
byte[] tempImage = new byte[length];
stream.Read(tempImage, 0, length);
var image = new Bitmap(stream);
var resizedImage = ResizeImage(image, new Size(300, 300));
霍勒如果你需要帮助,得到它运行。
在设置InterpolationMode(MSDN链接)
你也应该看看这个链接看看:Create High Quality Thumbnail - Resize Image Dynamically
从本质上讲,你的代码看起来类似于以下内容: 位图imageToScale =新的位图(//你想减少
Bitmap bitmap = new Bitmap(imgWidth, imgHeight);
using (Graphics graphics = Graphics.FromImage(result))
{
graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
graphics.DrawImageimageToScale, 0, 0, result.Width, result.Height);
bitmap.Save(memoryStreamNew, System.Drawing.Imaging.ImageFormat.Png);
}
bitmap.Save(//finish this depending on if you want to save to a file location, stream, etc...