日韩欧美国产精品免费一二-日韩欧美国产精品亚洲二区-日韩欧美国产精品专区-日韩欧美国产另-日韩欧美国产免费看-日韩欧美国产免费看清风阁

LOGO OA教程 ERP教程 模切知識交流 PMS教程 CRM教程 開發文檔 其他文檔  
 
網站管理員

利用asp.net獲取指定目錄下的所有圖片,并壓縮到指定大小

admin
2025年1月1日 17:51 本文熱度 787

以下是在 ASP.NET(aspx)中獲取指定目錄下的所有圖片并壓縮到指定大小的示例代碼:

一、使用 System.Drawing 和 System.IO 命名空間實現

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. 然后在按鈕的點擊事件或其他觸發事件中調用這個方法:

protected void CompressButton_Click(object sender, EventArgs e)
{
    string sourceDirectory = @"C:\YourSourceDirectory"; // 指定源目錄
    string destinationDirectory = @"C:\YourDestinationDirectory"; // 指定目標目錄
    int maxWidth = 800; // 設置最大寬度
    int maxHeight = 600; // 設置最大高度
    CompressImagesInDirectory(sourceDirectory, destinationDirectory, maxWidth, maxHeight);
}

請確保將源目錄和目標目錄替換為實際的路徑,并根據需要調整最大寬度和高度。這個方法將遍歷指定目錄中的所有 JPEG 和 PNG 圖像文件,將它們壓縮到指定的最大尺寸,并保存到目標目錄中。

以下是在上述代碼中添加設置壓縮質量參數的方法:

  1. 修改 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 編碼器并設置質量參數
                    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. 然后在調用該方法時傳入壓縮質量參數,例如:

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; // 設置 JPEG 壓縮質量為 80,可以根據需要調整
    CompressImagesInDirectory(sourceDirectory, destinationDirectory, maxWidth, maxHeight, jpegQuality);
}

這樣就可以在壓縮圖像時設置 JPEG 圖像的壓縮質量參數了。對于 PNG 圖像,目前沒有直接設置壓縮質量的方法,因為 PNG 是無損壓縮格式。


該文章在 2025/1/2 9:54:31 編輯過
關鍵字查詢
相關文章
正在查詢...
點晴ERP是一款針對中小制造業的專業生產管理軟件系統,系統成熟度和易用性得到了國內大量中小企業的青睞。
點晴PMS碼頭管理系統主要針對港口碼頭集裝箱與散貨日常運作、調度、堆場、車隊、財務費用、相關報表等業務管理,結合碼頭的業務特點,圍繞調度、堆場作業而開發的。集技術的先進性、管理的有效性于一體,是物流碼頭及其他港口類企業的高效ERP管理信息系統。
點晴WMS倉儲管理系統提供了貨物產品管理,銷售管理,采購管理,倉儲管理,倉庫管理,保質期管理,貨位管理,庫位管理,生產管理,WMS管理系統,標簽打印,條形碼,二維碼管理,批號管理軟件。
點晴免費OA是一款軟件和通用服務都免費,不限功能、不限時間、不限用戶的免費OA協同辦公管理系統。
Copyright 2010-2025 ClickSun All Rights Reserved

主站蜘蛛池模板: 国产观看精品一区二区三区 | 60分钟床色大片在线观看免费 | 欧美激情一区二区三区 | 国产精品视频免费一区二区三区 | 亚洲韩国日本欧美一区二区三区 | 黑人巨大 | 在线观看国产h视频 | 欧美日韩国产日韩 | 日本中文字幕人成中文字幕 | 一区二区三区在线视频不卡 | 爽死七七 | 99re5在线精品视频热线 | 每日更新亚洲成a人v | 成人免费视频软件网站 | 日本国产一区二区三区在线观看 | 亚洲色大成网站www 十年造就经典 | 成人午夜影院网站 | 国产在线视频不 | 亚洲日韩欧美不卡 | 影音先锋人 | 精品日韩视频一区二区三 | 欧美喷潮系列在线观看 | 韩国免费视频一区二区三区 | 永久在线免费观看美女热比网站 | 国产精品自拍视频合集 | 日韩专区视频 | 97视频在线 | 91精品一区二区三区在线播放 | 亚洲精品视频在线播放 | 成人自拍视频免费 | 国产精品福利在线72国 | 2025年国产中文字无 | 亚洲精品自 | 国产在线拍小情侣国产拍拍偷 | 亚洲欧美日韩国产精品 | 日本一区二区在线不卡 | 日韩高清免费视频观看 | 中日韩精品一区二区三区成人 | 成年奭片免费观看 | 精品日韩视频一区二区三 | 国产精品自产在线观看免费 |