现在的位置: 首页 > 综合 > 正文

图形绘制之四: 改变图片格式

2012年05月09日 ⁄ 综合 ⁄ 共 2903字 ⁄ 字号 评论关闭
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);
                //得到缩放后的图像, 根据pictureBox的大小确定图片的大小 ..
                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)
                {
                    //使用Bitmap类的.Save方法可以存储改变后的图片....
                    //这样的好处是,可以把图片进行任意处理后存储起来....
                    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;
        }
    }
}

抱歉!评论已关闭.