• C# 批量图片合并工具(附源代码)


    ps: 最近在网上寻了几天asp.net相关教材,最终发现几本PDF教材还不错,于是下来看看。但是本人还是不大习惯用电脑上看,眼睛受不起!

    于是去买了A4纸打印出重要章节看看,结果发现PDF里都加上logo备注信息,原大小打印到A4纸上字太小了,放大了打印还歪的,怎么办?

    只好导出PDF里的所有图片再打印。结果麻烦一个又接一个啊,发现里面导出的图片都是半截的,每2张图片为一页。哇靠!还得拼接图片再打印,

    又下载Photoshop蛮有耐心的拼接了几十张出来,整个人麻木了。啊ri_ta祖宗de,就不能给我弄好了再发出来?  哎!算了反正也都是免费下的,

    继续上网搜索关于批量图片合成工具,找了半天没有一个是给我这个定身量做的!看来只能自己动手做一个了,差点忘记自己学啥了。。。

       通过一个晚上的研究,最后终于使用自己编写的程序完成了我的个人需求。 好了,废话不多说下面附上所写代码:

      1 using System;
      2 using System.Collections.Generic;
      3 using System.ComponentModel;
      4 using System.Data;
      5 using System.Drawing;
      6 using System.Linq;
      7 using System.Text;
      8 using System.Windows.Forms;
      9 using System.IO;
     10 
     11 namespace WindowsFormsApplication2
     12 {
     13     public partial class Form1 : Form
     14     {
     15         public Form1()
     16         {
     17             InitializeComponent();
     18         }
     19 
     20         public string GetButton_Click(object sender)
     21         {
     22             string sPath = "";
     23             switch (((Button)sender).Name)
     24             {
     25                 case "button1":
     26                     if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
     27                     {
     28                         sPath = folderBrowserDialog1.SelectedPath.ToString();
     29                     }
     30                     break;
     31                 case "button2":
     32                     if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
     33                     {
     34                         sPath = folderBrowserDialog1.SelectedPath.ToString();
     35                     }
     36                     break;
     37             }
     38             return sPath;
     39         }
     40 
     41         private void button1_Click(object sender, EventArgs e)
     42         {
     43             textBox1.Text = GetButton_Click(sender);
     44         }
     45 
     46         private void button2_Click(object sender, EventArgs e)
     47         {
     48             textBox2.Text = GetButton_Click(sender);
     49         }
     50 
     51         private void textBox1_TextChanged(object sender, EventArgs e)
     52         {
     53             listBox1.Items.Clear();
     54             button3.Enabled = false;
     55             if (textBox1.Text != "")
     56             { 
     57                 DirectoryInfo TheFolder1 = new DirectoryInfo(textBox1.Text);
     58                 foreach (FileInfo NextFile in TheFolder1.GetFiles(comboBox1.Text))
     59                     this.listBox1.Items.Add(NextFile.Name);
     60                 label6.Text = string.Format("共:{0} 个文件", listBox1.Items.Count);
     61                 if (listBox1.Items.Count > 2 && textBox2.Text != "")
     62                     button3.Enabled = true;
     63                 else
     64                     button3.Enabled = false;
     65             }      
     66         }
     67 
     68         private void textBox2_TextChanged(object sender, EventArgs e)
     69         {
     70             if (listBox1.Items.Count > 2 && textBox2.Text != "")
     71                 button3.Enabled = true;
     72             else
     73                 button3.Enabled = false;
     74         }
     75 
     76         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
     77         {
     78             comboBox2.SelectedIndex = comboBox1.SelectedIndex;
     79         }
     80 
     81         private void Form1_Load(object sender, EventArgs e)
     82         {
     83             comboBox1.SelectedIndex = 0;
     84             comboBox2.SelectedIndex = 0;
     85         }
     86 
     87         private Bitmap MergerImg(params Bitmap[] maps)
     88         {
     89             int X = 0;
     90             int Y = 0;
     91             Bitmap newImg;
     92             for (int i = 0; i < maps.Length; i++)
     93             {
     94                 if (radioButton1.Checked) //左右合并
     95                 {
     96                     X += maps[i].Width;
     97                     Y = Y > maps[i].Height ? Y : maps[i].Height;
     98                 }
     99                 else                      //上下合并
    100                 {
    101 
    102                     X = X > maps[i].Width ? X : maps[i].Width;
    103                     Y += maps[i].Height;
    104                 }
    105             }
    106             newImg = new Bitmap(X, Y);
    107             X = 0;
    108             Y = 0;
    109             Graphics g = Graphics.FromImage(newImg);
    110             g.Clear(System.Drawing.Color.White);
    111             for (int i = 0; i < maps.Length; i++)
    112             {
    113                 g.DrawImage(maps[i], X, Y, maps[i].Width, maps[i].Height);
    114                 if (radioButton1.Checked) //左右合并
    115                 {
    116                     X += maps[i].Width;
    117                 }
    118                 else
    119                 {
    120                     Y += maps[i].Height;
    121                 }
    122             }
    123             g.Dispose();
    124             return newImg;
    125         }
    126 
    127         private void button3_Click(object sender, EventArgs e)
    128         {
    129             try
    130             {
    131                 comboBox1.Enabled = false;
    132                 comboBox2.Enabled = false;
    133                 button1.Enabled = false;
    134                 button2.Enabled = false;
    135                 button3.Enabled = false;
    136                 numericUpDown1.Enabled = false;
    137                 radioButton1.Enabled = false;
    138                 radioButton2.Enabled = false;
    139                 Bitmap[] bit = new Bitmap[(int)numericUpDown1.Value];
    140                 int j = 0;
    141                 progressBar1.Maximum = listBox1.Items.Count;
    142                 progressBar1.Value = 0;
    143                 for (int i = 1; i < listBox1.Items.Count + 1; i++)
    144                 {
    145                     bit[j] = new Bitmap(textBox1.Text + "\\" + listBox1.Items[i - 1].ToString());
    146                     j++;
    147                     if (i % (int)numericUpDown1.Value == 0)
    148                     {
    149                         j = 0;
    150                         string str;
    151                         str = textBox2.Text + "\\temp" + ((int)(i / (int)numericUpDown1.Value)).ToString() + comboBox2.Text;
    152                         if (this.comboBox2.SelectedIndex == 0)
    153                         {
    154                             MergerImg(bit).Save(str, System.Drawing.Imaging.ImageFormat.Bmp);
    155                         }
    156                         else
    157                         {
    158                             MergerImg(bit).Save(str, System.Drawing.Imaging.ImageFormat.Jpeg);
    159                         }
    160                     }
    161                     if (progressBar1.Value < progressBar1.Maximum)
    162                         progressBar1.Value += 1;
    163                     label5.Text = ((progressBar1.Value * 100) / (progressBar1.Maximum)).ToString() + " %";
    164                     Application.DoEvents();
    165                 }
    166                 MessageBox.Show("合并完成!", "提示", MessageBoxButtons.OK);
    167             }
    168             catch (Exception ee)
    169             {
    170                 MessageBox.Show(ee.Message);
    171             }
    172             finally
    173             {
    174                 comboBox1.Enabled = true;
    175                 comboBox2.Enabled = true;
    176                 button1.Enabled = true;
    177                 button2.Enabled = true;
    178                 button3.Enabled = true;
    179                 numericUpDown1.Enabled = true;
    180                 radioButton1.Enabled = true;
    181                 radioButton2.Enabled = true;
    182             }
    183         }
    184         
    185     }
    186 }
  • 相关阅读:
    跳转网页
    在代码插入图像的位置
    图像宽度和高度
    添加图像
    指向同一网站中其中的页面的链接
    指向其他网站的链接
    嵌套列表
    定义
    LeetCode561 数组拆分 I
    LeetCode344 反转字符串
  • 原文地址:https://www.cnblogs.com/han1982/p/2754473.html
Copyright © 2020-2023  润新知