首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> C# >> C# 入門

用 C# 製作圖片格式轉換器:ImageFormat 類別完整應用

     編輯:C# 入門

C# 影像格式轉換器:System.Drawing.Imaging 核心應用

.NET Framework 內建支援主流圖片格式雙向轉換,透過 ImageFormat 列舉與 Bitmap.Save 方法即可輕鬆實現。

支援格式一覽

格式副檔名ImageFormat 成員
BMP.bmpImageFormat.Bmp
GIF.gifImageFormat.Gif
Icon.icoImageFormat.Icon
JPEG.jpg, .jpegImageFormat.Jpeg
PNG.pngImageFormat.Png
TIFF.tifImageFormat.Tiff
WMF.wmfImageFormat.Wmf

開發環境需求

  • Windows 2000 Server / XP 以上
  • .NET Framework SDK
  • Visual Studio .NET

介面設計

  • PictureBox (m_pictureBox):顯示圖片,BorderStyle=FixedSingle
  • ComboBox (m_cmbOpen/m_cmbSaveAs):來源/目標格式選擇,Items 加入 *.bmp *.jpg *.gif *.tif
  • Button (m_btnOpen/m_btnSaveAs):開啟/轉換儲存

核心程式碼

開啟圖片

private void m_btnOpen_Click(object sender, EventArgs e) {
    OpenFileDialog ofd = new OpenFileDialog();
    ofd.Filter = m_cmbOpen.Text + "|" + m_cmbOpen.Text;
    ofd.InitialDirectory = Environment.CurrentDirectory;
    ofd.Title = "開啟圖片檔案";
    
    if (ofd.ShowDialog() == DialogResult.OK) {
        string fileName = ofd.FileName;
        m_bitmap = new Bitmap(fileName);
        
        // 依比例縮放 PictureBox
        if (m_bitmap.Width > m_bitmap.Height) {
            m_pictureBox.Width = m_width0;
            m_pictureBox.Height = (int)((double)m_bitmap.Height * m_width0 / m_bitmap.Width);
        } else {
            m_pictureBox.Height = m_height0;
            m_pictureBox.Width = (int)((double)m_bitmap.Width * m_height0 / m_bitmap.Height);
        }
        
        m_pictureBox.Image = m_bitmap;
        this.Text = "Image Converter: " + fileName;
        m_btnSaveAs.Enabled = true;
    }
}

轉換儲存

private void m_btnSaveAs_Click(object sender, EventArgs e) {
    SaveFileDialog sfd = new SaveFileDialog();
    sfd.Title = "轉換儲存為";
    sfd.OverwritePrompt = true;
    sfd.CheckPathExists = true;
    sfd.Filter = m_cmbSaveAs.Text + "|" + m_cmbSaveAs.Text;
    
    if (sfd.ShowDialog() == DialogResult.OK) {
        string fileName = sfd.FileName;
        
        switch (m_cmbSaveAs.Text) {
            case "*.bmp":
                m_bitmap.Save(fileName, ImageFormat.Bmp);
                break;
            case "*.jpg":
                m_bitmap.Save(fileName, ImageFormat.Jpeg);
                break;
            case "*.gif":
                m_bitmap.Save(fileName, ImageFormat.Gif);
                break;
            case "*.tif":
                m_bitmap.Save(fileName, ImageFormat.Tiff);
                break;
        }
        
        this.Text = "Image Converter: " + fileName;
    }
}

執行畫面

圖片格式轉換器執行畫面

擴充功能建議

  • 批次轉換:Directory.GetFiles 列舉資料夾,迴圈處理
  • 品質控制:JPEG EncoderParameters 設定壓縮品質
  • 保留中繼資料:EXIF 資訊複製
  • 多執行緒:Parallel.ForEach 加速批次處理
  • 格式偵測:Image.FromFile 自動偵測格式,不需 ComboBox 手動選擇

關鍵類別總結

類別命名空間用途
BitmapSystem.Drawing點陣圖操作、載入、儲存
ImageFormatSystem.Drawing.Imaging指定儲存格式
OpenFileDialogSystem.Windows.Forms開啟檔案對話框
SaveFileDialogSystem.Windows.Forms儲存檔案對話框
PictureBoxSystem.Windows.Forms圖片顯示控制項
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved