C#圧縮ピクチャアルゴリズム

6882 ワード

using System.IO;  
using System.Drawing;  
using System.Drawing.Imaging;  
using System;  
namespace Bll  
{  
    /// <summary>  
    ///        
    /// 1、                       
    /// 2、                 
    /// </summary>  
    public class ImageHepler  
    {  
        public Image ResourceImage, ReducedImage;  
        private int ImageWidth;  
        private int ImageHeight;  
        public string ErrMessage;  
  
        /// <summary>  
        ///         
        /// </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">          ,(   ),    :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">          ,(   ),    :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;  
            }  
        }  
    }  
}  

原作者不明http://www.open-open.com/lib/view/open1391348644910.html】