日期:2014-05-18  浏览次数:21147 次

C# 这个方法截取后图片为什么会比原图还大?
截图是缩小截取的,可压缩后比原图还要大,大家帮我看下这个方法
C# code

 /// <summary>
       /// 方法入口
       /// </summary>
       /// <param name="strOldPic">源图文件名(包括路径)</param>
       /// <param name="strNewPic">缩小后保存为文件名(包括路径)</param>
       /// <param name="intWidth">缩小至宽度</param>
       /// <param name="CropHeight">裁剪高度</param>
       /// <param name="AQuality">图片保存质量(1-100之间)</param>
       public string SmallPic1(string strOldPic, string strNewPic, float intWidth, int CropHeight, int AQuality)
        {
            System.Drawing.Bitmap objPic, objNewPic;

            try
            {
                
                objPic = new System.Drawing.Bitmap(strOldPic);
                float ImgWidth = objPic.Width;//获取图宽度
                float ImgHeight = objPic.Height;
                float intHeight = (intWidth / ImgWidth) * ImgHeight; //计算出相同比例高度 
                int ConfirmHeight = Convert.ToInt32(intHeight);
                int Width = Convert.ToInt32(intWidth);
                objNewPic = new System.Drawing.Bitmap(objPic, Width, ConfirmHeight);// 生成流                 
                return DoConvert(objNewPic, strNewPic, objNewPic.Width, CropHeight, AQuality);//裁剪并保存
            
            }
            catch (Exception exp) { error.LogError("按比例裁剪图片,自动计算高度 出错:", exp); return "压缩失败"; }
            finally
            {
                objPic = null;
                objNewPic = null;
            }
        }



 /// <summary>   
        /// 裁剪并缩放   
        /// </summary>   
        /// <param name="ASrcFileName">源图片信息</param>   
        /// <param name="ADestFileName">目标文件名称</param>   
        /// <param name="AWidth">转换后的宽度(像素)</param>   
        /// <param name="AHeight">转换后的高度(像素)</param>   
        /// <param name="AQuality">保存质量(取值在1-100之间)</param>   
       public  string DoConvert(Bitmap ASrcFileName, string ADestFileName, int AWidth, int AHeight, int AQuality)
        {
            try
            {

                Bitmap ASrcImg = ASrcFileName;//赋值图片信息               
                //Image ASrcImg = Image.FromFile(ASrcFileName);//获取图片信息

                //if (ASrcImg.Width <= AWidth && ASrcImg.Height <= AHeight)
                //{//图片的高宽均小于目标高宽,直接保存   
                //    ASrcImg.Save(ADestFileName);
                //    return;
                //}
                double ADestRate = AWidth * 1.0 / AHeight;              
                double ASrcRate = ASrcImg.Width * 1.0 / ASrcImg.Height;                
                //裁剪后的宽度   
                double ACutWidth = ASrcRate > ADestRate ? (ASrcImg.Height * ADestRate) : ASrcImg.Width;               
                //裁剪后的高度   
                double ACutHeight = ASrcRate > ADestRate ? ASrcImg.Height : (ASrcImg.Width / ADestRate);                
                //待裁剪的矩形区域,根据原图片的中心进行裁剪   
                Rectangle AFromRect = new Rectangle(Convert.ToInt32((ASrcImg.Width - ACutWidth) / 2), Convert.ToInt32((ASrcImg.Height - ACutHeight) / 2), (int)ACutWidth, (int)ACutHeight);
               
                //目标矩形区域   
                Rectangle AToRect = new Rectangle(0, 0, AWidth, AHeight);   
                Image ADestImg = new Bitmap(AWidth, AHeight);              
                Graphics ADestGraph = Graphics.FromImage(ADestImg);               
               ADestGraph.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;              
                ADestGraph.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;               
                ADestGraph.DrawImage(ASrcImg, AToRect, AFromRect, Graphi