本例中的檔案型別為 jpg
private void beginOperationToolStripMenuItem_Click(object sender, EventArgs e)
{
if (files.Length > 0)
{
backgroundWorker1.RunWorkerAsync();
}
}
private static Bitmap ResizeImage(String filename, int maxWidth, int maxHeight)
{
using (Image originalImage = Image.FromFile(filename))
{
//Caluate new Size
int newWidth = originalImage.Width;
int newHeight = originalImage.Height;
double aspectRatio = (double)originalImage.Width / (double)originalImage.Height;
if (aspectRatio <= 1 && originalImage.Width > maxWidth)
{
newWidth = maxWidth;
newHeight = (int)Math.Round(newWidth / aspectRatio);
}
else if (aspectRatio > 1 && originalImage.Height > maxHeight)
{
newHeight = maxHeight;
newWidth = (int)Math.Round(newHeight * aspectRatio);
}
if (newWidth >= 0 && newHeight >= 0)
{
Bitmap newImage = new Bitmap(newWidth, newHeight);
using (Graphics g = Graphics.FromImage(newImage))
{
//--Quality Settings Adjust to fit your application
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.DrawImage(originalImage, 0, 0, newImage.Width, newImage.Height);
return newImage;
}
}
return null;
}
}
然后
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
try
{
BackgroundWorker worker = sender as BackgroundWorker;
int counter = 0;
int percentage = 0;
foreach (string file in files)
{
Bitmap bmp1 = new Bitmap(ResizeImage(file, 512, 512));
string resizedfilename = Path.Combine(directoryPath, Path.GetFileNameWithoutExtension(file) "_resized.jpg");
bmp1.Save(resizedfilename);
bmp1.Dispose();
counter ;
percentage = counter * 100 / files.Length;
worker.ReportProgress(percentage);
}
}
catch(Exception err)
{
}
}
硬碟上原圖jpg大小為寬536高589
硬碟上的resize圖片是寬512高536
為什么調整后的影像高度是 536 ?
為什么調整后的圖片在硬碟上的大小是600 KB,而原始圖片大小只有130 KB?
uj5u.com熱心網友回復:
我認為您的縱橫比小于和大于向后。如果影像是“橫向”(寬度 > 高度),則縱橫比將為正(因為您正在執行width/ height)。如果它是“縱向”,它將是負面的。但你有相反的方式。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/333936.html
上一篇:在excel程式中尋找搜索
