精品秘无码一区二区三区老师-精品秘一区二三区免费雷安-精品蜜桃秘一区二区三区-精品蜜桃秘一区二区三区粉嫩-精品蜜桃一区二区三区-精品蜜臀国产aⅴ一区二区三区

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

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

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

以下是在 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

主站蜘蛛池模板: 真实强奷在线中文 | 国产成熟妇人高潮A片 | 国产无矿砖码砖专区2025 | 免费无码av片在线观看播放 | 免费无码午夜福利片69 | 国产美女精品在线观看 | 亚洲国产精品一区二区高清无码 | 亚洲精品无码不卡在线播放he | 无码人妻久久一区二区 | 午夜福利无码国产精品中文字 | 亚洲性色av私人影院无码 | 最新激情网站 | 成人无码区免费a∨直播 | 国产成人久久精品二三区无码 | 亚洲精品欧美二区三区 | 亚洲AV成人无码久久精品A片 | 久久亚洲小电影一区二区 | 亚洲国产成人久久综合碰 | 久久婷婷人人澡人人爽人人爱 | a级国产乱理伦片在线播放 a级国产乱理伦片在线观看 | s级爆乳玩具酱国产vip皮裤 | 国产精品日韩欧美制服 | 91精品国产综合久久婷婷 | 午夜精品一区二区三区免费视频 | 国产另类69xxxx末成年亚洲成人 | 国产女人水真多18毛片18精品 | 国产日韩欧美一区二区久久精品 | 人妻聚色窝窝人体www一区 | 亚洲精品久久国产高清 | 女性女同性aⅴ免费观女性恋 | 日本aⅴ在线观看 | 久久九九久精品国产免费直播 | 日韩免费无码视频一区二区三 | 欧美成人精品一区二区综合 | 在线无码天堂视频 | 日本中文字幕巨大的乳专区 | 国产日韩一区二区三区视频免费 | 日本高清在线播放一区二区三区 | 一区二区三区高清网 | 中文无码有码亚洲欧美 | 日韩av免费精品一区二区 |