[英]Trying to resize and compress height only maintaining aspect ratio with good quality
在這里,我正在處理從文件加載的圖像,並覆蓋將小寫名稱轉換為大寫形式的目標圖像,但是這里使用固定大小調整其大小,我需要使用260像素的高度進行調整,並保持其寬高比不設置圖像寬度
//--------------------------------------------------------------------------------//
private void ProcessImage()
{
if (File.Exists(pictureBox1.ImageLocation))
{
string SourceImagePath = pictureBox1.ImageLocation;
string ImageName = Path.GetFileName(SourceImagePath).ToUpper();
string TargetImagePath = Properties.Settings.Default.ImageTargetDirectory + "\\" + ImageName;
//Set the image to uppercase and save as uppercase
if (SourceImagePath.ToUpper() != TargetImagePath.ToUpper())
{
using (Image Temp = Image.FromFile(SourceImagePath))
{
// my problem is here, i need to resize only by height
// and maintain aspect ratio
Bitmap ResizedBitmap = resizeImage(Temp, new Size(175, 260));
//ResizedBitmap.Save(@TargetImagePath);
ResizedBitmap.Save(@TargetImagePath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
pictureBox1.ImageLocation = @TargetImagePath;
File.Delete(SourceImagePath);
}
}
}
//--------------------------------------------------------------------------------//
private static Bitmap 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 b;
}
//--------------------------------------------------------------------------------//
您的意思是只想將圖像調整為指定的高度,並保持寬度成比例?
/// <summary>
/// Resize the image to have the selected height, keeping the width proportionate.
/// </summary>
/// <param name="imgToResize"></param>
/// <param name="newHeight"></param>
/// <returns></returns>
private static Bitmap resizeImage(Image imgToResize, int newHeight)
{
int sourceWidth = imgToResize.Width;
int sourceHeight = imgToResize.Height;
float nPercentH = ((float)newHeight / (float)sourceHeight);
int destWidth = Math.Max((int)Math.Round(sourceWidth * nPercentH), 1); // Just in case;
int destHeight = newHeight;
Bitmap b = new Bitmap(destWidth, destHeight);
using (Graphics g = Graphics.FromImage((Image)b))
{
g.SmoothingMode = SmoothingMode.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
g.DrawImage(imgToResize, 0, 0, destWidth, destHeight);
}
return b;
}
注意-而不是顯式地處理Graphics g
,請使用using
語句,因為這樣可以保證在發生異常時進行處理。
更新我建議將ResizedBitmap
包裝在調用方法中的using
語句中。
更新2我無法找到一種方法來保存具有指定最大尺寸的JPG。 您可以做的就是以指定的質量保存它,如果文件太大,請以較低的質量重試:
public static void SaveJpegWithSpecifiedQuality(this Image image, string filename, int quality)
{
// http://msdn.microsoft.com/en-us/library/ms533844%28v=vs.85%29.aspx
// A quality level of 0 corresponds to the greatest compression, and a quality level of 100 corresponds to the least compression.
if (quality < 0 || quality > 100)
{
throw new ArgumentOutOfRangeException("quality");
}
System.Drawing.Imaging.Encoder qualityEncoder = System.Drawing.Imaging.Encoder.Quality;
EncoderParameters encoderParams = new EncoderParameters(1);
EncoderParameter encoderParam = new EncoderParameter(qualityEncoder, (long)quality);
encoderParams.Param[0] = encoderParam;
image.Save(filename, GetEncoder(ImageFormat.Jpeg), encoderParams);
}
private static ImageCodecInfo GetEncoder(ImageFormat format)
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.FormatID == format.Guid)
{
return codec;
}
}
return null;
}
請注意, 調整大小算法的質量與保存時文件壓縮的質量不同 。 兩種操作都是有損的 。 我建議您使用最佳質量算法調整大小(更新如上所示),然后嘗試保存質量,直到找到可行的算法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.