Ejemplo n.º 1
0
        /// <summary>取得图片文件的宽高</summary>
        /// <param name="srcFile"></param>
        /// <param name="ww">取得图片的宽度</param>
        /// <param name="hh">取得图片的高度</param>
        /// <returns></returns>
        public static bool Get_Pic_WW_HH(string srcFile, out int ww, out int hh)
        {
            string sExt = DirFileHelper.GetFileExtension(srcFile).ToLower();

            if (sExt == "gif" || sExt == "jpg" || sExt == "jpeg" || sExt == "bmp" || sExt == "png")
            {
                if (srcFile.IndexOf(":") < 0)
                {
                    srcFile = DirFileHelper.GetMapPath(srcFile);
                }
                if (DirFileHelper.IsExistFile(srcFile))
                {
                    try
                    {
                        Image testImage = Image.FromFile(srcFile);
                        ww = testImage.Width;
                        hh = testImage.Height;
                        testImage.Dispose();
                        return(true);
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                }
            }

            ww = 0;
            hh = 0;
            return(false);
        }
Ejemplo n.º 2
0
        /// <summary>在图片上生成图片水印</summary>
        /// <param name="srcFile">要添加水印的图片</param>
        /// <param name="top">上边距 为0时下边距生效</param>
        /// <param name="bottom">下边距 上边距为0时生效</param>
        /// <param name="left">左边距 为0时右边距生效</param>
        /// <param name="right">右边距 左边距为0时生效</param>
        /// <param name="limitWidth">原图小于该宽度,将不添加水印</param>
        /// <param name="HighQuality">如果大于0,使用质量控制(50-100)</param>
        /// <param name="_WaterPicPath">水印图片所在地址,默认:"/images/Water.png"</param>
        public static void MakeWaterPic(string srcFile, int top = 0, int bottom = 10, int left = 0, int right = 10, int limitWidth = 300, int HighQuality = 0, string _WaterPicPath = "/images/Water.png")
        {
            #region 取得图片绝对地址
            if (srcFile.IndexOf(":") < 0)
            {
                srcFile = DirFileHelper.GetMapPath(srcFile);
            }
            if (!DirFileHelper.IsExistFile(srcFile))
            {
                return;
            }
            #endregion

            #region 取得水印图片
            if (_WaterPicPath.IndexOf(":") < 0)
            {
                _WaterPicPath = DirFileHelper.GetMapPath(_WaterPicPath);
            }
            if (!DirFileHelper.IsExistFile(_WaterPicPath))
            {
                return;
            }
            #endregion

            //---------------------------------------
            #region 判断
            Image srcImage = Image.FromFile(srcFile);
            Image watImage = Image.FromFile(_WaterPicPath);

            //水印图大于原图或原图小于300,不加水印
            if (watImage.Width > srcImage.Width || srcImage.Width < limitWidth)
            {
                srcImage.Dispose();
                watImage.Dispose();
                return;
            }
            #endregion

            //---------------------------------------
            #region 位置
            int x = 0, y = 0;
            if (top != 0)
            {
                y = top;
                if (left != 0)
                {
                    x = left;
                }
                else
                {
                    x = srcImage.Width - watImage.Width - right;
                }
            }
            else if (bottom != 0)
            {
                y = srcImage.Height - watImage.Height - bottom;
                if (left != 0)
                {
                    x = left;
                }
                else
                {
                    x = srcImage.Width - watImage.Width - right;
                }
            }
            else
            {
                //居中
                x = srcImage.Width / 2 - watImage.Width / 2;
                y = srcImage.Height / 2 - watImage.Height / 2;
            }
            #endregion

            //---------------------------------------
            #region 保存
            Bitmap   bitmap = new Bitmap(srcImage);
            Graphics g      = Graphics.FromImage(bitmap);
            g.DrawImage(watImage, new Rectangle(x, y, watImage.Width, watImage.Height), 0, 0, watImage.Width, watImage.Height, GraphicsUnit.Pixel);
            srcImage.Dispose();
            watImage.Dispose();
            g.Dispose();

            try
            {
                if (DirFileHelper.GetFileExtension(srcFile) == "png")
                {
                    bitmap.Save(srcFile, System.Drawing.Imaging.ImageFormat.Png);
                }
                else//以jpg格式保存缩略图
                {
                    //---------------------------------------
                    //高质量--使用分级图片质量
                    if (HighQuality > 0)
                    {
                        // 以下代码为保存图片时,设置压缩质量
                        EncoderParameters encoderParams = new EncoderParameters();
                        long[]            quality       = new long[1];
                        quality[0] = HighQuality;   //50-100内
                        EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
                        encoderParams.Param[0] = encoderParam;

                        //获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象.
                        ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
                        ImageCodecInfo   jpegICI  = null;
                        for (int i = 0; i < arrayICI.Length; i++)
                        {
                            if (arrayICI[i].FormatDescription.Equals("JPEG"))
                            {
                                jpegICI = arrayICI[i];//设置JPEG编码
                                break;
                            }
                        }

                        //---------------------------------------
                        bitmap.Save(srcFile, jpegICI, encoderParams);
                    }
                    else
                    {
                        bitmap.Save(srcFile, System.Drawing.Imaging.ImageFormat.Jpeg);
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                bitmap.Dispose();
            }
            #endregion
        }
Ejemplo n.º 3
0
        /// <summary>生成缩略图</summary>
        /// <param name="srcFile">原图片路径(服务器路径d:\web\upload\100.jpg)</param>
        /// <param name="decFile">缩略图路径(服务器路径d:\web\upload\100.jpg)</param>
        /// <param name="iMaxWidth">限制的宽度</param>
        /// <param name="iMaxHeight">限制的高度</param>
        /// <param name="highQuality">如果大于0,使用质量控制(50-100)</param>
        /// <param name="CutType">0=按比例生成宽高,1=固定图片宽高,2=固定背景宽高,图片按比例生成</param>
        public static void MakeThumbImage(string srcFile, string decFile, int iMaxWidth, int iMaxHeight, int highQuality = 0, int CutType = 0)
        {
            #region 取得路径
            if (srcFile.IndexOf(":") < 0)
            {
                srcFile = DirFileHelper.GetMapPath(srcFile);
            }
            if (decFile.IndexOf(":") < 0)
            {
                decFile = DirFileHelper.GetMapPath(decFile);
            }
            if (!DirFileHelper.IsExistFile(srcFile))
            {
                return;
            }
            #endregion

            //---------------------------------------
            #region 原始图片宽高
            Image srcImage   = Image.FromFile(srcFile);
            int   iSrcWidth  = srcImage.Width;
            int   iSrcHeight = srcImage.Height;
            #endregion

            //---------------------------------------
            #region 读取新图片宽高
            int toWidth = 0, toHeight = 0, x = 0, y = 0;

            if (CutType == 1)//固定宽高
            {
                toWidth  = iMaxWidth;
                toHeight = iMaxHeight;
            }
            else
            {
                //如果原始图,高度大于宽度,按高度缩放,如果宽度大高度按宽度缩放(不变形)
                //如果图片缩放后,还是比限制大,再进行缩放,直接宽同度都不超过限制
                if ((iSrcWidth < iMaxWidth) && (iSrcHeight < iMaxHeight))
                {
                    toWidth  = iSrcWidth;
                    toHeight = iSrcHeight;
                }
                else
                {
                    if (iSrcHeight > iSrcWidth)
                    {
                        toHeight = iMaxHeight;
                        toWidth  = iSrcWidth * iMaxHeight / iSrcHeight;

                        if (toWidth > iMaxWidth)
                        {
                            //toHeight 必须在 toWidth 前
                            toHeight = toHeight * iMaxWidth / toWidth;
                            toWidth  = iMaxWidth;
                        }
                    }
                    else
                    {
                        toHeight = iSrcHeight * iMaxWidth / iSrcWidth;
                        toWidth  = iMaxWidth;

                        if (toHeight > iMaxHeight)
                        {
                            //toWidth 必须在 toHeight 前
                            toWidth  = toWidth * iMaxHeight / toHeight;
                            toHeight = iMaxHeight;
                        }
                    }
                }
            }
            #endregion

            #region 输出
            Bitmap bitmap;
            if (CutType == 2)
            {
                //2=固定背景宽高,图片按比例生成
                bitmap = new Bitmap(iMaxWidth, iMaxHeight);
                if (toWidth <= iMaxWidth && toHeight <= iMaxHeight)
                {
                    x = Convert.ToInt16((iMaxWidth - toWidth) / 2);
                    y = Convert.ToInt16((iMaxHeight - toHeight) / 2);
                }
                else
                {
                    if (toWidth < toHeight)
                    {
                        x = Convert.ToInt16((iMaxWidth - toWidth) / 2);
                    }
                    else
                    {
                        y = Convert.ToInt16((iMaxHeight - toHeight) / 2);
                    }
                }
            }
            else
            {
                bitmap = new Bitmap(toWidth, toHeight);
            }
            Graphics g = Graphics.FromImage(bitmap);

            //高质量
            //g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;

            //设置高质量插值法
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

            //设置高质量,低速度呈现平滑程度
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

            //清空画布并以透明背景色填充
            //g.Clear(System.Drawing.Color.Transparent);
            g.Clear(System.Drawing.Color.White);

            //在指定位置并且按指定大小绘制原图片的指定部分
            g.DrawImage(srcImage, new Rectangle(x, y, toWidth, toHeight), new Rectangle(0, 0, iSrcWidth, iSrcHeight), GraphicsUnit.Pixel);

            srcImage.Dispose();
            g.Dispose();

            try
            {
                if (DirFileHelper.GetFileExtension(srcFile) == "png")
                {
                    bitmap.Save(decFile, System.Drawing.Imaging.ImageFormat.Png);
                }
                else//以jpg格式保存缩略图
                {
                    //---------------------------------------
                    //高质量--使用分级图片质量
                    if (highQuality > 0)
                    {
                        // 以下代码为保存图片时,设置压缩质量
                        var encoderParams = new EncoderParameters();
                        var quality       = new long[1];
                        quality[0] = highQuality;   //50-100内
                        var encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);
                        encoderParams.Param[0] = encoderParam;

                        //获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象.
                        ImageCodecInfo[] arrayIci = ImageCodecInfo.GetImageEncoders();
                        ImageCodecInfo   jpegIci  = null;
                        for (int i = 0; i < arrayIci.Length; i++)
                        {
                            if (arrayIci[i].FormatDescription.Equals("JPEG"))
                            {
                                jpegIci = arrayIci[i];
                                //设置JPEG编码
                                break;
                            }
                        }

                        //---------------------------------------
                        bitmap.Save(decFile, jpegIci, encoderParams);
                    }
                    else
                    {
                        bitmap.Save(decFile, System.Drawing.Imaging.ImageFormat.Jpeg);
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                bitmap.Dispose();
            }
            #endregion
        }