一個批量生成縮略圖工具的源代碼

轉載請註明出處!

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

using System.Threading;
using System.IO;
using System.Diagnostics;
using System.Windows.Forms.Design;

namespace Image
{
 public class ImageConvert
 {
  public static void ShowThumbnail(string oldfile, string newfile, int h, int w)
  {
  
   System.Drawing.Image img = System.Drawing.Image.FromFile(oldfile);
   System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);

   int oldh = img.Height;
   int oldw = img.Width;

   int newh,neww;

   double h1 = oldh*1.0/h;
   double w1 = oldw*1.0/w;

   double f = (h1>w1)? h1:w1;

   if(f < 1.0)
   {
    newh = oldh;
    neww = oldw;
   }
   else
   {
    newh = (int)(oldh/f);
    neww = (int)(oldw/f);
   }

   System.Drawing.Image myThumbnail = img.GetThumbnailImage(neww, newh, myCallback, IntPtr.Zero);

   myThumbnail.Save(newfile, System.Drawing.Imaging.ImageFormat.Jpeg);

   img.Dispose();
   myThumbnail.Dispose();
  }
  private static bool ThumbnailCallback()
  {
   return false;
  }
 }


 public class FolderDialog : FolderNameEditor
 {
  FolderNameEditor.FolderBrowser fDialog = new System.Windows.Forms.Design.FolderNameEditor.FolderBrowser();
  public FolderDialog()
  {
  }
  public DialogResult DisplayDialog()
  {
   return DisplayDialog("請選擇一個文件夾");
  }

  public DialogResult DisplayDialog(string description)
  {
   fDialog.Description = description;
   return fDialog.ShowDialog();
  }
  public string Path
  {
   get
   {
    return fDialog.DirectoryPath;
   }
  }
  ~FolderDialog()
  {
   fDialog.Dispose();
  }
 }


 /// <summary>
 /// Form1 的摘要說明。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.LinkLabel linkLabel1;
  private System.Windows.Forms.LinkLabel linkLabel2;
  private System.Windows.Forms.TextBox path1;
  private System.Windows.Forms.TextBox path2;
  private System.Windows.Forms.TextBox width;
  private System.Windows.Forms.TextBox height;
  private System.Windows.Forms.ListView listView1;
  private System.Windows.Forms.LinkLabel linkLabel3;
  private System.Windows.Forms.LinkLabel linkLabel4;
  private System.Windows.Forms.ColumnHeader columnHeader1;
  private System.Windows.Forms.ColumnHeader columnHeader2;
  private System.Windows.Forms.ColumnHeader columnHeader3;
  private System.Windows.Forms.ColumnHeader columnHeader4;
  private System.Windows.Forms.ColumnHeader columnHeader5;
  private System.Windows.Forms.ColumnHeader columnHeader6;

  private ArrayList threads = new ArrayList();
  private ArrayList images = new ArrayList();
  /// <summary>
  /// 必需的設計器變量。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Windows 窗體設計器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 調用後添加任何構造函數代碼
   //
  }

  /// <summary>
  /// 清理所有正在使用的資源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows 窗體設計器生成的代碼
  /// <summary>
  /// 設計器支持所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內容。
  /// </summary>
  private void InitializeComponent()
  {
   this.linkLabel1 = new System.Windows.Forms.LinkLabel();
   this.linkLabel2 = new System.Windows.Forms.LinkLabel();
   this.path1 = new System.Windows.Forms.TextBox();
   this.path2 = new System.Windows.Forms.TextBox();
   this.width = new System.Windows.Forms.TextBox();
   this.height = new System.Windows.Forms.TextBox();
   this.listView1 = new System.Windows.Forms.ListView();
   this.linkLabel3 = new System.Windows.Forms.LinkLabel();
   this.linkLabel4 = new System.Windows.Forms.LinkLabel();
   this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
   this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
   this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
   this.columnHeader4 = new System.Windows.Forms.ColumnHeader();
   this.columnHeader5 = new System.Windows.Forms.ColumnHeader();
   this.columnHeader6 = new System.Windows.Forms.ColumnHeader();
   this.SuspendLayout();
   //
   // linkLabel1
   //
   this.linkLabel1.Location = new System.Drawing.Point(456, 16);
   this.linkLabel1.Name = "linkLabel1";
   this.linkLabel1.Size = new System.Drawing.Size(32, 16);
   this.linkLabel1.TabIndex = 0;
   this.linkLabel1.TabStop = true;
   this.linkLabel1.Text = "瀏覽";
   this.linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);
   //
   // linkLabel2
   //
   this.linkLabel2.Location = new System.Drawing.Point(456, 48);
   this.linkLabel2.Name = "linkLabel2";
   this.linkLabel2.Size = new System.Drawing.Size(32, 16);
   this.linkLabel2.TabIndex = 1;
   this.linkLabel2.TabStop = true;
   this.linkLabel2.Text = "瀏覽";
   this.linkLabel2.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel2_LinkClicked);
   //
   // path1
   //
   this.path1.Location = new System.Drawing.Point(16, 16);
   this.path1.Name = "path1";
   this.path1.Size = new System.Drawing.Size(432, 21);
   this.path1.TabIndex = 2;
   this.path1.Text = "";
   //
   // path2
   //
   this.path2.Location = new System.Drawing.Point(16, 48);
   this.path2.Name = "path2";
   this.path2.Size = new System.Drawing.Size(432, 21);
   this.path2.TabIndex = 3;
   this.path2.Text = "";
   //
   // width
   //
   this.width.Location = new System.Drawing.Point(80, 80);
   this.width.Name = "width";
   this.width.Size = new System.Drawing.Size(72, 21);
   this.width.TabIndex = 4;
   this.width.Text = "";
   //
   // height
   //
   this.height.Location = new System.Drawing.Point(216, 80);
   this.height.Name = "height";
   this.height.Size = new System.Drawing.Size(72, 21);
   this.height.TabIndex = 5;
   this.height.Text = "";
   //
   // listView1
   //
   this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
                      this.columnHeader1,
                      this.columnHeader2,
                      this.columnHeader3,
                      this.columnHeader4,
                      this.columnHeader5,
                      this.columnHeader6});
   this.listView1.Location = new System.Drawing.Point(16, 112);
   this.listView1.Name = "listView1";
   this.listView1.Size = new System.Drawing.Size(472, 152);
   this.listView1.TabIndex = 6;
   this.listView1.View = System.Windows.Forms.View.Details;
   //
   // linkLabel3
   //
   this.linkLabel3.Location = new System.Drawing.Point(304, 80);
   this.linkLabel3.Name = "linkLabel3";
   this.linkLabel3.Size = new System.Drawing.Size(32, 16);
   this.linkLabel3.TabIndex = 7;
   this.linkLabel3.TabStop = true;
   this.linkLabel3.Text = "添加";
   this.linkLabel3.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel3_LinkClicked);
   //
   // linkLabel4
   //
   this.linkLabel4.Location = new System.Drawing.Point(376, 80);
   this.linkLabel4.Name = "linkLabel4";
   this.linkLabel4.Size = new System.Drawing.Size(32, 16);
   this.linkLabel4.TabIndex = 8;
   this.linkLabel4.TabStop = true;
   this.linkLabel4.Text = "開始";
   this.linkLabel4.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel4_LinkClicked);
   //
   // columnHeader1
   //
   this.columnHeader1.Text = "源";
   this.columnHeader1.Width = 122;
   //
   // columnHeader2
   //
   this.columnHeader2.Text = "目的";
   this.columnHeader2.Width = 121;
   //
   // columnHeader3
   //
   this.columnHeader3.Text = "寬";
   this.columnHeader3.Width = 49;
   //
   // columnHeader4
   //
   this.columnHeader4.Text = "高";
   this.columnHeader4.Width = 42;
   //
   // columnHeader5
   //
   this.columnHeader5.Text = "文件數";
   //
   // columnHeader6
   //
   this.columnHeader6.Text = "完成數";
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
   this.ClientSize = new System.Drawing.Size(504, 294);
   this.Controls.Add(this.linkLabel4);
   this.Controls.Add(this.linkLabel3);
   this.Controls.Add(this.listView1);
   this.Controls.Add(this.height);
   this.Controls.Add(this.width);
   this.Controls.Add(this.path2);
   this.Controls.Add(this.path1);
   this.Controls.Add(this.linkLabel2);
   this.Controls.Add(this.linkLabel1);
   this.Name = "Form1";
   this.Text = "Form1";
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// 應用程序的主入口點。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  private void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
  {
   FolderDialog dlg = new FolderDialog();
   if(dlg.DisplayDialog()==DialogResult.OK)
    path1.Text = dlg.Path;

  }

  private void linkLabel2_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
  {
   FolderDialog dlg = new FolderDialog();
   if(dlg.DisplayDialog()==DialogResult.OK)
    path2.Text = dlg.Path;
  }

  private void linkLabel3_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
  {
   string[] img = Directory.GetFiles(path1.Text, "*.jpg");

   listView1.Items.Add(new ListViewItem(new string[]{path1.Text, path2.Text, width.Text, height.Text, img.Length.ToString(), "0"}));
  }

  private void linkLabel4_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
  {
   foreach(ListViewItem item in listView1.Items)
   {
    Thread t = new Thread(new ThreadStart(Process));
    t.Start();
    threads.Add(t);
   }
  }

  private void Process()
  {
   int index = threads.IndexOf(Thread.CurrentThread);
   ListViewItem item = listView1.Items[index];
   string path1 = item.SubItems[0].Text;
   string path2 = item.SubItems[1].Text;
   int width = int.Parse(item.SubItems[2].Text);
   int height = int.Parse(item.SubItems[3].Text);
   int i = 0;
   foreach(string file1 in Directory.GetFiles(path1, "*.jpg"))
   {
    i++;
    string file2 = path2 + @"/" + Path.GetFileName(file1);
    ImageConvert.ShowThumbnail(file1,file2, height, width);
    lock(listView1)
    {
     listView1.Items[index].SubItems[5].Text = i.ToString();
    }
   }
  }
 }
}
Release版下載地址:http://asp2004.net/temp/Image.exe

發佈了80 篇原創文章 · 獲贊 0 · 訪問量 31萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章