中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

C#壓縮圖片算法

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用
    using System.IO;  
    using System.Drawing;  
    using System.Drawing.Imaging;  
    using System;  
    namespace Bll  
    {  
        /// <summary>  
        /// 圖片處理類  
        /// 1、生成縮略圖片或按照比例改變圖片的大小和畫質(zhì)  
        /// 2、將生成的縮略圖放到指定的目錄下  
        /// </summary>  
        public class ImageHepler  
        {  
            public Image ResourceImage, ReducedImage;  
            private int ImageWidth;  
            private int ImageHeight;  
            public string ErrMessage;  
      
            /// <summary>  
            /// 類的構(gòu)造函數(shù)  
            /// </summary>  
            /// <param name="ImageFileName">圖片文件的全路徑名稱</param>  
            public ImageHepler(string ImageFileName)  
            {  
                ResourceImage = Image.FromFile(ImageFileName);  
                ErrMessage = "";  
            }  
      
            public bool ThumbnailCallback()  
            {  
                return false;  
            }  
      
            /// <summary>  
            /// 生成縮略圖,返回縮略圖的Image對象  
            /// </summary>  
            /// <param name="Width">縮略圖的寬度</param>  
            /// <param name="Height">縮略圖的高度</param>  
            /// <returns>縮略圖的Image對象</returns>  
            public Image GetReducedImage(int Width, int Height)  
            {  
                double LengthLong;          //存儲(長和寬中)較短的長度  
                int widthOK, heightOK;      //存儲實際要生成的圖片的長寬  
                if (Width < Height)         //判斷輸入的長和寬那個較短  
                {  
                    LengthLong = Width;     //把較短的存儲在 LengthLonh 用于計算  
                }  
                else  
                {  
                    LengthLong = Height;  
                }  
                try  
                {  
                    //判斷原圖片 長和寬   
                    //原圖比較長的一個邊要和縮略圖的邊相等  
                    if (ResourceImage.Width > ResourceImage.Height)  
                    {  
                        widthOK = (int)LengthLong;  
                        heightOK = (int)(LengthLong / ResourceImage.Width * ResourceImage.Height);  
                    }  
                    else  
                    {  
                        heightOK = (int)LengthLong;  
                        widthOK = (int)LengthLong / ResourceImage.Height * ResourceImage.Width;  
      
                    }  
                    Image ReducedImage;  
                    Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);  
                    ReducedImage = ResourceImage.GetThumbnailImage(widthOK, heightOK, callb, IntPtr.Zero);  
                    return ReducedImage;  
                }  
                catch (Exception e)  
                {  
                    ErrMessage = e.Message;  
                    return null;  
                }  
            }  
      
            /// <summary>  
            /// 生成縮略圖,將縮略圖文件保存到指定的路徑  
            /// </summary>  
            /// <param name="Width">縮略圖的寬度</param>  
            /// <param name="Height">縮略圖的高度</param>  
            /// <param name="targetFilePath">縮略圖保存的全文件名,(帶路徑),參數(shù)格式:D:\Images\filename.jpg</param>  
            /// <returns>成功返回true,否則返回false</returns>  
            public bool GetReducedImage(int Width, int Height, string targetFilePath)  
            {  
                double LengthLong;          //存儲(長和寬中)較短的長度  
                int widthOK, heightOK;      //存儲實際要生成的圖片的長寬  
                if (Width < Height)         //判斷輸入的長和寬那個較短  
                {  
                    LengthLong = Width;     //把較短的存儲在 LengthLonh 用于計算  
                }  
                else  
                {  
                    LengthLong = Height;  
                }  
                try  
                {  
                    //判斷原圖片 長和寬   
                    //原圖比較長的一個邊要和縮略圖的邊相等  
                    if (ResourceImage.Width > ResourceImage.Height)  
                    {  
                        widthOK = (int)LengthLong;  
                        heightOK = (int)(LengthLong / ResourceImage.Width * ResourceImage.Height);  
                    }  
                    else  
                    {  
                        heightOK = (int)LengthLong;  
                        widthOK = (int)LengthLong / ResourceImage.Height * ResourceImage.Width;  
                    }  
                    Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);  
                    ReducedImage = ResourceImage.GetThumbnailImage(widthOK, heightOK, callb, IntPtr.Zero);  
                    ReducedImage.Save(@targetFilePath, ImageFormat.Jpeg);  
                    //ReducedImage.Dispose();  
                    return true;  
                }  
                catch (Exception e)  
                {  
                    ErrMessage = e.Message;  
                    return false;  
                }  
            }  
      
            /// <summary>  
            /// 生成縮略圖,返回縮略圖的Image對象  
            /// </summary>  
            /// <param name="Percent">縮略圖的寬度百分比 如:需要百分之80,就填0.8</param>    
            /// <returns>縮略圖的Image對象</returns>  
            public Image GetReducedImage(double Percent)  
            {  
                try  
                {  
                    Image ReducedImage;  
                    Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);  
                    ImageWidth = Convert.ToInt32(ResourceImage.Width * Percent);  
                    ImageHeight = Convert.ToInt32(ResourceImage.Width * Percent);  
                    ReducedImage = ResourceImage.GetThumbnailImage(ImageWidth, ImageHeight, callb, IntPtr.Zero);  
                    return ReducedImage;  
                }  
                catch (Exception e)  
                {  
                    ErrMessage = e.Message;  
                    return null;  
                }  
            }  
      
            /// <summary>  
            /// 生成縮略圖,返回縮略圖的Image對象  
            /// </summary>  
            /// <param name="Percent">縮略圖的寬度百分比 如:需要百分之80,就填0.8</param>    
            /// <param name="targetFilePath">縮略圖保存的全文件名,(帶路徑),參數(shù)格式:D:\Images\filename.jpg</param>  
            /// <returns>成功返回true,否則返回false</returns>  
            public bool GetReducedImage(double Percent, string targetFilePath)  
            {  
                try  
                {  
                    Image.GetThumbnailImageAbort callb = new Image.GetThumbnailImageAbort(ThumbnailCallback);  
                    ImageWidth = Convert.ToInt32(ResourceImage.Width * Percent);  
                    ImageHeight = Convert.ToInt32(ResourceImage.Width * Percent);  
                    ReducedImage = ResourceImage.GetThumbnailImage(ImageWidth, ImageHeight, callb, IntPtr.Zero);  
                    ReducedImage.Save(@targetFilePath, ImageFormat.Jpeg);  
                    //ReducedImage.Dispose();  
                    return true;  
                }  
                catch (Exception e)  
                {  
                    ErrMessage = e.Message;  
                    return false;  
                }  
            }  
        }  
    }  


[csharp] view plaincopy

    using System;  
    using System.Data;  
    using System.Configuration;  
    using System.Linq;  
    using System.Web;  
    using System.IO;  
    using System.Collections;  
    using System.Collections.Generic;  
    using System.Security.AccessControl;  
    using System.Security.Permissions;  
    namespace Bll  
    {  
        public class FolderHelper  
        {  
            //判斷文件夾是否存在  
            public static bool checkFolderExits(string path)  
            {  
                DirectoryInfo dir = new DirectoryInfo(path);  
                if (dir.Exists)//文件夾存在  
                {     
                    return true;  
                }  
                else  
                {  
                   //dir.Create();//不存在就創(chuàng)建一個  
                    return false;  
                }  
            }  
            //創(chuàng)建一個文件夾,存在就創(chuàng)建失敗  
            public static bool CreateNewFolder(string path)  
            {  
                DirectoryInfo dir = new DirectoryInfo(path);  
      
                if (!dir.Exists)  
                {  
                    dir.Create();  
                    return true;  
                }  
                else  
                    return false;  
            }  
            /// <summary>  
            /// 在指定目錄下創(chuàng)建指定名稱文件夾  
            /// </summary>  
            /// <param name="ParentsPath"></param>  
            /// <param name="NewFolderName"></param>  
            /// <returns></returns>  
            public static bool CreateNewFolder(string ParentsPath, string NewFolderName)  
            {  
                string CreatePath = ParentsPath + @"\" + NewFolderName;  
                DirectoryInfo dir = new DirectoryInfo(CreatePath);  
      
                if (!dir.Exists)  
                {  
                    dir.Create();  
                    return true;  
                }  
                else  
                    return false;  
            }  
            /// <summary>  
            /// 返回目錄下的所有文件名  
            /// </summary>  
            /// <param name="path"></param>  
            /// <returns></returns>  
            public static ArrayList getAllFiles(string path)  
            {  
                DirectoryInfo dir = new DirectoryInfo(path);  
                if (dir.Exists)  
                {  
                    FileInfo[] fileinfo = dir.GetFiles();  
                    ArrayList list = new ArrayList();  
                    foreach (FileInfo f in fileinfo)  
                    {  
                        list.Add(f.Name);  
                    }  
                    return list;  
                }  
                else  
                    return null;  
            }  
            /// <summary>  
            /// 計算文件夾的大小  
            /// </summary>  
            /// <param name="d"></param>  
            /// <returns></returns>  
            public static long DirSize(DirectoryInfo d)  
            {  
                long Size = 0;  
                // Add file sizes.  
                FileInfo[] fis = d.GetFiles();//獲得目錄文件列表  
                foreach (FileInfo fi in fis)  
                {  
                    Size += fi.Length;  
                }  
                // Add subdirectory sizes.  
                DirectoryInfo[] dis = d.GetDirectories();//獲取目錄子目錄列表  
                foreach (DirectoryInfo di in dis)  
                {  
                    Size += DirSize(di);  
                }  
                return Size;  
            }  
            /// <summary>  
            /// 把文件夾得大小轉(zhuǎn)換成比較合適的表示單位  
            /// </summary>  
            /// <param name="size"></param>  
            /// <returns></returns>  
            public static string ViewSize(long size)  
            {  
                long m=size;  
                string viewstr;  
                  
                if ((m / 1024) > 0)//表示可以轉(zhuǎn)換成KB  
                {  
                    m = m / 1024;//轉(zhuǎn)換成KB  
                      
                    if ((m / 1024) > 0)//表示可以轉(zhuǎn)換成MB  
                    {  
                        m = m / 1024;//轉(zhuǎn)換成MB了  
      
                        if ((m / 1024) > 0)//表示可以轉(zhuǎn)換成GB  
                        {  
                            m = m / 1024;//轉(zhuǎn)換成GB了  
                            viewstr = m.ToString() + "GB";  
                        }  
                        else  
                        {  
                            viewstr = m.ToString() + "MB";  
                        }  
                    }  
                    else  
                    {  
                        viewstr = m.ToString() + "KB";  
                    }  
                }  
                else  
                {  
                    viewstr = m.ToString() + "byte";  
                }  
                return viewstr;  
            }  
            /// <summary>  
            /// 刪除指定目錄和內(nèi)容  
            /// </summary>  
            /// <param name="dir"></param>  
            /// <returns></returns>  
            public static bool delDir(string dir)  
            {  
                bool flag = false;  
                DirectoryInfo d = new DirectoryInfo(dir);  
                if (d.Exists)//判斷目錄是否存在  
                {  
                    try  
                    {  
                        d.Delete();  
                        flag = true;  
                    }  
                    catch (Exception e) { flag = false; }  
                }  
                return flag;  
            }  
            /// <summary>  
            /// 刪除指定文件  
            /// </summary>  
            /// <param name="fil"></param>  
            /// <returns></returns>  
            public static bool delFile(string fil)  
            {  
                bool flag = false;  
                FileInfo d = new FileInfo(fil);  
                if (d.Exists)//判斷目錄是否存在  
                {  
                    try  
                    {  
                        d.Delete();  
                        flag = true;  
                    }  
                    catch (Exception e) { flag = false; }  
                }  
                return flag;  
            }  
            public static void Copy(string sourceDirectory, string targetDirectory)  
            {  
                DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);  
                DirectoryInfo diTarget = new DirectoryInfo(targetDirectory);  
      
                CopyAll(diSource, diTarget);  
            }  
            /// <summary>  
            /// 復(fù)制目錄及子文件到指定目錄  
            /// </summary>  
            /// <param name="source"></param>  
            /// <param name="target"></param>  
            public static void CopyAll(DirectoryInfo source, DirectoryInfo target)  
            {  
                // Check if the target directory exists, if not, create it.  
                if (Directory.Exists(target.FullName) == false)  
                {  
                    Directory.CreateDirectory(target.FullName);  
                }  
      
                // Copy each file into it's new directory.  
                foreach (FileInfo fi in source.GetFiles())  
                {  
                    Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name);  
                    fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);  
                }  
      
                // Copy each subdirectory using recursion.  
                foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())  
                {  
                    DirectoryInfo nextTargetSubDir =  
                        target.CreateSubdirectory(diSourceSubDir.Name);  
                    CopyAll(diSourceSubDir, nextTargetSubDir);  
                }  
            }  
      
      
      
            /// <summary>  
            /// 循環(huán)讀取某個目錄下的所有文件和目錄,查詢有關(guān)每一項的一些信息。返回一個文件列表  
            /// </summary>  
            /// <param name="strCurrentDir"></param>  
            public static List<fileEntity> FileView(string strCurrentDir)  
            {  
                List<fileEntity> fileList = new List<fileEntity>();  
                DirectoryInfo dir = new DirectoryInfo(strCurrentDir);  
      
                foreach (FileSystemInfo fsi in dir.GetFileSystemInfos())//這個循環(huán)再讀取文件的信息  
                {  
                    try  
                    {  
                        //FileSystemInfo 對象可以表示文件或目錄,從而可以作為 FileInfo 或 DirectoryInfo 對象的基礎(chǔ)。 當分析許多文件和目錄時,請使用該基類。  
                        FileInfo fi;  
                        DirectoryInfo di;  
                        //創(chuàng)建一個自己寫的實體類的實體  
                        fileEntity newfile = new fileEntity();  
                        if (fsi is FileInfo)//外層循環(huán)讀取文件信息  
                        {  
                            //表示當前fsi是文件  
                            fi = (FileInfo)fsi;  
                            newfile.fileName = fi.Name;  
                            newfile.fileExt = fi.Extension;  
                            newfile.fileSize = fi.Length;  
                            newfile.FileModify = fi.LastWriteTime;  
                            //通過擴展名來選擇文件顯示圖標  
                            switch (newfile.fileExt)  
                            {  
                                case ".gif":  
                                    newfile.picName = "gif.gif";  
                                    break;  
                                default:  
                                    newfile.picName = "other.gif";  
                                    break;  
                            }  
                            newfile.picName = "<img src='" + newfile.picName + "' width=25 height=20>";  
                        }  
                        else  
                        {  
                            di = (DirectoryInfo)fsi;  
                            newfile.fileName = di.Name;  
                            newfile.fileSize = DirSize(di);//調(diào)用計算文件夾大小的方法  
                            newfile.FileModify = di.LastWriteTime;  
                            newfile.picName = "<img src='directory.gif' width=25 height=20>";  
                        }  
                        fileList.Add(newfile);  
                    }  
                    catch (Exception e) { }  
                }  
                return fileList;  
      
            }  
      
      
            /// <summary>  
            /// 顯示目錄和文件  
            /// </summary>  
            /// <param name="path"></param>  
            public static void All(string path)  
            {  
                FileInfo fi;//文件  
                DirectoryInfo di;//目錄  
                DirectoryInfo dir=null;  
                int i = 0; //控制行的顏色  
                try  
                {  
                    dir = new DirectoryInfo(path);  
                }  
                catch (Exception e) { }  
                foreach (FileSystemInfo fsi in dir.GetFileSystemInfos())  
                {  
                    try  
                    {  
                        fileEntity newfile = new fileEntity();  
                        FolderEntity folder = new FolderEntity();  
                        newfile.fileName = "";  
                        newfile.picName = "";  
                        newfile.fileExt = "";  
                        newfile.fileSize = 0;  
                        folder.folderName = "";  
                        folder.picName = "";  
      
                        i += 1;  
                        if (fsi is FileInfo)//判斷當前讀取的是不是一個文件  
                        {  
                            //表示當前fsi是文件  
                            fi = (FileInfo)fsi;  
                            newfile.fileName = fi.Name;  
                            newfile.fileExt = fi.Extension;  
                            newfile.fileSize = fi.Length;  
                            newfile.FileModify = fi.LastWriteTime;  
      
                            //將文件加上可以下載文件的鏈接  
      
      
                            newfile.fileName = "<a href='........'></a>";  
      
      
                            //通過擴展名來選擇文件顯示圖標  
      
                            //Response.Write(Session["DataBasePath"].ToString()+"\\filetype\\"+pfun.getFileExt(FileExt)+".gif");  
      
                            if (fsi.Exists)  
                            {  
                                switch (newfile.fileExt)  
                                {  
                                    case ".gif":  
                                        newfile.picName = "gif.gif";  
                                        break;  
                                    default:  
                                        newfile.picName = "other.gif";  
                                        break;  
                                }  
                            }  
                            else  
                            {  
                                newfile.picName = "unknown.gif";  
                            }  
      
      
                            /* 
                            switch(FileExt) 
                            { 
                                case ".gif": 
                                    FilePic = "gif.gif"; 
                                    break; 
                                default: 
                                    FilePic = "other.gif"; 
                                    break; 
                            } 
                            */  
      
                            newfile.picName = "<img src='filetype/" + newfile.picName + "' width=16 height=16>";  
      
                        }  
                        else  
                        {  
                            //當前為目錄  
                            di = (DirectoryInfo)fsi;  
                            folder.folderName = di.Name;  
      
                            //給目錄加上鏈接  
      
                            folder.folderName = "<a href='.......'><a>";  
                            folder.lastTime = di.LastWriteTime;  
                            folder.picName = "<img src='filetype/folder.gif' width=16 height=16>";  
      
                        }  
                    }catch(Exception e){}  
                }  
      
      
            }  
        }  
    }  

標簽: isp

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:Boyer-Moore算法java實現(xiàn)

下一篇:c#xml文檔操作類(2)