网上搜集的一个比较实用的图片操作类

namespace System.Images.ImagesHelper
{
    /**//// <summary>
    ///ImgOperate 的摘要说明
    /// </summary>
    public class ImagesHelper
    {
        图片处理类构造方法#region 图片处理类构造方法
        /**//// <summary>
        /// 图片处理类构造方法
        /// </summary>
        public ImagesHelper()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }
        #endregion
        用于缩小图片#region 用于缩小图片
        //************************************************************//
        //下面给出三个简单的方法,后面两个方法是扩展
        //************************************************************//
        /**//// <summary>
        ///用于缩小图片
        /// </summary>
        /// <param name="strOldPic">原始图片路径</param>
        /// <param name="strNewPic">新图片路径</param>
        /// <param name="intWidth">新图片宽度</param>
        /// <param name="intHeight">新图片高度</param>
        /// <param name="isDeleteOldPic">是否删除原始图片</param>
        public void ReducePic(string strOldPic, string strNewPic, int intWidth, int intHeight, bool isDeleteOldPic)
        {
            System.Drawing.Bitmap objPic, objNewPic;
            try
            {
                objPic = new System.Drawing.Bitmap(strOldPic);
                objNewPic = new System.Drawing.Bitmap(objPic, intWidth, intHeight);
                objNewPic.Save(strNewPic);
                objPic.Dispose();
                objNewPic.Dispose();
                if (isDeleteOldPic)
                {
                    if (File.Exists(strOldPic))
                    {
                        File.Delete(strOldPic);
                    }
                }

            }
            catch (Exception exp) { throw exp; }
            finally
            {
                objPic = null;
                objNewPic = null;
            }
        }
        /**//// <summary>
        /// 将图片缩放至指定类型指定值
        /// </summary>
        /// <param name="strOldPic">原始图片路径</param>
        /// <param name="strNewPic">新图片路径</param>
        /// <param name="intValue">指定值</param>
        /// <param name="type">缩放类型(W/w为指定宽度,H/h为指定高度)</param>
        /// <param name="isDeleteOldPic">是否删除原始图片</param>
        public  void ReducePic(string strOldPic, string strNewPic, int intValue, string type, bool isDeleteOldPic)
        {
            System.Drawing.Bitmap objPic, objNewPic;
            int intWidth = 0;
            int intHeight = 0;
            try
            {
                objPic = new System.Drawing.Bitmap(strOldPic);
                if (type == "W" || type == "w")
                {
                    intWidth = intValue;
                    intHeight = Convert.ToInt32(((intWidth * 1.0) / (objPic.Width * 1.0)) * objPic.Height);
                }
                else if (type == "H" || type == "h")
                {
                    intHeight = intValue;
                    intWidth = Convert.ToInt32(((intHeight * 1.0) / (objPic.Height * 1.0)) * objPic.Width);
                }
                else
                {
                    throw new Exception("未指定图片缩放类型");
                }
                objNewPic = new System.Drawing.Bitmap(objPic, intWidth, intHeight);
                objNewPic.Save(strNewPic);
                objPic.Dispose();
                objNewPic.Dispose();
                if (isDeleteOldPic)
                {
                    if (File.Exists(strOldPic))
                    {
                        File.Delete(strOldPic);
                    }
                }

            }
            catch (Exception exp) { throw exp; }
            finally
            {
                objPic = null;
                objNewPic = null;
            }
        }
        /**//// <summary>
        /// 用于缩放图片
        /// </summary>
        /// <param name="strOldPic">原始图片路径</param>
        /// <param name="strNewPic">新图片路径</param>
        /// <param name="balance">缩放比例</param>
        /// <param name="isDeleteOldPic">是否删除原始图片</param>
        public  void ReducePic(string strOldPic, string strNewPic, double balance, bool isDeleteOldPic)
        {
            System.Drawing.Bitmap objPic, objNewPic;
            try
            {
                objPic = new System.Drawing.Bitmap(strOldPic);
                int intWidth = Convert.ToInt32((objPic.Width) * balance);
                int intHeight = Convert.ToInt32((objPic.Height) * balance);
                objNewPic = new System.Drawing.Bitmap(objPic, intWidth, intHeight);
                objNewPic.Save(strNewPic);
                objPic.Dispose();
                objNewPic.Dispose();
                if (isDeleteOldPic)
                {
                    if (File.Exists(strOldPic))
                    {
                        File.Delete(strOldPic);
                    }
                }

            }
            catch (Exception exp) { throw exp; }
            finally
            {
                objPic = null;
                objNewPic = null;
            }
        }
        /**//// <summary>
        /// 获取图片指定部分
        /// </summary>
        /// <param name="pPath">图片路径</param>
        /// <param name="pSavePath">保存路径</param>
        /// <param name="pPartStartPointX">目标图片开始绘制处的坐标X值(通常为)</param>
        /// <param name="pPartStartPointY">目标图片开始绘制处的坐标Y值(通常为)</param>
        /// <param name="pPartWidth">目标图片的宽度</param>
        /// <param name="pPartHeight">目标图片的高度</param>
        /// <param name="pOrigStartPointX">原始图片开始截取处的坐标X值</param>
        /// <param name="pOrigStartPointY">原始图片开始截取处的坐标Y值</param>
        public void ReducePic(string pPath, string pSavedPath, int pPartStartPointX, int pPartStartPointY, int pPartWidth, int pPartHeight, int pOrigStartPointX, int pOrigStartPointY)
        {
            System.Drawing.Image originalImg = System.Drawing.Image.FromFile(pPath);
            Bitmap partImg = new Bitmap(pPartWidth, pPartHeight);
            Graphics graphics = Graphics.FromImage(partImg);
            Rectangle destRect = new Rectangle(new Point(pPartStartPointX, pPartStartPointY), new Size(pPartWidth, pPartHeight));//目标位置
            Rectangle origRect = new Rectangle(new Point(pOrigStartPointX, pOrigStartPointY), new Size(pPartWidth, pPartHeight));//原图位置(默认从原图中截取的图片大小等于目标图片的大小)
            graphics.DrawImage(originalImg, destRect, origRect, GraphicsUnit.Pixel);
            partImg.Save(pSavedPath, ImageFormat.Jpeg);
        }
        #endregion
        添加水印文字#region 添加水印文字
        /**//// <summary>
        /// 为上传的图片添加水印文字
        /// </summary>
        /// <param name="Path">原始图片路径</param>
        /// <param name="Path_sy">水印图片路径</param>
        /// <param name="WaterText">水印文字</param>
        public  void AddWaterText(string Path, string Path_sy, string WaterText)
        {
            string addText = WaterText;
            System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
            g.DrawImage(image, 0, 0, image.Width, image.Height);
            System.Drawing.Font f = new System.Drawing.Font("Verdana", 10);
            System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.LightSlateGray);
            g.DrawString(addText, f, b, image.Width - 100, image.Height - 20);
            g.Dispose();
            image.Save(Path_sy);
            image.Dispose();
        }
        #endregion
        为图片增加水印图片#region 为图片增加水印图片
        /**//// <summary>
        /// 在图片上生成图片水印
        /// </summary>
        /// <param name="Path">原服务器图片路径</param>
        /// <param name="Path_syp">生成的带图片水印的图片路径</param>
        /// <param name="Path_sypf">水印图片路径</param>
        public  void AddWaterPic(string Path, string Path_syp, string Path_sypf)
        {
            System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
            System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Path_sypf);
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);
            g.DrawImage(copyImage, new System.Drawing.Rectangle(image.Width - copyImage.Width, image.Height - copyImage.Height, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, System.Drawing.GraphicsUnit.Pixel);
            g.Dispose();
            image.Save(Path_syp);
            image.Dispose();
        }
        #endregion
        获取图片的高度和宽度#region 获取图片的高度和宽度
        /**//// <summary>
        /// 获取图片的高度和宽度
        /// </summary>
        /// <param name="imgurl">img图片的地址</param>
        /// <returns></returns>
        public int[] GetImgWidth(string imgurl)
        {
            System.Drawing.Image image;
            image = System.Drawing.Image.FromFile(imgurl);
            int imgwidth = image.Width;
            int imgHeight = image.Height;
            int[] img = { imgwidth, imgHeight };
            return img;
        }
        #endregion
        压缩图片#region 压缩图片
        /**//// <summary>
        /// 对图片进行压缩
        /// </summary>
        /// <param name="filePath">原始图片路径</param>
        /// <param name="filePath_ystp">压缩后的图片路径</param>
        public void DensityPic(string filePath, string filePath_ystp) //压缩图片 
        {
            Bitmap bmp = new Bitmap(filePath);
            //ImageCoderInfo 
            ImageCodecInfo ici = null;
            //Encoder 
            System.Drawing.Imaging.Encoder ecd = null;
            //EncoderParameter 
            EncoderParameter ept = null;
            //EncoderParameters 
            EncoderParameters eptS = null;
            try
            {
                ici = this.getImageCoderInfo("image/jpeg");
                ecd = System.Drawing.Imaging.Encoder.Quality;
                eptS = new EncoderParameters(1);
                ept = new EncoderParameter(ecd, 10L);
                eptS.Param[0] = ept;
                bmp.Save(filePath_ystp, ici, eptS);
            } 


            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                bmp.Dispose();
                ept.Dispose();
                eptS.Dispose();
            }
        }
        private ImageCodecInfo getImageCoderInfo(string coderType)// 获取图片编码类型信息 
        {
            ImageCodecInfo[] iciS = ImageCodecInfo.GetImageEncoders();
            ImageCodecInfo retIci = null;
            foreach (ImageCodecInfo ici in iciS)
            {
                if (ici.MimeType.Equals(coderType))
                    retIci = ici;
            }

            return retIci;
        }
        #endregion
        获取图片的指定部分#region 获取图片的指定部分
        /**//**/
        /**//// <summary>
        /// 获取图片指定部分
        /// </summary>
        /// <param name="pPath">图片路径</param>
        /// <param name="pSavePath">保存路径</param>
        /// <param name="pPartStartPointX">目标图片开始绘制处的坐标X值(通常为)</param>
        /// <param name="pPartStartPointY">目标图片开始绘制处的坐标Y值(通常为)</param>
        /// <param name="pPartWidth">目标图片的宽度</param>
        /// <param name="pPartHeight">目标图片的高度</param>
        /// <param name="pOrigStartPointX">原始图片开始截取处的坐标X值</param>
        /// <param name="pOrigStartPointY">原始图片开始截取处的坐标Y值</param>
        /// <param name="pFormat">保存格式,通常可以是jpeg</param>
        public void GetPart(string pPath, string pSavedPath, int pPartStartPointX, int pPartStartPointY, int pPartWidth, int pPartHeight, int pOrigStartPointX, int pOrigStartPointY)
        {
            string normalJpgPath = pSavedPath;

            using (System.Drawing.Image originalImg = System.Drawing.Image.FromFile(pPath))
            {
                Bitmap partImg = new Bitmap(pPartWidth, pPartHeight);
                Graphics graphics = Graphics.FromImage(partImg);
                Rectangle destRect = new Rectangle(new Point(pPartStartPointX, pPartStartPointY), new Size(pPartWidth, pPartHeight));//目标位置
                Rectangle origRect = new Rectangle(new Point(pOrigStartPointX, pOrigStartPointY), new Size(pPartWidth, pPartHeight));//原图位置(默认从原图中截取的图片大小等于目标图片的大小)


                /**////文字水印  
                System.Drawing.Graphics G = System.Drawing.Graphics.FromImage(partImg);
                System.Drawing.Font f = new Font("Lucida Grande", 6);
                System.Drawing.Brush b = new SolidBrush(Color.Gray);
                G.Clear(Color.White);
                graphics.DrawImage(originalImg, destRect, origRect, GraphicsUnit.Pixel);
                G.DrawString("TianTang.Com", f, b, 0, 0);
                G.Dispose();

                originalImg.Dispose();
                if (File.Exists(normalJpgPath))
                {
                    File.SetAttributes(normalJpgPath, FileAttributes.Normal);
                    File.Delete(normalJpgPath);
                }
                partImg.Save(normalJpgPath, ImageFormat.Jpeg);
            }
        }
        #endregion
        获取图片的指定部分#region 获取图片的指定部分
        /**//**/
        /**//// <summary>
        /// 获取按比例缩放的图片指定部分
        /// </summary>
        /// <param name="pPath">图片路径</param>
        /// <param name="pSavePath">保存路径</param>
        /// <param name="pPartStartPointX">目标图片开始绘制处的坐标X值(通常为)</param>
        /// <param name="pPartStartPointY">目标图片开始绘制处的坐标Y值(通常为)</param>
        /// <param name="pPartWidth">目标图片的宽度</param>
        /// <param name="pPartHeight">目标图片的高度</param>
        /// <param name="pOrigStartPointX">原始图片开始截取处的坐标X值</param>
        /// <param name="pOrigStartPointY">原始图片开始截取处的坐标Y值</param>
        /// <param name="imageWidth">缩放后的宽度</param>
        /// <param name="imageHeight">缩放后的高度</param>
        public void GetPart(string pPath, string pSavedPath, int pPartStartPointX, int pPartStartPointY, int pPartWidth, int pPartHeight, int pOrigStartPointX, int pOrigStartPointY, int imageWidth, int imageHeight)
        {
            string normalJpgPath = pSavedPath;
            using (System.Drawing.Image originalImg = System.Drawing.Image.FromFile(pPath))
            {
                if (originalImg.Width == imageWidth && originalImg.Height == imageHeight)
                {
                    GetPart(pPath, pSavedPath, pPartStartPointX, pPartStartPointY, pPartWidth, pPartHeight, pOrigStartPointX, pOrigStartPointY);
                    return;
                }

                System.Drawing.Image.GetThumbnailImageAbort callback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
                System.Drawing.Image zoomImg = originalImg.GetThumbnailImage(imageWidth, imageHeight, callback, IntPtr.Zero);//缩放
                Bitmap partImg = new Bitmap(pPartWidth, pPartHeight);

                Graphics graphics = Graphics.FromImage(partImg);
                Rectangle destRect = new Rectangle(new Point(pPartStartPointX, pPartStartPointY), new Size(pPartWidth, pPartHeight));//目标位置
                Rectangle origRect = new Rectangle(new Point(pOrigStartPointX, pOrigStartPointY), new Size(pPartWidth, pPartHeight));//原图位置(默认从原图中截取的图片大小等于目标图片的大小)

                /**////文字水印  
                System.Drawing.Graphics G = System.Drawing.Graphics.FromImage(partImg);
                System.Drawing.Font f = new Font("Lucida Grande", 6);
                System.Drawing.Brush b = new SolidBrush(Color.Gray);
                G.Clear(Color.White);

                graphics.DrawImage(zoomImg, destRect, origRect, GraphicsUnit.Pixel);
                G.DrawString("TianTang.Com", f, b, 0, 0);
                G.Dispose();

                originalImg.Dispose();
                if (File.Exists(normalJpgPath))
                {
                    File.SetAttributes(normalJpgPath, FileAttributes.Normal);
                    File.Delete(normalJpgPath);
                }
                partImg.Save(normalJpgPath, ImageFormat.Jpeg);
            }
        }
        public bool ThumbnailCallback()
        {
            return false;
        }
        #endregion
    }
}

posted @ 2010-05-28 23:31  帝都蜗牛  阅读(556)  评论(1编辑  收藏  举报