是否可以在 C# 的帮助下调整图像大小?我有一个文件夹,里面有数千张图片。我需要制作一个应用程序来扫描该文件夹中的所有图像并检查它们的大小参数,然后将那些宽度低于 300 像素的图像按比例调整大小,使其宽度等于 300 像素。有可能吗?以及那时的样子。
问问题
805 次
1 回答
3
检查这个线程。它包含调整图像大小的代码示例http://forums.asp.net/t/1038068.aspx 要获取文件夹中所有文件的文件名,您可以使用
var startPath = @"c:\temp";
var imageFileExtensions =
new [] { ".jpg", ".gif", ".jpeg", ".png", ".bmp" };
var pathsToImages =
from filePath in Directory.EnumerateFiles(
startPath,
"*.*",
SearchOption.AllDirectories)
where imageFileExtensions.Contains(Path.GetExtension(filePath))
select filePath;
调整图像大小,您可以使用
public 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;
}
于 2013-01-03T14:28:56.320 回答