Ejemplo n.º 1
0
        private static WaterPosition GetWaterPosition(float width, float height, float waterMarkWidth, float waterMarkHeight, WaterPositionOptions position)
        {
            WaterPosition position2 = new WaterPosition();

            switch (position)
            {
            case WaterPositionOptions.LeftTop:
                position2.X = 0f;
                position2.Y = 0f;
                break;

            case WaterPositionOptions.RightTop:
                position2.X = width - waterMarkWidth;
                position2.Y = 0f;
                break;

            case WaterPositionOptions.Middle:
                position2.X = ((float)(width - waterMarkWidth)) / 2f;
                position2.Y = ((float)(height - waterMarkHeight)) / 2f;
                break;

            case WaterPositionOptions.LeftBottom:
                position2.X = 0f;
                position2.Y = height - waterMarkHeight;
                break;

            case WaterPositionOptions.RightBottom:
                position2.X = width - waterMarkWidth;
                position2.Y = height - waterMarkHeight;
                break;
            }
            return(position2);
        }
Ejemplo n.º 2
0
        private static void SaveWaterPic(string sUploadPathAndFileName, WaterPositionOptions position)
        {
            sUploadPathAndFileName = HttpContext.Current.Server.MapPath(sUploadPathAndFileName);

            Image    image  = Image.FromFile(sUploadPathAndFileName);
            Bitmap   bitmap = new Bitmap(image.Width, image.Height);
            Graphics g      = Graphics.FromImage(bitmap);

            g.DrawImage(image, new Point(0, 0));
            Image         waterImage    = GetWaterMarkImage();
            WaterPosition waterPosition = GetWaterPosition(image.Width, image.Height, waterImage.Width, waterImage.Height, position);

            //打水印
            g.DrawImage(waterImage, new Rectangle((int)waterPosition.X, (int)waterPosition.Y, waterImage.Width, waterImage.Height), 0, 0, waterImage.Width, waterImage.Height, System.Drawing.GraphicsUnit.Pixel);
            image.Dispose();
            waterImage.Dispose();
            bitmap.Save(sUploadPathAndFileName);
            bitmap.Dispose();
            g.Dispose();
        }