Exemple #1
0
        /// <summary>
        /// 图片水印处理方法
        /// </summary>
        /// <param name="path">需要加载水印的图片路径(绝对路径)</param>
        /// <param name="waterpath">水印图片(绝对路径)</param>
        /// <param name="location">水印位置(传送正确的代码)</param>
        public static string ImageWatermark(string path, string waterpath, WaterLocation location)
        {
            string kz_name = Path.GetExtension(path);

            if (_imageExtList.Contains(kz_name))
            {
                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 loca     = GetLocation(location, img, waterimg);
                g.DrawImage(waterimg, new Rectangle(int.Parse(loca[0].ToString()), int.Parse(loca[1].ToString()), waterimg.Width, waterimg.Height));
                waterimg.Dispose();
                g.Dispose();
                string newpath = Path.GetDirectoryName(path) + filename + kz_name;
                img.Save(newpath);
                img.Dispose();
                File.Copy(newpath, path, true);
                if (File.Exists(newpath))
                {
                    File.Delete(newpath);
                }
            }
            return(path);
        }
Exemple #2
0
        /// <summary>
        /// 文字水印位置的方法
        /// </summary>
        /// <param name="location">位置代码</param>
        /// <param name="img">图片对象</param>
        /// <param name="width">宽(当水印类型为文字时,传过来的就是字体的大小)</param>
        /// <param name="height">高(当水印类型为文字时,传过来的就是字符的长度)</param>
        private static ArrayList GetLocation(WaterLocation location, Image img, float width, float height)
        {
            #region

            ArrayList loca = new ArrayList();  //定义数组存储位置
            float     x    = 10;
            float     y    = 10;

            if (location == WaterLocation.LT)
            {
                loca.Add(x);
                loca.Add(y);
            }
            else if (location == WaterLocation.T)
            {
                x = img.Width / 2 - (width * height) / 2;
                loca.Add(x);
                loca.Add(y);
            }
            else if (location == WaterLocation.RT)
            {
                x = img.Width - width;
            }
            else if (location == WaterLocation.LC)
            {
                y = img.Height / 2;
            }
            else if (location == WaterLocation.C)
            {
                x = img.Width / 2 - (width) / 2;
                y = img.Height / 2;
            }
            else if (location == WaterLocation.RC)
            {
                x = img.Width - width;
                y = img.Height / 2;
            }
            else if (location == WaterLocation.LB)
            {
                y = img.Height - height - 5;
            }
            else if (location == WaterLocation.B)
            {
                x = img.Width / 2 - (width) / 2;
                y = img.Height - height - 5;
            }
            else
            {
                x = img.Width - width;
                y = img.Height - height - 5;
            }
            loca.Add(x);
            loca.Add(y);
            return(loca);

            #endregion
        }
Exemple #3
0
        /// <summary>
        /// 图片水印位置处理方法
        /// </summary>
        /// <param name="location">水印位置</param>
        /// <param name="img">需要添加水印的图片</param>
        /// <param name="waterimg">水印图片</param>
        private static ArrayList GetLocation(WaterLocation location, Image img, Image waterimg)
        {
            ArrayList loca = new ArrayList();
            int       x    = 0;
            int       y    = 0;

            if (location == WaterLocation.LT)
            {
                x = 10;
                y = 10;
            }
            else if (location == WaterLocation.T)
            {
                x = img.Width / 2 - waterimg.Width / 2;
                y = img.Height - waterimg.Height;
            }
            else if (location == WaterLocation.RT)
            {
                x = img.Width - waterimg.Width;
                y = 10;
            }
            else if (location == WaterLocation.LC)
            {
                x = 10;
                y = img.Height / 2 - waterimg.Height / 2;
            }
            else if (location == WaterLocation.C)
            {
                x = img.Width / 2 - waterimg.Width / 2;
                y = img.Height / 2 - waterimg.Height / 2;
            }
            else if (location == WaterLocation.RC)
            {
                x = img.Width - waterimg.Width;
                y = img.Height / 2 - waterimg.Height / 2;
            }
            else if (location == WaterLocation.LB)
            {
                x = 10;
                y = img.Height - waterimg.Height;
            }
            else if (location == WaterLocation.B)
            {
                x = img.Width / 2 - waterimg.Width / 2;
                y = img.Height - waterimg.Height;
            }
            else
            {
                x = img.Width - waterimg.Width;
                y = img.Height - waterimg.Height;
            }
            loca.Add(x);
            loca.Add(y);
            return(loca);
        }
Exemple #4
0
        /// <summary>
        /// 文字水印处理方法
        /// </summary>
        /// <param name="path">图片路径(绝对路径)</param>
        /// <param name="size">字体大小</param>
        /// <param name="letter">水印文字</param>
        /// <param name="color">颜色</param>
        /// <param name="location">水印位置</param>
        public static string LetterWatermark(string path, int size, string letter, Color color, WaterLocation location, bool adjustSize = true)
        {
            #region

            string kz_name = Path.GetExtension(path);
            if (_imageExtList.Contains(kz_name))
            {
                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 gs       = Graphics.FromImage(img);
                Font     font     = new Font("宋体", size);
                var      sizeF    = gs.MeasureString(letter, font);
                if (adjustSize)
                {
                    while ((sizeF.Width * 2 > img.Width || sizeF.Height * 2 > img.Height) && size > 8)
                    {
                        size--;
                        font  = new Font("宋体", size);
                        sizeF = gs.MeasureString(letter, font);
                    }
                    if (sizeF.Width * 2 > img.Width || sizeF.Height * 2 > img.Height)
                    {
                        return(path);
                    }
                }
                ArrayList loca = GetLocation(location, img, sizeF.Width, sizeF.Height);
                Brush     br   = new SolidBrush(color);
                gs.DrawString(letter, font, br, float.Parse(loca[0].ToString()), float.Parse(loca[1].ToString()));
                gs.Dispose();
                string newpath = Path.GetDirectoryName(path) + filename + kz_name;
                img.Save(newpath);
                img.Dispose();
                File.Copy(newpath, path, true);
                if (File.Exists(newpath))
                {
                    File.Delete(newpath);
                }
            }
            return(path);

            #endregion
        }