Exemple #1
0
        public static Image Watermark(Image image, string watermarkPath, string mark, WatermarkPosition position)
        {
            if (image == null)
            {
                throw new ArgumentNullException("原图片不能为null");
            }
            if (string.IsNullOrEmpty(mark))
            {
                throw new ArgumentException("水印文字不能为空");
            }
            using (Graphics graphics = Graphics.FromImage(image))
            {
                graphics.DrawImage(image, 0, 0, image.Width, image.Height);
                graphics.SmoothingMode      = SmoothingMode.HighQuality;
                graphics.TextRenderingHint  = TextRenderingHint.ClearTypeGridFit;
                graphics.CompositingQuality = CompositingQuality.HighQuality;
                graphics.InterpolationMode  = InterpolationMode.HighQualityBicubic;
                using (Font font = new Font("Arial", 10f, FontStyle.Bold | FontStyle.Italic))
                {
                    using (Brush brush = new SolidBrush(Color.White))
                    {
                        SizeF ef = graphics.MeasureString(mark, font);
                        float x;
                        float y;
                        switch (position)
                        {
                        case WatermarkPosition.Bottom_left:
                            x = 10f;
                            y = (float)image.Height - ef.Height - 10f;
                            goto IL_16F;

                        case WatermarkPosition.Top_left:
                            x = 10f;
                            y = 10f;
                            goto IL_16F;

                        case WatermarkPosition.Top_right:
                            x = (float)image.Width - ef.Width - 10f;
                            y = 10f;
                            goto IL_16F;

                        case WatermarkPosition.Center:
                            x = (float)image.Width / 2f - ef.Width / 2f;
                            y = (float)image.Height / 2f - ef.Height / 2f;
                            goto IL_16F;
                        }
                        x = (float)image.Width - ef.Width - 10f;
                        y = (float)image.Height - ef.Height - 10f;
IL_16F:
                        graphics.DrawString(mark, font, brush, x, y);
                        ImageUtil.SaveImage(image, watermarkPath);
                    }
                }
            }
            return(image);
        }
Exemple #2
0
 public static Image Thumbnail(Image image, string thumbnailPath, int width, int height)
 {
     if (image == null)
     {
         throw new ArgumentNullException("原图片不能为null");
     }
     if (width <= 0 || height <= 0)
     {
         throw new ArgumentException("缩略图宽度或高度不能小于或等于零");
     }
     image = image.GetThumbnailImage(width, height, null, IntPtr.Zero);
     ImageUtil.SaveImage(image, thumbnailPath);
     return(image);
 }