add Watermark Image - CSharp System.Drawing

CSharp examples for System.Drawing:Image Effect

Description

add Watermark Image

Demo Code


using System.Web;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Drawing;
using System;/*w  ww. j a v  a  2 s.c o m*/

public class Main{
        public static bool addWatermarkImage(string oldpath, string newpath, string WaterMarkPicPath, string _watermarkPosition = "WM_CENTER", int transparentValue = 30)
        {
            try
            {
                Image image = Image.FromFile(HttpContext.Current.Server.MapPath(oldpath));
                Bitmap bitmap = new Bitmap(image.Width, image.Height, PixelFormat.Format24bppRgb);
                Graphics graphics = Graphics.FromImage(bitmap);
                graphics.Clear(Color.White);
                graphics.SmoothingMode = SmoothingMode.HighQuality;
                graphics.InterpolationMode = InterpolationMode.High;
                graphics.DrawImage(image, 0, 0, image.Width, image.Height);
                Image image2 = new Bitmap(HttpContext.Current.Server.MapPath(WaterMarkPicPath));
                ImageAttributes imageAttr = new ImageAttributes();
                ColorMap map = new ColorMap {
                    OldColor = Color.FromArgb(0xff, 0, 0xff, 0),
                    NewColor = Color.FromArgb(0, 0, 0, 0)
                };
                ColorMap[] mapArray = new ColorMap[] { map };
                imageAttr.SetRemapTable(mapArray, ColorAdjustType.Bitmap);
                float num = ((float) transparentValue) / 100f;
                float[][] numArray2 = new float[5][];
                float[] numArray3 = new float[5];
                numArray3[0] = 1f;
                numArray2[0] = numArray3;
                float[] numArray4 = new float[5];
                numArray4[1] = 1f;
                numArray2[1] = numArray4;
                float[] numArray5 = new float[5];
                numArray5[2] = 1f;
                numArray2[2] = numArray5;
                float[] numArray6 = new float[5];
                numArray6[3] = num;
                numArray2[3] = numArray6;
                float[] numArray7 = new float[5];
                numArray7[4] = 1f;
                numArray2[4] = numArray7;
                float[][] newColorMatrix = numArray2;
                ColorMatrix matrix = new ColorMatrix(newColorMatrix);
                imageAttr.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                int x = 0;
                int y = 0;
                int width = 0;
                int height = 0;
                double num6 = 1.0;
                if ((image.Width > (image2.Width * 4)) && (image.Height > (image2.Height * 4)))
                {
                    num6 = 1.0;
                }
                else if ((image.Width > (image2.Width * 4)) && (image.Height < (image2.Height * 4)))
                {
                    num6 = Convert.ToDouble((int) (image.Height / 4)) / Convert.ToDouble(image2.Height);
                }
                else if ((image.Width < (image2.Width * 4)) && (image.Height > (image2.Height * 4)))
                {
                    num6 = Convert.ToDouble((int) (image.Width / 4)) / Convert.ToDouble(image2.Width);
                }
                else if ((image.Width * image2.Height) > (image.Height * image2.Width))
                {
                    num6 = Convert.ToDouble((int) (image.Height / 4)) / Convert.ToDouble(image2.Height);
                }
                else
                {
                    num6 = Convert.ToDouble((int) (image.Width / 4)) / Convert.ToDouble(image2.Width);
                }
                width = Convert.ToInt32((double) (image2.Width * num6));
                height = Convert.ToInt32((double) (image2.Height * num6));
                string str = _watermarkPosition;
                if (str != null)
                {
                    if (!(str == "WM_TOP_LEFT"))
                    {
                        if (str == "WM_TOP_RIGHT")
                        {
                            goto Label_02FA;
                        }
                        if (str == "WM_BOTTOM_RIGHT")
                        {
                            goto Label_030E;
                        }
                        if (str == "WM_BOTTOM_LEFT")
                        {
                            goto Label_032C;
                        }
                        if (str == "WM_CENTER")
                        {
                            goto Label_0340;
                        }
                    }
                    else
                    {
                        x = 10;
                        y = 10;
                    }
                }
                goto Label_035E;
            Label_02FA:
                x = (image.Width - width) - 10;
                y = 10;
                goto Label_035E;
            Label_030E:
                x = (image.Width - width) - 10;
                y = (image.Height - height) - 10;
                goto Label_035E;
            Label_032C:
                x = 10;
                y = (image.Height - height) - 10;
                goto Label_035E;
            Label_0340:
                x = (image.Width / 2) - (width / 2);
                y = (image.Height / 2) - (height / 2);
            Label_035E:
                graphics.DrawImage(image2, new Rectangle(x, y, width, height), 0, 0, image2.Width, image2.Height, GraphicsUnit.Pixel, imageAttr);
                image2.Dispose();
                imageAttr.Dispose();
                bitmap.Save(HttpContext.Current.Server.MapPath(newpath));
                bitmap.Dispose();
                image.Dispose();
                return true;
            }
            catch
            {
            }
            return false;
        }
}

Related Tutorials