Esempio n. 1
0
        /// <summary>
        /// 等比例缩放,不留白
        /// </summary>
        /// <param name="strOldPic"></param>
        /// <param name="strNewPic"></param>
        /// <param name="inputWidth"></param>
        /// <param name="inputHeight"></param>
        /// <returns></returns>
        public static bool ZoomSmallPic2(string strOldPic, string strNewPic, int inputWidth, int inputHeight)
        {
            Bitmap objPic, objNewPic, outBitMap;

            try
            {
                objPic = new Bitmap(strOldPic);

                int imageWidth  = objPic.Width;
                int imageHeight = objPic.Height;


                if (imageWidth > inputWidth)
                {
                    imageHeight = ParseHelper.ToInt((double)inputWidth / (double)imageWidth * imageHeight);
                    imageWidth  = inputWidth;
                }
                if (imageHeight > inputHeight)
                {
                    imageWidth  = ParseHelper.ToInt((double)inputHeight / (double)imageHeight * imageWidth);
                    imageHeight = inputHeight;
                }

                //缩放好的图片
                objNewPic = new Bitmap(objPic, imageWidth, imageHeight);


                //检测目录是否存在
                var saveDir = Path.GetDirectoryName(strNewPic);

                if (saveDir != null && !Directory.Exists(saveDir))
                {
                    Directory.CreateDirectory(saveDir);
                }

                string mergeImageExt = Path.GetExtension(strNewPic);
                if (mergeImageExt != null && mergeImageExt.Contains("png"))
                {
                    objNewPic.Save(strNewPic, ImageFormat.Png);
                }
                else if (mergeImageExt != null && mergeImageExt.Contains("gif"))
                {
                    objNewPic.Save(strNewPic, ImageFormat.Gif);
                }
                else
                {
                    objNewPic.Save(strNewPic, ImageFormat.Jpeg);
                }



                objPic.Dispose();
                objNewPic.Dispose();
            }
            catch (Exception exp)
            {
                throw exp;
            }
            finally
            {
                objPic    = null;
                objNewPic = null;
                outBitMap = null;
            }

            return(true);
        }
Esempio n. 2
0
        /// <summary>
        /// 给图片添加文字水印
        /// </summary>
        /// <param name="filePath">图片完整服务器路径</param>
        /// <param name="text">水印文字</param>
        /// <param name="rate"></param>
        public static void AddText(string filePath, string text, double rate = 1.0)
        {
            if (string.IsNullOrEmpty(text))
            {
                return;
            }

            Image    image = Image.FromFile(filePath);
            Graphics g     = Graphics.FromImage(image);

            //将图片绘制到graphics中
            g.DrawImage(image, 0, 0, image.Width, image.Height);

            //文字的起始位置,x,y坐标,计算坐标
            int fontHeight = ParseHelper.ToInt(50 * rate);

            var       family    = new FontFamily("微软雅黑");
            const int fontStyle = (int)FontStyle.Regular;


            int emSize = ParseHelper.ToInt(40 * rate);

            int x = image.Width / 2 - text.Length * emSize / 2;
            int y = image.Height / 2 - fontHeight / 2;

            if (x < 0)
            {
                x = 0;
            }

            var          origin = new Point(x, y);
            StringFormat format = StringFormat.GenericDefault;

            //设置字体的颜色
            //Brush bFont = new SolidBrush(Color.FromArgb(214, 203, 199));
            Brush bFont = new SolidBrush(Color.WhiteSmoke);

            //描边
            var myPath = new GraphicsPath();

            myPath.AddString(text, family, fontStyle, emSize, origin, format);
            g.FillPath(bFont, myPath);

            //写字(0表示没有,2是以前的参数)
            //g.DrawPath(new Pen(Color.Black, 2), myPath);
            g.DrawPath(new Pen(Color.WhiteSmoke, 0), myPath);

            //释放graphics
            g.Dispose();

            //确定新图片的文件路径
            var oldfileName = Path.GetFileNameWithoutExtension(filePath);
            var tempPath    = filePath;

            if (oldfileName != null)
            {
                string newpath = filePath.Replace(oldfileName, "img_X");
                //保存写上字的图片
                image.Save(newpath);
                //释放image
                image.Dispose();
                //删除没加水印的图片,记得一定要放在image释放之后,否则无法删除
                File.Delete(filePath);
                //重命名
                File.Move(newpath, tempPath);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 按比例缩小图片,自动计算宽度
        /// </summary>
        /// <param name="strOldPic">源图文件名(包括路径)</param>
        /// <param name="strNewPic">缩小后保存为文件名(包括路径)</param>
        /// <param name="inputWidth">缩小到宽度</param>
        /// <param name="inputHeight">缩小至高度</param>
        public static bool ZoomSmallPic(string strOldPic, string strNewPic, int inputWidth, int inputHeight)
        {
            Bitmap objPic, objNewPic, outBitMap;

            try
            {
                objPic = new Bitmap(strOldPic);

                int imageWidth  = objPic.Width;
                int imageHeight = objPic.Height;


                if (imageWidth > inputWidth)
                {
                    imageHeight = ParseHelper.ToInt((double)inputWidth / (double)imageWidth * imageHeight);
                    imageWidth  = inputWidth;
                }
                if (imageHeight > inputHeight)
                {
                    imageWidth  = ParseHelper.ToInt((double)inputHeight / (double)imageHeight * imageWidth);
                    imageHeight = inputHeight;
                }

                //缩放好的图片
                objNewPic = new Bitmap(objPic, imageWidth, imageHeight);

                if (inputWidth == int.MaxValue || inputHeight == int.MaxValue)
                {
                    inputWidth  = objPic.Width;
                    inputHeight = objPic.Height;
                }


                //放到图片的中心位置
                outBitMap = new Bitmap(inputWidth, inputHeight);//输出的图片

                Graphics grap = Graphics.FromImage(outBitMap);
                grap.Clear(Color.White);
                grap.CompositingQuality = CompositingQuality.HighSpeed;

                int newPositonX = (inputWidth - imageWidth) / 2;
                int newPositonY = (inputHeight - imageHeight) / 2;

                grap.DrawImage(objNewPic, newPositonX, newPositonY);

                //检测目录是否存在
                var saveDir = Path.GetDirectoryName(strNewPic);

                if (saveDir != null && !Directory.Exists(saveDir))
                {
                    Directory.CreateDirectory(saveDir);
                }

                string mergeImageExt = Path.GetExtension(strNewPic);
                if (mergeImageExt != null && mergeImageExt.Contains("png"))
                {
                    outBitMap.Save(strNewPic, ImageFormat.Png);
                }
                else if (mergeImageExt != null && mergeImageExt.Contains("gif"))
                {
                    outBitMap.Save(strNewPic, ImageFormat.Gif);
                }
                else
                {
                    outBitMap.Save(strNewPic, ImageFormat.Jpeg);
                }



                grap.Dispose();

                objPic.Dispose();
                objNewPic.Dispose();
                outBitMap.Dispose();
            }
            catch (Exception exp)
            {
                throw exp;
            }
            finally
            {
                objPic    = null;
                objNewPic = null;
                outBitMap = null;
            }

            return(true);
        }