예제 #1
0
 /// <summary>
 /// 初始化水印的文字和图片信息
 /// </summary>
 public WaterMarkConfig()
 {
     if (this.WaterMarkTextInfo == null)
     {
         this.WaterMarkTextInfo = new WaterMarkText();
     }
     if (this.WaterMarkImageInfo == null)
     {
         this.WaterMarkImageInfo = new WaterMarkImage();
     }
 }
예제 #2
0
        /// <summary>
        /// 添加文字水印
        /// </summary>
        /// <param name="picture">Graphics对象</param>
        /// <param name="waterMarkTextInfo">文字水印对象</param>
        /// <param name="width">宽度</param>
        /// <param name="height">高度</param>
        private static void AddWatermarkText(Graphics picture, WaterMarkText waterMarkTextInfo, int width, int height)
        {
            SizeF  ef;
            string text = waterMarkTextInfo.Text;
            string waterMarkPosition  = waterMarkTextInfo.WaterMarkPosition;
            int    waterMarkPositionX = waterMarkTextInfo.WaterMarkPositionX;
            int    waterMarkPositionY = waterMarkTextInfo.WaterMarkPositionY;
            int    foneSize           = waterMarkTextInfo.FoneSize;
            string foneType           = waterMarkTextInfo.FoneType;
            string foneStyle          = waterMarkTextInfo.FoneStyle;
            string foneColor          = waterMarkTextInfo.FoneColor;

            foneColor = "#" + foneColor.TrimStart('#');
            int    foneBorder      = waterMarkTextInfo.FoneBorder;
            string foneBorderColor = waterMarkTextInfo.FoneBorderColor;

            foneBorderColor = "#" + foneBorderColor.TrimStart('#');
            Font   font = null;
            string str7 = foneStyle;

            if (str7 != null)
            {
                if (!(str7 == "Bold"))
                {
                    if (str7 == "Italic")
                    {
                        font = new Font(foneType, (float)foneSize, FontStyle.Italic);
                        goto Label_00F9;
                    }
                    if (str7 == "Regular")
                    {
                        font = new Font(foneType, (float)foneSize, FontStyle.Regular);
                        goto Label_00F9;
                    }
                    if (str7 == "Strikeout")
                    {
                        font = new Font(foneType, (float)foneSize, FontStyle.Strikeout);
                        goto Label_00F9;
                    }
                    if (str7 == "Underline")
                    {
                        font = new Font(foneType, (float)foneSize, FontStyle.Underline);
                        goto Label_00F9;
                    }
                }
                else
                {
                    font = new Font(foneType, (float)foneSize, FontStyle.Bold);
                    goto Label_00F9;
                }
            }
            font = new Font(foneType, (float)foneSize, FontStyle.Regular);
Label_00F9:
            ef = picture.MeasureString(text, font);
            float  x    = 0f;
            float  y    = 0f;
            string str8 = waterMarkPosition;

            if (str8 != null)
            {
                if (!(str8 == "WM_TOP_LEFT"))
                {
                    if (str8 == "WM_TOP_RIGHT")
                    {
                        x = (width * 0.99f) - (ef.Width / 2f);
                        y = height * 0.01f;
                    }
                    else if (str8 == "WM_BOTTOM_RIGHT")
                    {
                        x = (width * 0.99f) - (ef.Width / 2f);
                        y = (height * 0.99f) - ef.Height;
                    }
                    else if (str8 == "WM_BOTTOM_LEFT")
                    {
                        x = (width * 0.01f) + (ef.Width / 2f);
                        y = (height * 0.99f) - ef.Height;
                    }
                    else if (str8 == "WM_SetByManual")
                    {
                        x = waterMarkPositionX;
                        y = waterMarkPositionY;
                    }
                }
                else
                {
                    x = (width * 0.01f) + (ef.Width / 2f);
                    y = height * 0.01f;
                }
            }
            StringFormat format = new StringFormat
            {
                Alignment = StringAlignment.Center
            };
            ColorConverter converter = new ColorConverter();
            Color          color     = (Color)converter.ConvertFromString(foneBorderColor);
            SolidBrush     brush     = new SolidBrush(color);

            picture.DrawString(text, font, brush, x + foneBorder, y + foneBorder, format);
            ColorConverter converter2 = new ColorConverter();
            Color          color2     = (Color)converter2.ConvertFromString(foneColor);
            SolidBrush     brush2     = new SolidBrush(color2);

            try
            {
                picture.DrawString(text, font, brush2, x, y, format);
            }
            catch (ArgumentNullException ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                brush.Dispose();
                brush2.Dispose();
                format.Dispose();
                font.Dispose();
            }
        }
예제 #3
0
        /// <summary>
        /// 添加水印
        /// </summary>
        /// <param name="originalImagePath">源图相对路径,相对于网站根目录。如:wwwroot/upload/image/a.jpg</param>
        public static void AddWaterMark(string originalImagePath)
        {
            string filename  = "";
            string extension = Path.GetExtension(originalImagePath);

            filename = originalImagePath.Replace(extension, "WaterMark" + extension);
            var            waterMarkConfig    = ConfigHelper.Get <WaterMarkConfig>();
            WaterMarkText  waterMarkTextInfo  = waterMarkConfig.WaterMarkTextInfo;
            WaterMarkImage waterMarkImageInfo = waterMarkConfig.WaterMarkImageInfo;
            int            waterMarkType      = waterMarkConfig.WaterMarkType;
            float          transparence       = Convert.ToSingle(waterMarkImageInfo.Transparence) / 100f;

            string str5 = FileHelper.MapPath("~/") + Path.DirectorySeparatorChar.ToString();

            originalImagePath = str5 + originalImagePath;
            filename          = str5 + filename;
            Image image = null;

            try
            {
                image = Image.FromFile(originalImagePath);
            }
            catch (FileNotFoundException ex)
            {
                throw new Exception(ex.Message);
            }
            Bitmap   bitmap  = new Bitmap(image.Width, image.Height, PixelFormat.Format24bppRgb);
            Graphics picture = Graphics.FromImage(bitmap);

            picture.Clear(Color.Green);
            picture.SmoothingMode     = SmoothingMode.HighQuality;
            picture.InterpolationMode = InterpolationMode.High;
            picture.DrawImage(image, 0, 0, image.Width, image.Height);
            switch (waterMarkType)
            {
            case 0:
                AddWatermarkText(picture, waterMarkTextInfo, image.Width, image.Height);
                break;

            case 1:
                AddWatermarkImage(picture, waterMarkImageInfo, image.Width, image.Height, transparence);
                break;
            }
            try
            {
                bitmap.Save(filename, ImageFormat.Jpeg);
            }
            catch (ArgumentNullException ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                bitmap.Dispose();
                image.Dispose();
                picture.Dispose();
            }
            FileHelper.Delete(originalImagePath, FileMethod.File);
            string newFile = filename.Replace("WaterMark", "");

            FileHelper.Move(filename, newFile, FileMethod.File);
        }