Exemplo n.º 1
0
        public static void AddWaterMark(string originalImagePath)
        {
            string filename  = "";
            string extension = Path.GetExtension(originalImagePath);

            filename = originalImagePath.Replace(extension, "WaterMark" + extension);
            WaterMarkText  waterMarkTextInfo  = SiteConfig.WaterMarkConfig.WaterMarkTextInfo;
            WaterMarkImage waterMarkImageInfo = SiteConfig.WaterMarkConfig.WaterMarkImageInfo;
            int            waterMarkType      = SiteConfig.WaterMarkConfig.WaterMarkType;
            string         str4 = VirtualPathUtility.AppendTrailingSlash(SiteConfig.SiteOption.UploadDir);
            string         str5 = VirtualPathUtility.AppendTrailingSlash(HttpContext.Current.Server.MapPath("~/"));

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

            try
            {
                image = Image.FromFile(originalImagePath);
            }
            catch (FileNotFoundException)
            {
                CustomException.ThrowBllException("添加水印的源图片未找到。");
            }
            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);
                break;
            }
            try
            {
                bitmap.Save(filename, ImageFormat.Jpeg);
            }
            catch (ArgumentNullException)
            {
                CustomException.ThrowBllException("该图像的保存路径未找到。");
            }
            finally
            {
                bitmap.Dispose();
                image.Dispose();
                picture.Dispose();
            }
            FileSystemObject.Delete(originalImagePath, FsoMethod.File);
            string newFile = filename.Replace("WaterMark", "");

            FileSystemObject.Move(filename, newFile, FsoMethod.File);
        }
Exemplo n.º 2
0
 public WaterMarkConfig()
 {
     if (this.waterMarkTextInfo == null)
     {
         this.waterMarkTextInfo = new WaterMarkText();
     }
     if (this.waterMarkImageInfo == null)
     {
         this.waterMarkImageInfo = new WaterMarkImage();
     }
 }
Exemplo n.º 3
0
        private static void AddWatermarkImage(Graphics picture, WaterMarkImage waterMarkImageInfo, int width, int height)
        {
            string filename           = HttpContext.Current.Server.MapPath("~/" + waterMarkImageInfo.ImagePath);
            string waterMarkPosition  = waterMarkImageInfo.WaterMarkPosition;
            int    waterMarkPositionX = waterMarkImageInfo.WaterMarkPositionX;
            int    waterMarkPositionY = waterMarkImageInfo.WaterMarkPositionY;
            Image  image = null;

            try
            {
                image = new Bitmap(filename);
            }
            catch (ArgumentException)
            {
                CustomException.ThrowBllException("图片水印的路径未找到。");
            }
            ImageAttributes imageAttr = new ImageAttributes();
            ColorMap        map       = new ColorMap();

            map.OldColor = Color.FromArgb(0xff, 0, 0xff, 0);
            map.NewColor = Color.FromArgb(0, 0, 0, 0);
            ColorMap[] mapArray = new ColorMap[] { map };
            imageAttr.SetRemapTable(mapArray, ColorAdjustType.Bitmap);
            float[][] numArray2 = new float[5][];
            float[]   numArray3 = new float[5];
            numArray3[0] = 1f;
            numArray2[0] = numArray3;
            float[] numArray4 = new float[5];
            numArray4[1] = 1f;
            numArray2[1] = numArray4;
            float[] numArray5 = new float[5];
            numArray5[2] = 1f;
            numArray2[2] = numArray5;
            float[] numArray6 = new float[5];
            numArray6[3] = 0.3f;
            numArray2[3] = numArray6;
            float[] numArray7 = new float[5];
            numArray7[4] = 1f;
            numArray2[4] = numArray7;
            float[][]   newColorMatrix = numArray2;
            ColorMatrix matrix         = new ColorMatrix(newColorMatrix);

            imageAttr.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
            int    x     = 0;
            int    y     = 0;
            int    num5  = 0;
            int    num6  = 0;
            double num7  = 1.0;
            double num8  = Convert.ToDouble((int)(width / 4));
            double num9  = Convert.ToDouble((int)(height / 4));
            double num10 = Convert.ToDouble(image.Width);
            double num11 = Convert.ToDouble(image.Height);

            if ((width > (image.Width * 4)) && (height > (image.Height * 4)))
            {
                num7 = 1.0;
            }
            else if ((width > (image.Width * 4)) && (height < (image.Height * 4)))
            {
                num7 = num9 / num11;
            }
            else if ((width < (image.Width * 4)) && (height > (image.Height * 4)))
            {
                num7 = num8 / num10;
            }
            else if ((width * image.Height) > (height * image.Width))
            {
                num7 = num9 / num11;
            }
            else
            {
                num7 = num8 / num10;
            }
            num5 = Convert.ToInt32((double)(image.Width * num7));
            num6 = Convert.ToInt32((double)(image.Height * num7));
            string str3 = waterMarkPosition;

            if (str3 != null)
            {
                if (!(str3 == "WM_TOP_LEFT"))
                {
                    if (str3 == "WM_TOP_RIGHT")
                    {
                        x = (width - num5) - 10;
                        y = 10;
                    }
                    else if (str3 == "WM_BOTTOM_RIGHT")
                    {
                        x = (width - num5) - 10;
                        y = (height - num6) - 10;
                    }
                    else if (str3 == "WM_BOTTOM_LEFT")
                    {
                        x = 10;
                        y = (height - num6) - 10;
                    }
                    else if (str3 == "WM_SetByManual")
                    {
                        x = waterMarkPositionX;
                        y = waterMarkPositionY;
                    }
                }
                else
                {
                    x = 10;
                    y = 10;
                }
            }
            try
            {
                picture.DrawImage(image, new Rectangle(x, y, num5, num6), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttr);
            }
            catch
            {
                CustomException.ThrowBllException("添加图片水印出现异常。");
            }
            finally
            {
                image.Dispose();
                imageAttr.Dispose();
            }
        }