UQYT

用C#写个图片格式转换程序

由于使用的是C#中的Image类,因此支持的图片格式有

BMP
JPG
PNG
GIF
EMF
EXIF
ICON
TIFF
WMF

整个程序的界面如下:

点击开始转换,进行图片格式的转换:



C#真的是爽啊,写这个程序用了不到三个小时,如果用VC写,我想怎么也不能完成了,当然了,VC高手除外。呵呵,全部程序如下:

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

namespace ImageConverter
{
    public partial class Form1 : Form
    {
        FileInfo[] files=null;

        public Form1()
        {
            InitializeComponent();
        }

        private bool CheckFormat()
        {
            if (cbxPrevFormat.Text.Trim() == "" || cbxLaterFormat.Text.Trim() == "")
            {
                MessageBox.Show("请选择图片格式!", "错误提示");
                return false;
            }

            return true;
        }

        private void ListImage()
        {
            progressBar1.Value = 0;

            DirectoryInfo di = new DirectoryInfo(textBox1.Text.Trim());
            files = di.GetFiles("*."+cbxPrevFormat.Text.Trim());

            foreach(FileInfo f in files)
            {
                ListViewItem lvi = listView1.Items.Add(f.FullName);
                lvi.SubItems.Add("未转换");
            }

            progressBar1.Maximum = files.Length;
        }

        private void textBox1_Click(object sender, EventArgs e)
        {
            if(!CheckFormat()) return;
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() != DialogResult.OK) return;
            textBox1.Text = fbd.SelectedPath;
            ListImage();
        }

        private void textBox2_Click(object sender, EventArgs e)
        {
            if(!CheckFormat()) return;
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            if (fbd.ShowDialog() != DialogResult.OK) return;
            textBox2.Text = fbd.SelectedPath;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.MinimumSize = this.Size;
            this.MaximumSize = this.Size;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private bool CheckDirectory()
        {
            if (textBox1.Text.Trim() == "" || textBox2.Text.Trim() == "")
            {
                return false;
            }

            return true;
        }

        private void ConvertImage()
        {
            listView1.Items.Clear();
            foreach (FileInfo f in files)
            {
                Image img = Image.FromFile(f.FullName);
                string fullpath = f.FullName;
                string[] part = fullpath.Split(\'\\\');
                string filename = part[part.Length-1];
                string[] part2 = filename.Split(\'.\');
                string filename2 = part2[0];
                img.Save(textBox2.Text.Trim() + "\\" + filename2 + "." + cbxLaterFormat.Text.Trim());

                ListViewItem lvi = listView1.Items.Add(f.FullName);
                lvi.SubItems.Add("完成");

                progressBar1.Value++;
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (!CheckDirectory())
            {
                MessageBox.Show("请先选择图片路径!", "错误提示");
                return;
            }

            ConvertImage();
        }
    }
}

分类:

技术点:

相关文章:

  • 2021-10-08
  • 2021-10-25
  • 2021-11-10
  • 2021-12-10
  • 2021-10-25
  • 2021-11-11
  • 2021-09-05
  • 2021-09-05
猜你喜欢
  • 2021-09-13
  • 2021-09-13
  • 2021-09-13
  • 2021-12-16
  • 2021-09-13
  • 2021-09-05
  • 2021-09-11
相关资源
相似解决方案