示例#1
0
        public static Bitmap AddWatermark(Bitmap srcBitmap, string watermarkPath, WatermarkPositionType positionType)
        {
            using (Bitmap watermarkBitmap = new Bitmap(System.Drawing.Image.FromFile(watermarkPath)))
            {
                int posX = 0, posY = 0;
                int offsetX = 5, offsetY = 5;
                int srcWidth        = srcBitmap.Width;
                int srcHeight       = srcBitmap.Height;
                int watermarkWidth  = watermarkBitmap.Width;
                int watermarkHeight = watermarkBitmap.Height;
                switch (positionType)
                {
                case WatermarkPositionType.TopLeft:
                    posX = offsetX;
                    posY = offsetY;
                    break;

                case WatermarkPositionType.TopCenter:
                    posX = srcWidth / 2 - watermarkWidth / 2;
                    posY = offsetY;
                    break;

                case WatermarkPositionType.TopRight:
                    posX = srcWidth - watermarkWidth - offsetX;
                    posY = offsetY;
                    break;

                case WatermarkPositionType.Center:
                    posX = srcWidth / 2 - watermarkWidth / 2;
                    posY = srcHeight / 2 - watermarkHeight / 2;
                    break;

                case WatermarkPositionType.BottomLeft:
                    posX = offsetX;
                    posY = srcHeight - watermarkHeight - offsetY;
                    break;

                case WatermarkPositionType.BottomCenter:
                    posX = srcWidth / 2 - watermarkWidth / 2;
                    posY = srcHeight - watermarkHeight - offsetY;
                    break;

                case WatermarkPositionType.BottomRight:
                    posX = srcWidth - watermarkWidth - offsetX;
                    posY = srcHeight - watermarkHeight - offsetY;
                    break;

                default:
                    throw new ArgumentOutOfRangeException("positionType");
                }
                return(AddWatermark(srcBitmap, posX, posY, watermarkBitmap, 0.2f));
            }
        }
示例#2
0
        protected static Point FindPosition(WatermarkPositionType positionType, int offset, Size img, Size img2, int add)
        {
            Point position;

            switch (positionType)
            {
            case WatermarkPositionType.TOP_LEFT:
                position = new Point(offset, offset);
                break;

            case WatermarkPositionType.TOP_RIGHT:
                position = new Point(img.Width - img2.Width - offset - add, offset);
                break;

            case WatermarkPositionType.BOTTOM_LEFT:
                position = new Point(offset, img.Height - img2.Height - offset - add);
                break;

            case WatermarkPositionType.BOTTOM_RIGHT:
                position = new Point(img.Width - img2.Width - offset - add, img.Height - img2.Height - offset - add);
                break;

            case WatermarkPositionType.CENTER:
                position = new Point(img.Width / 2 - img2.Width / 2 - add, img.Height / 2 - img2.Height / 2 - add);
                break;

            case WatermarkPositionType.LEFT:
                position = new Point(offset, img.Height / 2 - img2.Height / 2 - add);
                break;

            case WatermarkPositionType.TOP:
                position = new Point(img.Width / 2 - img2.Width / 2 - add, offset);
                break;

            case WatermarkPositionType.RIGHT:
                position = new Point(img.Width - img2.Width - offset - add, img.Height / 2 - img2.Height / 2 - add);
                break;

            case WatermarkPositionType.BOTTOM:
                position = new Point(img.Width / 2 - img2.Width / 2 - add, img.Height - img2.Height - offset - add);
                break;

            default:
                position = Point.Empty;
                break;
            }

            return(position);
        }
 protected static Point FindPosition(WatermarkPositionType positionType, int offset, Size img, Size img2, int add)
 {
     Point position;
     switch (positionType)
     {
         case WatermarkPositionType.TOP_LEFT:
             position = new Point(offset, offset);
             break;
         case WatermarkPositionType.TOP_RIGHT:
             position = new Point(img.Width - img2.Width - offset - add, offset);
             break;
         case WatermarkPositionType.BOTTOM_LEFT:
             position = new Point(offset, img.Height - img2.Height - offset - add);
             break;
         case WatermarkPositionType.BOTTOM_RIGHT:
             position = new Point(img.Width - img2.Width - offset - add, img.Height - img2.Height - offset - add);
             break;
         case WatermarkPositionType.CENTER:
             position = new Point(img.Width / 2 - img2.Width / 2 - add, img.Height / 2 - img2.Height / 2 - add);
             break;
         case WatermarkPositionType.LEFT:
             position = new Point(offset, img.Height / 2 - img2.Height / 2 - add);
             break;
         case WatermarkPositionType.TOP:
             position = new Point(img.Width / 2 - img2.Width / 2 - add, offset);
             break;
         case WatermarkPositionType.RIGHT:
             position = new Point(img.Width - img2.Width - offset - add, img.Height / 2 - img2.Height / 2 - add);
             break;
         case WatermarkPositionType.BOTTOM:
             position = new Point(img.Width / 2 - img2.Width / 2 - add, img.Height - img2.Height - offset - add);
             break;
         default:
             position = Point.Empty;
             break;
     }
     return position;
 }