批量切割图片

form1.cs

 

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.Drawing;
using System.IO;
using System.Threading;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
     
        public Form1()
        {
            InitializeComponent();
          
        }

        private void LogUri(int i,string path)
        {

            ListViewItem item = this.listView2.Items.Insert(i, path);

        }
     
        private void button1_Click(object sender, System.EventArgs e)
        {
           
                try
                {
                    Form3 dlg = new Form3(); if (dlg.ShowDialog() == DialogResult.OK)
                    {
                        DirectoryInfo info = dlg.info;
                        this.textBox1.Text = dlg.fullPath;

                    }
                }
                catch (Exception err)
                {
                    Console.WriteLine(err.Message);
                }
            }
       

        private void Form1_Load(object sender, EventArgs e)
        {

        }
      


        private void button2_Click(object sender, EventArgs e)
        {
           
            try
            {
                Form3 dlg = new Form3(); if (dlg.ShowDialog() == DialogResult.OK)
                {
                    DirectoryInfo info = dlg.info;
                    textBox2.Text = dlg.fullPath;

                    // 在文本框中添加目录信息
                  
                }
            }
            catch (Exception err)
            {
                Console.WriteLine(err.Message);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {


            if (textBox1.Text.Trim().Length == 0)
            {
                MessageBox.Show("文件路径不能为空!");
                return;
            }
            if (textBox2.Text.Trim().Length == 0)
            {
                MessageBox.Show("复制路径不能为空!");
                return;
            }
            if (textBox3.Text.Trim().Length == 0)
            {
                MessageBox.Show("请设置宽度");
                return;
            }
            if (textBox4.Text.Trim().Length == 0)
            {
                MessageBox.Show("请设置高度");
                return;
            }
            this.backgroundWorker1.RunWorkerAsync();
            this.button3.Enabled = false;
            this.button4.Enabled = true;
           
          
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            this.progressBar1.Value = e.ProgressPercentage;
        
            this.listView2.Items.Add(e.UserState.ToString());

        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.button3.Enabled = true;
           
            MessageBox.Show("完成任务");

        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            work(this.backgroundWorker1,this.textBox1.Text,this.textBox2.Text);

        }

        private void work(BackgroundWorker bk,string path1,string path2)
        {
           
          
          //  string path1 = this.textBox1.Text;
            string[] MyFiles = Directory.GetFiles(path1);
            int cwidth = Convert.ToInt32(this.textBox3.Text.ToString());
            int cheight = Convert.ToInt32(this.textBox4.Text.ToString());
            int resize =1;
            if (this.checkBox2.Checked)
            {
               if(this.textBox5.Text=="")
               {
                   resize=1;
               }
               else
               {
                   resize = Convert.ToInt32(this.textBox5.Text) / cwidth;
            }
            }
          
            for (int g = 0; g < MyFiles.Length; g++)
            {
                if (bk.CancellationPending) //这里判断一下是否用户要求取消后台进行,并可以尽早退出。
                {
                    bk.ReportProgress(100 * (g + 1) / MyFiles.Length, String.Format("当前值是 {0},操作被用户申请中断", MyFiles[g]));
                    return ;
                }
             //   this.listView2.Items.Clear();
                string img1 = MyFiles[g];
                if (img1.IndexOf(".jpg") > 0 || img1.IndexOf(".JPG") > 0)
                {
                    //this.label7.Text = img1;
                    string name = img1.Replace(path1, "");
                    name = name.Replace(".jpg", "");
                    name = name.Replace(".JPG", "");
                    System.Drawing.Image image;
                    image = System.Drawing.Image.FromFile(img1);
                    int width = image.Width;
                    int height = image.Height;
                    int i = height / cheight;
                    int k = width / cwidth;
                    int mh = height % cheight;
                    int mw = width % cwidth;

                    string path = path2 + "//" + name +"//";
                    if (this.checkBox1.Checked)
                    {
                        path = path2;

                    }
                    if (!Directory.Exists(path))     //   保证不重复  
                    {
                        Directory.CreateDirectory(path);
                    }

                    ImgCropper_WebHandler iw = new ImgCropper_WebHandler();
                    string fidd = "";
                    for (int n = 0; n < k; n++)
                    {
                        for (int j = 0; j < i; j++)
                        {
                            fidd = iw.smallimage(img1, resize, n * cwidth, j * cheight, cwidth, cheight, path, name, n, j) + "<br>";
                            //LogUri(n + j, path + name + n + j + ".jpg");
                            bk.ReportProgress(100*g / MyFiles.Length, String.Format("当前值是 {0} ", path + name + n + j + ".jpg"));

                            Thread.Sleep(1);
                        }
                        if (mh > 0)
                        {

                            fidd = iw.smallimage(img1, resize, n * cwidth, i * cheight,cwidth, mh, path, name, n, i) + "<br>";
                           // LogUri(n + i, path + name + n + i + ".jpg");
                            bk.ReportProgress(g / MyFiles.Length * 100, String.Format("当前值是 {0} ", path + name + n + i + ".jpg"));
                            Thread.Sleep(1);
                        }
                    }
                    if (mw > 0)
                    {
                        for (int j = 0; j < i; j++)
                        {

                            fidd = iw.smallimage(img1, resize, k * cwidth, j * cheight, mw, cheight, path, name, k, j) + "<br>";
                            //LogUri(k + j, path + name + k + j + ".jpg");
                            bk.ReportProgress(g / MyFiles.Length * 100, String.Format("当前值是 {0} ", path + name + k + j + ".jpg"));
                            Thread.Sleep(1);
                        }
                    }
                    if (mh > 0 && mw > 0)
                    {

                        fidd = iw.smallimage(img1, resize, k * cwidth, i * cheight, mw, mh, path, name, k, i) + "<br>";
                       // LogUri(k + i, path + name + k + i + ".jpg");
                        bk.ReportProgress(g / MyFiles.Length, String.Format("当前值是 {0} ", path + name + k + i + ".jpg"));
                        Thread.Sleep(1);
                    }

 

                }


            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.backgroundWorker1.CancelAsync();

        }

       

     

    }
 

}

form3//

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;
namespace WindowsFormsApplication1
{
    public partial class Form3 : Form
    {
private static string driveLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// 用于列举驱动器盘符
private DirectoryInfo folder; // 用于保存目录信息

//修改类的构造函数如下:fillTree()函数在后面介绍

        public Form3()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
            fillTree();// 该函数调用完成目录选择对话框的初始设置
        }

        private void fillTree()
        {
            DirectoryInfo directory;
            string sCurPath = ""; // 重新清空
            treeView1.Nodes.Clear();

            // 将硬盘上的所有的驱动器都列举出来
            foreach (char c in driveLetters)
            {
                sCurPath = c + "://";
                try
                {
                    // 获得该路径的目录信息
                    directory = new DirectoryInfo(sCurPath);

                    // 如果获得的目录信息正确,则将它添加到目录树视中
                    if (directory.Exists == true)
                    {
                        TreeNode newNode = new TreeNode(directory.FullName);
                        treeView1.Nodes.Add(newNode); // 添加新的节点到根节点
                        getSubDirs(newNode);
                        // 调用getSubDirs()函数,检查该驱动器上的任何存在子目录
                    }
                }
                catch (Exception doh)
                {
                    Console.WriteLine(doh.Message);
                }
            }
        }

        private void getSubDirs(TreeNode parent)
        {
            DirectoryInfo directory;
            try
            {
                // 如果还没有检查过这个文件夹,则检查之
                if (parent.Nodes.Count == 0)
                {
                    directory = new DirectoryInfo(parent.FullPath);
                    foreach (DirectoryInfo dir in directory.GetDirectories())
                    {
                        // 新建一个数节点,并添加到目录树视
                        TreeNode newNode = new TreeNode(dir.Name);
                        parent.Nodes.Add(newNode);
                    }
                }

                foreach (TreeNode node in parent.Nodes)
                {
                    // 如果还没有检查过这个文件夹,则检查
                    if (node.Nodes.Count == 0)
                    {
                        directory = new DirectoryInfo(node.FullPath);

                        // 检查该目录上的任何子目录
                        foreach (DirectoryInfo dir in directory.GetDirectories())
                        {
                            // 新建一个数节点,并添加到目录树视
                            TreeNode newNode = new TreeNode(dir.Name);
                            node.Nodes.Add(newNode);
                        }
                    }
                }
            }
            catch (Exception doh)
            {
                Console.WriteLine(doh.Message);
            }
        }

        private string fixPath(TreeNode node)
        {
            string sRet = "";
            try
            {
                sRet = node.FullPath;
                int index = sRet.IndexOf("");
                if (index > 1)
                {
                    sRet = node.FullPath.Remove(index, 1);
                }
            }
            catch (Exception doh)
            {
                Console.WriteLine(doh.Message);
            }
            return sRet;
        }

        //接着,给该类添加以下几个属性,这几个属性都是反映所选择的目录的相关信息的,它们将被主窗体的类调用:

        public string name// 返回所选择的目录的名称
        {
            get { return ((folder != null && folder.Exists)) ? folder.Name : null; }
        }
        public string fullPath// 返回所选择的目录的完整路径
        {
            get { return ((folder != null && folder.Exists && treeView1.SelectedNode != null)) ? fixPath(treeView1.SelectedNode) : null; }

        }


        public DirectoryInfo info// 返回所选择的目录的信息
        {
            get { return ((folder != null && folder.Exists)) ? folder : null; }
        }

        //最后,还要添加目录树控件的BeforeSelect()和BeforeExpand()事件函数以及两个按钮的OnClick事件函数。BeforeSelect()事件函数是用户选定目录前的一个事件函数,它完成了子目录取得、设置文本框内容、获取该目录信息等功能。同样,BeforeExpand()事件函数完成相似的功能,只不过它是在目录节点被展开前发生的。具体的函数实现如下:

        private void treeView1_BeforeSelect(object sender, System.Windows.Forms.TreeViewCancelEventArgs e)
        {
            getSubDirs(e.Node); // 取得选择节点的子文件夹
            textBox1.Text = fixPath(e.Node); // 更新文本框内容
            folder = new DirectoryInfo(e.Node.FullPath); // 获得它的目录信息
        }

        private void treeView1_BeforeExpand(object sender, System.Windows.Forms.TreeViewCancelEventArgs e)
        {
            getSubDirs(e.Node); // 取得选择节点的子文件夹
            textBox1.Text = fixPath(e.Node); // 更新文本框内容
            folder = new DirectoryInfo(e.Node.FullPath); // 获得它的目录信息
        }

        private void button1_Click(object sender, System.EventArgs e)
        // "选择"按钮的消息处理函数
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void button2_Click(object sender, System.EventArgs e)
        // "取消"按钮的消息处理函数
        {
            folder = null;
            this.Close();
        }

        private void Form3_Load(object sender, EventArgs e)
        {

        }

     


        //4.到此为止,我们已经完成了所有的代码编写工作。现在可以按Ctrl+F5试试运行效果了。

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值