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

        
Windows 窗体设计器生成的代码
        
/// <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;
            
foreachstring 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( );
                }

            }

        }

    }

}


posted @ 2007-09-11 10:07  宿远  阅读(325)  评论(0)    收藏  举报