首页 > 编程 > C# > 正文

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

2023-05-16 12:36:05
字体:
来源:转载
供稿:网友

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表