Example #1
0
        /// <summary>
        /// 设置水印
        /// </summary>
        /// <param name="original"></param>
        /// <param name="watermark"></param>
        /// <returns></returns>
        public Image MakeWatermark(Image original, Watermark watermark)
        {
            //Bitmap b = new Bitmap(original.Width, original.Height, PixelFormat.Format24bppRgb);
            //b.SetResolution(original.HorizontalResolution, original.VerticalResolution);

            // 不加水印
            if (watermark.Ratio < 0)
            {
                return(original);
            }
            //当ratio大于1时,加的部分水印会在图片外面(不显示)
            Size markSize = new Size((int)Math.Round(watermark.Ratio * original.Width), (int)Math.Round(watermark.Ratio * original.Height));

            watermark.SetSize(markSize);

            switch (watermark.WatermarkType)
            {
            case WatermarkType.Text:
                //添加文字水印
                var textWm = watermark as TextWatermark;
                if (!string.IsNullOrEmpty(textWm.Text))
                {
                    textWm.HorizontalResolution = original.HorizontalResolution; // b.HorizontalResolution;
                    textWm.VerticalResolution   = original.VerticalResolution;   // b.VerticalResolution;
                    var            textMark = this.CreateTextWatermark(textWm);
                    ImageWatermark imageWm  = new ImageWatermark()
                    {
                        Alignment        = textWm.Alignment,
                        Alpha            = textWm.Alpha,
                        Image            = textMark,
                        MarginPercentage = textWm.MarginPercentage,
                        Ratio            = textWm.Ratio,
                    };
                    imageWm.SetSize(textWm.Size);
                    return(MakeImageWatermark(original, imageWm));
                }
                break;

            case WatermarkType.Image:
                return(MakeImageWatermark(original, watermark as ImageWatermark));

                break;

            default:
                break;
            }
            return(original);
        }
Example #2
0
        /// <summary>
        /// 保存图片
        /// </summary>
        /// <param name="originalFile"></param>
        /// <param name="savePath"></param>
        /// <param name="maxWidth"></param>
        /// <param name="maxHeight"></param>
        /// <param name="isCutOverRange">从内部适配长/宽,多的切除,取中段</param>
        /// <returns></returns>
        public string SaveImage(Image originalFile, string savePath, int maxWidth, int maxHeight, Watermark watermark, bool isCutOverRange = false)
        {
            if (originalFile == null || string.IsNullOrEmpty(savePath))
            {
                return(string.Empty);
            }

            if (!Directory.Exists(savePath))
            {
                Directory.CreateDirectory(savePath);
            }

            var formalImage = SaveImage(originalFile, maxWidth, maxHeight, watermark, isCutOverRange);
            var fileName    = DateTimeExtensions.GetRandomString(DateTime.Now) + ".jpeg";
            var file        = SaveTo(formalImage, savePath, fileName);

            return(Path.Combine(savePath, fileName));
        }
Example #3
0
        /// <summary>
        /// 按比例格式化图片
        /// </summary>
        /// <param name="original"></param>
        /// <param name="maxWidth"></param>
        /// <param name="maxHeight"></param>
        /// <param name="isCutOverRange">从内部适配长/宽,多的切除,取中段</param>
        /// <returns></returns>
        private Image SaveImage(Image original, int maxWidth, int maxHeight, Watermark waterMark, bool isCutOverRange = false)
        {
            if (original == null)
            {
                return(original);
            }

            Image response = null;
            var   x        = 0d;
            var   y        = 0d;
            var   width    = 0d;
            var   height   = 0d;

            //get minimal image
            if (original.Width > maxWidth)
            {
                x     = (original.Width - maxWidth) / 2d;
                width = maxWidth;
            }
            else
            {
                width = original.Width;
            }
            if (original.Height > maxWidth)
            {
                y      = (original.Height - maxHeight) / 2.0d;
                height = maxHeight;
            }
            else
            {
                height = original.Height;
            }

            if (isCutOverRange)
            {
                //var crossWidth = width;
                //var crossHeight = height;
                var widthRatio  = width / maxWidth * 1d;
                var heightRatio = height / maxHeight * 1d;
                if (widthRatio >= heightRatio)
                {
                    width = maxWidth * heightRatio;
                }
                else
                {
                    height = maxHeight * widthRatio;
                }
                x = (original.Width - width) / 2d;
                y = (original.Height - height) / 2d;
            }

            //用指定的大小和格式初始化 Bitmap 类的新实例
            //Bitmap bitmap = new Bitmap((int)width, (int)height, PixelFormat.Format32bppArgb);
            Bitmap bitmap = new Bitmap((int)width, (int)height);

            bitmap.SetResolution(original.VerticalResolution, original.HorizontalResolution);
            Graphics graphics = Graphics.FromImage(bitmap);

            graphics.Clear(Color.Transparent);
            graphics.SmoothingMode     = SmoothingMode.HighQuality;
            graphics.InterpolationMode = InterpolationMode.High;
            graphics.DrawImage(original, 0, 0, new Rectangle((int)x, (int)y, (int)width, (int)height), GraphicsUnit.Pixel);
            graphics.Save();
            graphics.Dispose();
            response = bitmap;
            if (waterMark != null)
            {
                response = new ImageWatermarkMaker().MakeWatermark(bitmap, waterMark);
            }
            return(response);
        }