Esempio n. 1
0
        public static void AddWatermarks(FileStream fs, WatermarkCollection marks, Stream outputStream)
        {
            Image    img            = Image.FromStream(fs);
            Graphics gr             = null;
            int      pad            = 2;
            float    actualFontSize = img.Height / 1000;
            //if (actualFontSize < 12)
            //    actualFontSize = 12;



            float factor = img.Height > img.Width ? img.Height : img.Width;

            if (actualFontSize < img.Height * .005)
            {
                actualFontSize = Convert.ToInt32(factor * .005);
            }

            float upperBackground = 0;
            float lowerBackground = 0;


            // Determine text background heights
            foreach (Watermark mark in marks)
            {
                if (string.IsNullOrEmpty(mark.Body))
                {
                    continue;
                }

                Font font = mark.TextFont;
                font = new Font(font.Name, actualFontSize);

                if ((mark.Location == WatermarkLocation.UpperLeft || mark.Location == WatermarkLocation.UpperRight))
                {
                    upperBackground = font.Height * 2;
                }

                if ((mark.Location == WatermarkLocation.LowerLeft || mark.Location == WatermarkLocation.LowerRight))
                {
                    lowerBackground = font.Height * 2;
                }
            }

            try
            {
                gr = Graphics.FromImage(img);
                gr.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height));
                //gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            }
            catch (Exception ex)
            {
                Image img1 = img;
                img = new Bitmap(img1, img.Width, img.Height);
                gr  = Graphics.FromImage(img);
                gr.DrawImage(img1, new Rectangle(0, 0, img.Width, img.Height));//, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel);
                img1.Dispose();
            }

            // Draw text backgrounds
            if (upperBackground > 0)
            {
                using (Brush brush = new SolidBrush(Color.FromArgb(128, 0, 0, 0)))
                    gr.FillRectangle(brush, 0, 0, img.Width, upperBackground + (pad * 2));
            }
            if (lowerBackground > 0)
            {
                using (Brush brush = new SolidBrush(Color.FromArgb(128, 0, 0, 0)))
                    gr.FillRectangle(brush, 0, img.Height - lowerBackground - (pad * 2), img.Width, lowerBackground + (pad * 2));
            }

            // Draw strings
            foreach (Watermark mark in marks)
            {
                Font font = mark.TextFont;
                if (img.VerticalResolution == 96)
                {
                    actualFontSize = actualFontSize * 1.25f;
                }
                font = new Font(font.Name, actualFontSize);

                SolidBrush sbrush = new SolidBrush(mark.TextColor);

                // The position where to draw the watermark on the image
                SizeF ss = gr.MeasureString(mark.Body, font);

                // Lower left
                Point pt = new Point(pad, img.Height - ((int)ss.Height - pad));

                // Upper left
                if (mark.Location == WatermarkLocation.UpperLeft)
                {
                    pt = new Point(pad, pad);
                }

                // Upper right
                if (mark.Location == WatermarkLocation.UpperRight)
                {
                    pt = new Point(img.Width - ((int)ss.Width + pad), pad);
                }

                // Lower right
                if (mark.Location == WatermarkLocation.LowerRight)
                {
                    pt = new Point(img.Width - ((int)ss.Width + pad), img.Height - ((int)ss.Height - pad));
                }

                // Center
                if (mark.Location == WatermarkLocation.Center)
                {
                    pt = new Point((img.Width / 2) - (((int)ss.Width / 2)), (img.Height / 2) - (((int)ss.Height / 2)));
                }

                // Print
                gr.DrawString(mark.Body, font, sbrush, pt);
                // DEBUG gr.DrawString(Convert.ToString(actualFontSize), font, sbrush, pt);
                // gr.DrawString(Convert.ToString(img.VerticalResolution), font, sbrush, pt);
            }

            // Cleanup
            gr.Dispose();

            // Save to memory stream
            img.Save(outputStream, ImageFormat.Jpeg);
        }