Example #1
0
        /// <summary>
        /// 设置文字水印
        /// </summary>
        /// <param name="path">图片路径(绝对路径)</param>
        /// <param name="size">字体大小</param>
        /// <param name="letter">水印文字</param>
        /// <param name="color">颜色</param>
        /// <param name="location">水印位置</param>
        /// <returns></returns>
        public static string LetterWatermark(string path, int size, string letter, Color color,
                                             ImageLocationMode location)
        {
            string extName = Path.GetExtension(path);

            if (extName == ".jpg" || extName == ".bmp" || extName == ".jpeg")
            {
                DateTime time     = DateTime.Now;
                string   fileName = "" + time.Year.ToString() + time.Month.ToString() + time.Day.ToString() +
                                    time.Hour.ToString() + time.Minute.ToString() + time.Second.ToString() +
                                    time.Millisecond.ToString();
                Image     img   = Bitmap.FromFile(path);
                Graphics  g     = Graphics.FromImage(img);
                ArrayList coors = GetLocation(location, img, size, letter.Length);
                Font      font  = new Font("宋体", size);
                Brush     br    = new SolidBrush(color);
                g.DrawString(letter, font, br, float.Parse(coors[0].ToString()), float.Parse(coors[1].ToString()));
                g.Dispose();
                string newPath = Path.GetDirectoryName(path) + fileName + extName;
                img.Save(newPath);
                img.Dispose();
                File.Copy(newPath, path, true);
                if (File.Exists(newPath))
                {
                    File.Delete(newPath);
                }
            }
            return(path);
        }
Example #2
0
        /// <summary>
        /// 获取水印位置
        /// </summary>
        /// <param name="location">水印位置</param>
        /// <param name="img">需要添加水印的图片</param>
        /// <param name="waterImg">水印图片</param>
        /// <returns></returns>
        private static ArrayList GetLocation(ImageLocationMode location, Image img, Image waterImg)
        {
            ArrayList coords = new ArrayList();
            int       x      = 0;
            int       y      = 0;

            switch (location)
            {
            case ImageLocationMode.LeftTop:
                x = 10;
                y = 10;
                break;

            case ImageLocationMode.Top:
                x = img.Width / 2 - waterImg.Width / 2;
                y = img.Height - waterImg.Height;
                break;

            case ImageLocationMode.RightTop:
                x = img.Width - waterImg.Width;
                y = 10;
                break;

            case ImageLocationMode.LeftCenter:
                x = 10;
                y = img.Height / 2 - waterImg.Height / 2;
                break;

            case ImageLocationMode.Center:
                x = img.Width / 2 - waterImg.Width / 2;
                y = img.Height / 2 - waterImg.Height / 2;
                break;

            case ImageLocationMode.RightCenter:
                x = img.Width - waterImg.Width;
                y = img.Height / 2 - waterImg.Height / 2;
                break;

            case ImageLocationMode.LeftBottom:
                x = 10;
                y = img.Height - waterImg.Height;
                break;

            case ImageLocationMode.Bottom:
                x = img.Width / 2 - waterImg.Width / 2;
                y = img.Height - waterImg.Height;
                break;

            case ImageLocationMode.RightBottom:
                x = img.Width - waterImg.Width;
                y = img.Height - waterImg.Height;
                break;

            default:
                break;
            }
            coords.Add(x);
            coords.Add(y);
            return(coords);
        }
Example #3
0
        /// <summary>
        /// 获取水印位置
        /// </summary>
        /// <param name="location">水印位置</param>
        /// <returns></returns>
        private static Gravity GetLocation(ImageLocationMode location)
        {
            switch (location)
            {
            case ImageLocationMode.LeftTop:
                return(Gravity.Northwest);

            case ImageLocationMode.Top:
                return(Gravity.North);

            case ImageLocationMode.RightTop:
                return(Gravity.Northeast);

            case ImageLocationMode.RightCenter:
                return(Gravity.East);

            case ImageLocationMode.RightBottom:
                return(Gravity.Southeast);

            case ImageLocationMode.Bottom:
                return(Gravity.South);

            case ImageLocationMode.LeftBottom:
                return(Gravity.Southwest);

            case ImageLocationMode.LeftCenter:
                return(Gravity.West);

            case ImageLocationMode.Center:
                return(Gravity.Center);

            default:
                return(Gravity.Center);
            }
        }
Example #4
0
        /// <summary>
        /// 设置图片水印,使用MagickImage.Net
        /// </summary>
        /// <param name="path">需要加载水印的图片路径(绝对路径)</param>
        /// <param name="waterpath">水印图片(绝对路径)</param>
        /// <param name="location">水印位置</param>
        /// <returns></returns>
        public static string ImageWatermarkByMagick(string path, string waterpath, ImageLocationMode location)
        {
            string extName = Path.GetExtension(path);

            if (string.IsNullOrEmpty(extName))
            {
                return(path);
            }
            extName = extName.ToLower();
            if (!(extName == ".jpg" || extName == ".bmp" || extName == ".jpeg" || extName == ".png"))
            {
                return(path);
            }
            // 读取需要水印的图片
            using (ImageMagick.MagickImage image = new ImageMagick.MagickImage(path))
            {
                // 读取水印图片
                using (ImageMagick.MagickImage watermark = new ImageMagick.MagickImage(waterpath))
                {
                    // 设置水印透明度
                    //watermark.Evaluate(Channels.Alpha, EvaluateOperator.Divide, 7);
                    // 设置绘制水印位置
                    image.Composite(watermark, GetLocation(location), CompositeOperator.Over);
                }
                image.Resize(image.Width, image.Height);
                image.Quality           = 75;
                image.CompressionMethod = ImageMagick.CompressionMethod.JPEG;
                image.Write(path);
                return(path);
            }
        }
Example #5
0
        /// <summary>
        /// 设置图片水印
        /// </summary>
        /// <param name="path">需要加载水印的图片路径(绝对路径)</param>
        /// <param name="waterpath">水印图片(绝对路径)</param>
        /// <param name="location">水印位置</param>
        /// <returns></returns>
        public static string ImageWatermark(string path, string waterpath, ImageLocationMode location)
        {
            string extName = Path.GetExtension(path);

            if (extName == ".jpg" || extName == ".bmp" || extName == ".jpeg")
            {
                DateTime time     = DateTime.Now;
                string   fileName = "" + time.Year.ToString() + time.Month.ToString() + time.Day.ToString() +
                                    time.Hour.ToString() + time.Minute.ToString() + time.Second.ToString() +
                                    time.Millisecond.ToString();
                Image     img      = Bitmap.FromFile(path);
                Image     waterImg = Image.FromFile(waterpath);
                Graphics  g        = Graphics.FromImage(img);
                ArrayList coors    = GetLocation(location, img, waterImg);
                g.DrawImage(waterImg, new Rectangle(int.Parse(coors[0].ToString()), int.Parse(coors[1].ToString()),
                                                    waterImg.Width, waterImg.Height));
                waterImg.Dispose();
                g.Dispose();
                string newPath = Path.GetDirectoryName(path) + fileName + extName;
                img.Save(newPath);
                img.Dispose();
                File.Copy(newPath, path, true);
                if (File.Exists(newPath))
                {
                    File.Delete(newPath);
                }
            }
            return(path);
        }
Example #6
0
        /// <summary>
        /// 获取水印位置
        /// </summary>
        /// <param name="location">水印位置</param>
        /// <param name="img">需要添加水印的图片</param>
        /// <param name="width">宽(当水印类型为文字时,传过来的就是字体的大小)</param>
        /// <param name="height">高(当水印类型为文字时,传过来的就是字符的长度)</param>
        /// <returns></returns>
        private static ArrayList GetLocation(ImageLocationMode location, Image img, int width, int height)
        {
            ArrayList coords = new ArrayList();
            float     x      = 10;
            float     y      = 10;

            switch (location)
            {
            case ImageLocationMode.LeftTop:
                x = 10;
                y = 10;
                break;

            case ImageLocationMode.Top:
                x = img.Width / 2 - (width * height) / 2;
                break;

            case ImageLocationMode.RightTop:
                x = img.Width - width * height;
                break;

            case ImageLocationMode.LeftCenter:
                y = img.Height / 2;
                break;

            case ImageLocationMode.Center:
                x = img.Width / 2 - (width * height) / 2;
                y = img.Height / 2;
                break;

            case ImageLocationMode.RightCenter:
                x = img.Width - height;
                y = img.Height / 2;
                break;

            case ImageLocationMode.LeftBottom:
                y = img.Height - width - 5;
                break;

            case ImageLocationMode.Bottom:
                x = img.Width / 2 - (width * height) / 2;
                y = img.Height - width - 5;
                break;

            case ImageLocationMode.RightBottom:
                x = img.Width - width * height;
                y = img.Height - width - 5;
                break;

            default:
                break;
            }
            coords.Add(x);
            coords.Add(y);
            return(coords);
        }
Example #7
0
        /// <summary>
        /// 设置图片水印
        /// </summary>
        /// <param name="path">需要加载水印的图片路径(绝对路径)</param>
        /// <param name="waterpath">水印图片(绝对路径)</param>
        /// <param name="location">水印位置</param>
        /// <returns></returns>
        public static string ImageWatermark(string path, string waterpath, ImageLocationMode location)
        {
            string extName = Path.GetExtension(path);

            if (extName == ".jpg" || extName == ".bmp" || extName == ".jpeg" || extName == ".png")
            {
                DateTime time     = DateTime.Now;
                string   fileName = "" + time.Year.ToString() + time.Month.ToString() + time.Day.ToString() +
                                    time.Hour.ToString() + time.Minute.ToString() + time.Second.ToString() +
                                    time.Millisecond.ToString();

                //Image img = null;
                //Graphics g = null;
                //try
                //{
                //    img = Bitmap.FromFile(path);
                //    g = Graphics.FromImage(img);
                //    Image waterImg = Image.FromFile(waterpath);
                //    ArrayList coors = GetLocation(location, img, waterImg);
                //    g.DrawImage(waterImg, new Rectangle(int.Parse(coors[0].ToString()), int.Parse(coors[1].ToString()),
                //    waterImg.Width, waterImg.Height));
                //    waterImg.Dispose();
                //    string newPath = Path.GetDirectoryName(path) + fileName + extName;
                //    img.Save(newPath);
                //    File.Copy(newPath, path, true);
                //    if (File.Exists(newPath))
                //    {
                //        File.Delete(newPath);
                //    }
                //}
                //catch (OutOfMemoryException ex)
                //{
                //    if (img != null)
                //    {
                //        img.Dispose();
                //        img = null;
                //    }
                //    throw ex;
                //    //return ImageWatermarkByMagick(path, waterpath, location);
                //}
                //finally
                //{
                //    if (img != null)
                //    {
                //        img.Dispose();
                //    }
                //    if (g != null)
                //    {
                //        g.Dispose();
                //    }
                //}
                Image img = Image.FromFile(path);
                // 处理不在指定范围内的图片
                if (!Enum.IsDefined(typeof(PixelFormat), img.PixelFormat))
                {
                    int width  = img.Width;
                    int height = img.Height;
                    img.Dispose();
                    img = null;
                    using (Bitmap temp = new Bitmap(path))
                    {
                        img = temp.Clone(new Rectangle(0, 0, width, height), PixelFormat.Format32bppRgb);
                    }
                }
                Image waterImg = Image.FromFile(waterpath);
                using (Graphics g = Graphics.FromImage(img))
                {
                    ArrayList coors = GetLocation(location, img, waterImg);
                    g.DrawImage(waterImg, new Rectangle(int.Parse(coors[0].ToString()), int.Parse(coors[1].ToString()),
                                                        waterImg.Width, waterImg.Height));
                    waterImg.Dispose();
                }
                string newPath = Path.GetDirectoryName(path) + fileName + extName;
                img.Save(newPath);
                img.Dispose();
                File.Copy(newPath, path, true);
                if (File.Exists(newPath))
                {
                    File.Delete(newPath);
                }
            }
            return(path);
        }