当前位置:K88软件开发文章中心编程语言.NET.NET01 → 文章内容

使用C#批量生成缩略图的工具源代码

减小字体 增大字体 作者:佚名  来源:翔宇亭IT乐园  发布时间:2018-12-31 11:48:39

:2010-03-03 18:02:00

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( );
        }
    }
    

:2010-03-03 18:02:00

    /// <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);
        }

上一页  

:2010-03-03 18:02:00

        #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

上一页  

:2010-03-03 18:02:00

        /// <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( );
                }
            }
        }
    }
}

原文地址:http://lanustudio.bokee.com/viewdiary.25518139.html


上一页  [1] [2] [3] [4] 


使用C#批量生成缩略图的工具源代码