No 62・画像ファイルフォーマットの変換


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Ex13_06
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private System.Drawing.Bitmap MyBitmap;
        private void button1_Click(object sender, EventArgs e)
        {
            // 
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = " (JPeg, Gif, Bmp, etc.)|*.jpg;*.jpeg;*.gif;*.bmp;*.tif; *.tiff; *.png| JPeg  (*.jpg;*.jpeg)|*.jpg;*.jpeg |GIF  (*.gif)|*.gif |BMP (*.bmp)|*.bmp|Tiff (*.tif;*.tiff)|*.tif;*.tiff|Png (*.png)| *.png | (*.*)|*.*";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                // 
                Bitmap SrcBitmap = new Bitmap(openFileDialog.FileName);
                // 
                MyBitmap = new Bitmap(SrcBitmap, this.pictureBox1.Width, this.pictureBox1.Height);
                this.pictureBox1.Image = MyBitmap;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // 
            if (MyBitmap == null)
            {
                MessageBox.Show(" !", " ");
                return;
            }
            SaveFileDialog saveDlg = new SaveFileDialog();
            if (saveDlg.ShowDialog() == DialogResult.Cancel)
                return;
            string fileName = saveDlg.FileName;
            try
            {
                if (this.comboBox1 .SelectedIndex ==0)
                {
                    MyBitmap.Save(fileName + ".bmp", System.Drawing.Imaging.ImageFormat.Bmp);
                }
                if (this.comboBox1.SelectedIndex ==1)
                {
                    MyBitmap.Save(fileName + ".jpg", System.Drawing.Imaging.ImageFormat.Gif);
                }
                if (this.comboBox1.SelectedIndex == 2)
                {
                    MyBitmap.Save(fileName + ".png", System.Drawing.Imaging.ImageFormat.Jpeg);
                }
                if (this.comboBox1.SelectedIndex == 3)
                {
                    MyBitmap.Save(fileName + ".gif", System.Drawing.Imaging.ImageFormat.Png);
                }
                if (this.comboBox1.SelectedIndex == 4)
                {
                    MyBitmap.Save(fileName + ".tif", System.Drawing.Imaging.ImageFormat.Tiff);
                }
                if (this.comboBox1.SelectedIndex == 5)
                {
                    MyBitmap.Save(fileName + ".wmf", System.Drawing.Imaging.ImageFormat.Wmf);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, " ");
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.comboBox1.SelectedIndex = 0;
        }
    }
}