设为首页
加入收藏
联系我们
首页 基础教程 技术文档 实例分析 数 据 库 疑难杂症 ASP.NET 七夕许愿树 技术论坛
最新文章
  .NET上传图片加文字…
 在vs.net bate…
 C#中利用正则表达式实现…
 Visual C#的SQ…
 关于使用存储过程创建分页
 通用分页显示查询存储过程
 大数据量的分页
 什么是web.confi…
 ASP.NET 配置文件…
 对“三层结构”的深入理解…
 多个关键字的查询问题
 VB和C# 语法对比图 …
 C#基础全接触
 用C#压缩和修复Acce…
 C#中的类型相等与恒等(…
推荐文章
 关于C#中的REF和黓认…
 读书笔记c#高级编程 委…
 【算法】C#快速排序类
 Visual C#的SQ…
 C#中调用API
 Infragistics…
 C#接口转换
 C#读取设备信息
 用.net操作word
 C# MessageBo…
 Visual C#中的数…
 雅虎公司C#笔试题,看看…
 C#.NET使用NHib…
 .net学习之运算符重载…
 Visual C# 3.…
热门文章
 ADO.Net与ADO在…
 开发ASP.NET下的M…
 用C#+XMI技术进行U…
 什么是虚拟机?
 C#基础全接触
 C#中利用正则表达式实现…
 雅虎公司C#笔试题,看看…
 用Visual C# 2…
 C#学习第一天
 C#语言初级入门(1)
 VB和C# 语法对比图 …
 远程重启计算机(C#)
 什么是B/S三层?
 Visual C#的SQ…
 Visual C#常用函…
一个批量生成缩略图工具的源代码
阅读正文 文字大小:增大 减小  文字行距:增大 减小   双击自动滚屏
本文由中国C#技术学习中心整理  如果你对本文有不明之处请到技术论坛讨论!

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.Po
            int( 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.Po
            int( 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.Po
            int( 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.Po
            int( 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.Po
            int( 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.Po
            int( 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.Po
            int( 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.Po
            int( 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.Po
            int( 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

本文由中国C#技术学习中心整理  如果你对本文有不明之处请到技术论坛讨论!

中国C#技术交流QQ群:6337034  10976424  9383681  35248582  35248645
版权所有:中国C#技术学习中心 Copyright © 20010-20012
建议浏览分辨率使用:1024*768分辨率
粤ICP备05002251号