2010-12-17 15:09:22Chris C.S Huang

[C#] 儲存影像為Jpeg, BMP, PNG檔

 將影像儲存為JPEG檔案的函式:

private void saveJpeg(string path, Bitamp img, long quality);

存為BMP檔案的程式碼:

  Image image = Image.FromFile(openFilePath);
  image.Save(saveFilePath, ImageFormat.Bmp);

  or

  myBitmap = new Bitmap(openFilePath);

  myBitmap.Save(saveFilePath, ImageFormat.Bmp);

存為PNG檔案的程式碼

    myBitmap = new Bitmap(openFilePath);

    myBitmap.Save(saveFilePath, ImageFormat.Png);

程式範例如下(存JPEG):



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.IO;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Bitmap myBitmap = null;
        private void btnOpenImg_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "BMP File| *.bmp";
            openFileDialog1.Title = "儲存影像檔";
            openFileDialog1.FilterIndex = 3;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                myBitmap = new Bitmap(openFileDialog1.FileName);

                pictureBox1.Image = (Image) myBitmap;

              }
        }
        private void btnSaveImg_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            string curDir;
            curDir = Directory.GetCurrentDirectory();
            saveFileDialog1.InitialDirectory = curDir;

            saveFileDialog1.Filter = "JPG File|*.jpg";
            saveFileDialog1.Title = "儲存影像檔";
            saveFileDialog1.FilterIndex = 3;
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
        
                saveJpeg(saveFileDialog1.FileName, myBitmap, 85);
            }
       
        }
        private void saveJpeg(string path, Bitmap img, long quality)
        {
            // Encoder parameter for image quality
            EncoderParameter qualityParam = new EncoderParameter(Encoder.Quality, quality);

            // Jpeg image codec
            ImageCodecInfo jpegCodec = this.getEncoderInfo("image/jpeg");
            if (jpegCodec == null)
                return;

            EncoderParameters encoderParams = new EncoderParameters(1);
            encoderParams.Param[0] = qualityParam;

            img.Save(path, jpegCodec, encoderParams);
        }

        private ImageCodecInfo getEncoderInfo(string mimeType)
        {
            // Get image codecs for all image formats
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();

            // Find the correct image codec
            for (int i = 0; i < codecs.Length; i++)
                if (codecs[i].MimeType == mimeType)
                    return codecs[i];
            return null;
        }
    }    
}

參考資料:

C# Tutorial - Image Editing: Saving, Cropping, and Resizing

Compress Image With C# & System.Drawing.Imaging

 

威爾剛 2019-12-20 11:53:02

感謝分享!

http://www.yyj.tw/

日本藤素 2019-01-16 14:31:33

不錯的分享 http://heijin.hi99.cc/