Пример #1
0
        /// <summary>
        /// 添加水印
        /// </summary>
        /// <param name="bitmap">原始图片</param>
        protected Image WaterMark(Image image, User user)
        {
            ///读取水印配置
            CommonConfig.ConfigItem watermarkmodel = CommonConfig.Instance.GetConfig("WaterMark");

            if (watermarkmodel != null)
            {
                if (watermarkmodel.Cate == 1) //判断水印类型
                {
                    //水印图片
                    Image copyImage = Image.FromFile(System.Web.HttpContext.Current.Server.MapPath(watermarkmodel.ImageUrl));
                    int   width     = 0;
                    int   height    = 0;
                    switch (watermarkmodel.Location)
                    {
                    case 1: width = 0; height = 0; break;                                                                       //左上

                    case 2: width = (image.Width - copyImage.Width) / 2; height = 0; break;                                     //左中

                    case 3: width = image.Width - copyImage.Width; height = 0; break;                                           //右上

                    case 4: width = 0; height = (image.Height - copyImage.Height) / 2; break;                                   //左中

                    case 5: width = (image.Width - copyImage.Width) / 2; height = (image.Height - copyImage.Height) / 2; break; //中中

                    case 6: width = image.Width - copyImage.Width; height = (image.Height - copyImage.Height) / 2; break;       //右中

                    //case 7: width = 0; height = image.Height - copyImage.Height; break;//左下
                    case 7: width = 10; height = image.Height - Tools.SafeInt(copyImage.Height * 2); break;               //左下

                    case 8: width = (image.Width - copyImage.Width) / 2; height = image.Height - copyImage.Height; break; //中下

                    case 9: width = image.Width - copyImage.Width; height = image.Height - copyImage.Height; break;       //右下
                    }
                    Graphics g = Graphics.FromImage(image);
                    g.DrawImage(copyImage, new Rectangle(width, height, Convert.ToInt16(watermarkmodel.Width), Convert.ToInt16(watermarkmodel.Height)), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
                    using (Font f = new Font("Verdana", 10))
                    {
                        using (Brush b = new SolidBrush(Color.White))
                        {
                            g.DrawString((string.IsNullOrWhiteSpace(user.DrawText) ? BaseHelper.FilterEmoji(user.NickName) : user.DrawText), f, b, Tools.SafeInt(watermarkmodel.Width) + 15, image.Height - Tools.SafeInt(copyImage.Height * 2));
                            g.DrawString("http://www.xiaoweipian.com/u/" + user.Number, f, b, 10, image.Height - copyImage.Height);
                        }
                    }
                    g.Dispose();
                    copyImage.Dispose();
                }
                else
                {
                    //文字水印
                    int width      = 0;
                    int height     = 0;
                    int fontwidth  = Convert.ToInt32(watermarkmodel.FontSize * watermarkmodel.Word.Length);
                    int fontheight = Convert.ToInt32(watermarkmodel.FontSize);
                    switch (watermarkmodel.Location)
                    {
                    case 1: width = 0; height = 0; break;

                    case 2: width = (image.Width - fontwidth) / 2; height = 0; break;

                    case 3: width = image.Width - fontwidth; height = 0; break;

                    case 4: width = 0; height = (image.Height - fontheight) / 2; break;

                    case 5: width = (image.Width - fontwidth) / 2; height = (image.Height - fontheight) / 2; break;

                    case 6: width = image.Width - fontwidth; height = (image.Height - fontheight) / 2; break;

                    case 7: width = 0; height = image.Height - fontheight; break;

                    case 8: width = (image.Width - fontwidth) / 2; height = image.Height - fontheight; break;

                    case 9: width = image.Width - fontwidth; height = image.Height - fontheight; break;
                    }
                    Graphics g = Graphics.FromImage(image);
                    g.DrawImage(image, 0, 0, image.Width, image.Height);
                    Font  f = new Font("Verdana", float.Parse(watermarkmodel.FontSize.ToString()));
                    Brush b = new SolidBrush(Color.White);
                    g.DrawString(watermarkmodel.Word, f, b, width, height);
                    g.Dispose();
                }
            }
            return(image);
        }