以下是在 ASP.NET(aspx)中獲取指定目錄下的所有圖片并壓縮到指定大小的示例代碼:
一、使用 System.Drawing 和 System.IO 命名空間實現(xiàn)
1. 在頁面的后臺代碼中添加以下方法:
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
public void CompressImagesInDirectory(string sourceDirectory, string destinationDirectory, int maxWidth, int maxHeight)
{
if (!Directory.Exists(destinationDirectory))
{
Directory.CreateDirectory(destinationDirectory);
}
string[] imageFiles = Directory.GetFiles(sourceDirectory, "*.jpg");
string[] pngImageFiles = Directory.GetFiles(sourceDirectory, "*.png");
string[] allImageFiles = imageFiles.Concat(pngImageFiles).ToArray();
foreach (string imagePath in allImageFiles)
{
using (Image image = Image.FromFile(imagePath))
{
int newWidth = image.Width;
int newHeight = image.Height;
if (image.Width > maxWidth || image.Height > maxHeight)
{
double ratioX = (double)maxWidth / image.Width;
double ratioY = (double)maxHeight / image.Height;
double ratio = Math.Min(ratioX, ratioY);
newWidth = (int)(image.Width * ratio);
newHeight = (int)(image.Height * ratio);
}
using (Bitmap resizedImage = new Bitmap(newWidth, newHeight))
{
using (Graphics graphics = Graphics.FromImage(resizedImage))
{
graphics.DrawImage(image, 0, 0, newWidth, newHeight);
}
string fileName = Path.GetFileName(imagePath);
string destinationPath = Path.Combine(destinationDirectory, fileName);
ImageFormat format = GetImageFormat(imagePath);
resizedImage.Save(destinationPath, format);
}
}
}
}
private ImageFormat GetImageFormat(string imagePath)
{
string extension = Path.GetExtension(imagePath).ToLower();
if (extension == ".jpg" || extension == ".jpeg")
{
return ImageFormat.Jpeg;
}
else if (extension == ".png")
{
return ImageFormat.Png;
}
else
{
return ImageFormat.Jpeg;
}
}
2. 然后在按鈕的點擊事件或其他觸發(fā)事件中調(diào)用這個方法:
protected void CompressButton_Click(object sender, EventArgs e)
{
string sourceDirectory = @"C:\YourSourceDirectory"; // 指定源目錄
string destinationDirectory = @"C:\YourDestinationDirectory"; // 指定目標(biāo)目錄
int maxWidth = 800; // 設(shè)置最大寬度
int maxHeight = 600; // 設(shè)置最大高度
CompressImagesInDirectory(sourceDirectory, destinationDirectory, maxWidth, maxHeight);
}
請確保將源目錄和目標(biāo)目錄替換為實際的路徑,并根據(jù)需要調(diào)整最大寬度和高度。這個方法將遍歷指定目錄中的所有 JPEG 和 PNG 圖像文件,將它們壓縮到指定的最大尺寸,并保存到目標(biāo)目錄中。
以下是在上述代碼中添加設(shè)置壓縮質(zhì)量參數(shù)的方法:
修改 CompressImagesInDirectory 方法如下:
public void CompressImagesInDirectory(string sourceDirectory, string destinationDirectory, int maxWidth, int maxHeight, int jpegQuality)
{
if (!Directory.Exists(destinationDirectory))
{
Directory.CreateDirectory(destinationDirectory);
}
string[] imageFiles = Directory.GetFiles(sourceDirectory, "*.jpg");
string[] pngImageFiles = Directory.GetFiles(sourceDirectory, "*.png");
string[] allImageFiles = imageFiles.Concat(pngImageFiles).ToArray();
foreach (string imagePath in allImageFiles)
{
using (Image image = Image.FromFile(imagePath))
{
int newWidth = image.Width;
int newHeight = image.Height;
if (image.Width > maxWidth || image.Height > maxHeight)
{
double ratioX = (double)maxWidth / image.Width;
double ratioY = (double)maxHeight / image.Height;
double ratio = Math.Min(ratioX, ratioY);
newWidth = (int)(image.Width * ratio);
newHeight = (int)(image.Height * ratio);
}
using (Bitmap resizedImage = new Bitmap(newWidth, newHeight))
{
using (Graphics graphics = Graphics.FromImage(resizedImage))
{
graphics.DrawImage(image, 0, 0, newWidth, newHeight);
}
string fileName = Path.GetFileName(imagePath);
string destinationPath = Path.Combine(destinationDirectory, fileName);
ImageFormat format = GetImageFormat(imagePath);
if (format == ImageFormat.Jpeg)
{
// 使用 JPEG 編碼器并設(shè)置質(zhì)量參數(shù)
EncoderParameters encoderParams = new EncoderParameters(1);
encoderParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, jpegQuality);
resizedImage.Save(destinationPath, GetJpegCodecInfo(), encoderParams);
}
else
{
resizedImage.Save(destinationPath, format);
}
}
}
}
}
private ImageCodecInfo GetJpegCodecInfo()
{
ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
foreach (ImageCodecInfo codec in codecs)
{
if (codec.MimeType == "image/jpeg")
return codec;
}
return null;
}
private ImageFormat GetImageFormat(string imagePath)
{
string extension = Path.GetExtension(imagePath).ToLower();
if (extension == ".jpg" || extension == ".jpeg")
{
return ImageFormat.Jpeg;
}
else if (extension == ".png")
{
return ImageFormat.Png;
}
else
{
return ImageFormat.Jpeg;
}
}
2. 然后在調(diào)用該方法時傳入壓縮質(zhì)量參數(shù),例如:
protected void CompressButton_Click(object sender, EventArgs e)
{
string sourceDirectory = @"C:\YourSourceDirectory";
string destinationDirectory = @"C:\YourDestinationDirectory";
int maxWidth = 800;
int maxHeight = 600;
int jpegQuality = 80; // 設(shè)置 JPEG 壓縮質(zhì)量為 80,可以根據(jù)需要調(diào)整
CompressImagesInDirectory(sourceDirectory, destinationDirectory, maxWidth, maxHeight, jpegQuality);
}
這樣就可以在壓縮圖像時設(shè)置 JPEG 圖像的壓縮質(zhì)量參數(shù)了。對于 PNG 圖像,目前沒有直接設(shè)置壓縮質(zhì)量的方法,因為 PNG 是無損壓縮格式。
該文章在 2025/1/2 9:54:31 編輯過