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

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

主站蜘蛛池模板: 国产在线看片免费视频 | 国产97碰免费视频 | 亚洲a∨午| 好吊妞无缓冲不卡在线视频 | 色色www | 午夜性刺激 | 一本到加勒比东 | 国产性爱 | 国产最新精品盗摄视频 | 99国产精品9| 99精品视频免费在线观看 | 国产专区 | 国产香蕉一区二区在线网站 | 国精产品一区一区三区有 | 欧美日韩一区二区在线 | 精品乱码一区二区三四区视频 | 欧美超高清xxxhd | 国产一区视频在线观看免费 | 欧美激情αv一区二区三区 国语在线看免 | 天美传媒 | 日韩精品真人荷官 | 欧美xx在线 | 亚洲欧美香蕉在线日韩精选 | 伊人影院视频 | 欧美日韩在线播放成人 | 日本不卡一区二区三区在线 | 最新电视剧免费在线观看 | 日本va在线视频播放 | 国产精品一区二区公司 | 在线精品 | 亚洲欧美中文字幕在线播放 | 日韩欧美国产偷亚洲清高 | 国产拍偷精品网最新在线观 | 日韩一区在线观看免费观看免费 | 欧美高清中文字幕综合网 | 91日本在线精品高清观看 | 高清有码国产一区二区 | 亚洲人成在线观看网站播放 | 国产高颜值大学生情侣酒店 | 日本中文字幕专区视频在线 | 了解最新38在线信息 |