コード例 #1
0
ファイル: ImageWaterMark.cs プロジェクト: liuhuachao/weixin
        /// <summary>
        /// 添加图片水印
        /// </summary>
        /// <param name="oldpath">原图片绝对地址</param>
        /// <param name="newpath">新图片放置的绝对地址</param>
        /// <param name="wmtType">要添加的水印的类型</param>
        /// <param name="wmPosition">水印位置</param>
        /// <param name="waterMarkContent">水印内容,若为文字水印,则是要添加的文字;若为图片水印,则是图片的路径</param>
        public void addWaterMark(string oldpath, string newpath,WaterMarkType wmType,WaterMarkPosition wmPosition,string wmContent)
        {
            try
            {
                Image image = Image.FromFile(oldpath);
                Bitmap b = new Bitmap(image.Width, image.Height);
                Graphics g = Graphics.FromImage(b);
                g.Clear(Color.White);
                g.SmoothingMode = SmoothingMode.HighQuality;
                g.InterpolationMode = InterpolationMode.High;
                g.DrawImage(image, 0, 0, image.Width, image.Height);
                switch (wmType)
                {
                    case WaterMarkType.TextMark:
                        this.addWatermarkText(g, wmContent, wmPosition, image.Width, image.Height);
                        break;
                    case WaterMarkType.ImageMark:
                        Image waterMarkImage = Image.FromFile(wmContent);
                        this.addWatermarkImage(g, waterMarkImage, wmPosition, image.Width, image.Height);
                    break;
                }
                b.Save(newpath,ImageFormat.Jpeg);
                b.Dispose();
                image.Dispose();
            }
            catch(Exception ex)
            {
                MessageBox.Show("图片生成水印异常:"+ex.Message);
            }
            finally
            {

            }
        }
コード例 #2
0
        public InMemoryImage Render(InMemoryImage sourceImage, WaterMarkPosition position)
        {
            //from http://www.codeproject.com/KB/GDI-plus/watermark.aspx

            Graphics gSource = Graphics.FromImage(sourceImage.Image);

            ImageAttributes imageAttributes = new ImageAttributes();
            ColorMap        colorMap        = new ColorMap();

            // The first step in manipulating the watermark image is to replace the
            // background color (green) with one that is transparent (Alpha=0, R=0, G=0, B=0).

            colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
            colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
            ColorMap[] remapTable = { colorMap };

            imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);

            //The second color manipulation is used to change the opacity of the watermark.
            //This is done by applying a 5x5 matrix that contains the coordinates for the RGBA space.
            //By setting the 3rd row and 3rd column to 0.3f we achieve a level of opacity.
            //The result is a watermark which slightly shows the underlying image.

            float[][] colorMatrixElements =
            {
                new float[] { 1.0f, 0.0f, 0.0f, 0.0f, 0.0f },
                new float[] { 0.0f, 1.0f, 0.0f, 0.0f, 0.0f },
                new float[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f },
                new float[] { 0.0f, 0.0f, 0.0f, 0.5f, 0.0f },
                new float[] { 0.0f, 0.0f, 0.0f, 0.0f, 1.0f }
            };

            ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);

            imageAttributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

            int xPosOfWm = Math.Min(sourceImage.Image.Width / 25, 10);
            int yPosOfWm = Math.Min(sourceImage.Image.Height / 25, 10);

            Point watPosition = GetWatermarkCoordinates(sourceImage, position);

            gSource.DrawImage(watermark,
                              new Rectangle(watPosition.X, watPosition.Y, watermark.Width, watermark.Height),
                              0,
                              0,
                              watermark.Width,
                              watermark.Height,
                              GraphicsUnit.Pixel,
                              imageAttributes);


            gSource.Dispose();

            return(sourceImage);
        }
コード例 #3
0
        private Point GetWatermarkCoordinates(InMemoryImage sourceImage, WaterMarkPosition position)
        {
            float safeMargin = 0.05f;
            float xOffsetPercent = 0f, yOffsetPercent = 0f;
            float xW, yW = 0;             //position of left upper corner of watermark

            switch (position)
            {
            case WaterMarkPosition.Center:
                xOffsetPercent = 50;
                yOffsetPercent = 50;
                break;

            case WaterMarkPosition.RightBottom:
                xOffsetPercent = 95;
                yOffsetPercent = 95;
                break;

            case WaterMarkPosition.RightUpper:
                xOffsetPercent = 95;
                yOffsetPercent = 5;
                break;

            case WaterMarkPosition.LeftBottom:
                xOffsetPercent = 5;
                yOffsetPercent = 95;
                break;

            case WaterMarkPosition.LeftUpper:
                xOffsetPercent = 5;
                yOffsetPercent = 5;
                break;

            case WaterMarkPosition.CenterUpper:
                xOffsetPercent = 50;
                yOffsetPercent = 5;
                break;
            }
            xOffsetPercent = xOffsetPercent / 100;
            yOffsetPercent = yOffsetPercent / 100;
            //
            xW = (sourceImage.Image.Size.Height * (xOffsetPercent)) - watermark.Size.Height * xOffsetPercent;
            yW = (sourceImage.Image.Size.Width * (yOffsetPercent)) - watermark.Size.Width * yOffsetPercent;

            //set safe margins
            xW = Math.Max(xW, sourceImage.Image.Size.Height * safeMargin);
            xW = Math.Min(xW, sourceImage.Image.Size.Height * (1 - safeMargin) - watermark.Size.Height);

            yW = Math.Max(yW, sourceImage.Image.Size.Width * safeMargin);
            yW = Math.Min(yW, sourceImage.Image.Size.Width * (1 - safeMargin) - watermark.Size.Width);

            return(new Point(Convert.ToInt32(yW), Convert.ToInt32(xW)));
        }
コード例 #4
0
        private void addWatermarkText(Graphics picture, string _watermarkText, WaterMarkPosition _watermarkPosition, int _width, int _height)
        {
            int[] array = new int[]
            {
                32,
                30,
                28,
                26,
                24,
                22,
                20,
                18,
                16,
                14,
                12,
                10,
                8,
                6,
                4
            };
            Font  font  = null;
            SizeF sizeF = default(SizeF);

            for (int i = 0; i < array.Length; i++)
            {
                font  = new Font("Arial Black", (float)array[i], FontStyle.Bold);
                sizeF = picture.MeasureString(_watermarkText, font);
                if ((ushort)sizeF.Width < (ushort)_width)
                {
                    break;
                }
            }
            Bitmap   bitmap   = new Bitmap((int)sizeF.Width + 3, (int)sizeF.Height + 3, PixelFormat.Format32bppArgb);
            Graphics graphics = Graphics.FromImage(bitmap);
            PointF   pointF   = new PointF(0f, 0f);
            Brush    brush    = new SolidBrush(Color.FromArgb(255, Color.Black));
            Brush    brush2   = new SolidBrush(Color.FromArgb(255, Color.Black));

            graphics.DrawString(_watermarkText, font, brush, pointF.X, pointF.Y + 1f);
            graphics.DrawString(_watermarkText, font, brush, pointF.X + 1f, pointF.Y);
            graphics.DrawString(_watermarkText, font, brush2, pointF.X + 1f, pointF.Y + 1f);
            graphics.DrawString(_watermarkText, font, brush2, pointF.X, pointF.Y + 2f);
            graphics.DrawString(_watermarkText, font, brush2, pointF.X + 2f, pointF.Y);
            brush.Dispose();
            brush2.Dispose();
            graphics.SmoothingMode = SmoothingMode.HighQuality;
            graphics.DrawString(_watermarkText, font, new SolidBrush(Color.White), pointF.X, pointF.Y, StringFormat.GenericDefault);
            graphics.Save();
            graphics.Dispose();
            this.addWatermarkImage(picture, new Bitmap(bitmap), WaterMarkPosition.WMP_Right_Bottom, _width, _height);
        }
コード例 #5
0
        /// <summary>
        /// 添加水印(分图片水印与文字水印两种)
        /// </summary>
        /// <param name="oldpath">原图片绝对地址</param>
        /// <param name="newpath">新图片放置的绝对地址</param>
        /// <param name="wmpPosition">要添加的水印的位置</param>
        /// <param name="wmtType">要添加的水印的类型</param>
        /// <param name="sWaterMarkContent">水印内容,若添加文字水印,此即为要添加的文字;
        /// 若要添加图片水印,此为图片的路径</param>
        public void addWaterMark(string oldpath, string newpath, WaterMarkPosition wmpPosition,
                                 WaterMarkType wmtType, string sWaterMarkContent)
        {
            try
            {
                Image image = Image.FromFile(oldpath);

                Bitmap b = new Bitmap(image.Width, image.Height,
                                      PixelFormat.Format24bppRgb);

                Graphics g = Graphics.FromImage(b);
                g.Clear(Color.White);
                g.SmoothingMode     = SmoothingMode.HighQuality;
                g.InterpolationMode = InterpolationMode.High;

                g.DrawImage(image, 0, 0, image.Width, image.Height);

                switch (wmtType)
                {
                case WaterMarkType.TextMark:
                    //文字水印
                    this.addWatermarkText(g, sWaterMarkContent, wmpPosition.ToString(),
                                          image.Width, image.Height);
                    break;

                case WaterMarkType.ImageMark:
                    //图片水印
                    Image imageWatermark = Image.FromFile(sWaterMarkContent);
                    this.addWatermarkImage(g, imageWatermark, wmpPosition.ToString(), image.Width, image.Height);
                    break;
                }

                b.Save(newpath);
                b.Dispose();
                image.Dispose();
            }
            catch
            {
                if (File.Exists(oldpath))
                {
                    //File.Delete(oldpath);
                }
            }
            finally
            {
                if (File.Exists(oldpath))
                {
                    //File.Delete(oldpath);
                }
            }
        }
コード例 #6
0
ファイル: ImgHelper.cs プロジェクト: xtablab27/xtab.tools
        public static Bitmap WaterMark(Image Image, Image WaterImage, WaterMarkPosition WaterMarkPosition = WaterMarkPosition.Center)
        {
            switch (WaterMarkPosition)
            {
            case WaterMarkPosition.Right:
                WaterImage = LimitSize(WaterImage, new Size(Image.Width / 3, 0));

                using (var g = Graphics.FromImage(Image))
                {
                    g.DrawImage(WaterImage, Image.Width - (WaterImage.Width + 20), Image.Height - (WaterImage.Height + 20));
                }

                break;

            case WaterMarkPosition.Left:
                WaterImage = LimitSize(WaterImage, new Size(Image.Width / 3, 0));

                using (var g = Graphics.FromImage(Image))
                {
                    g.DrawImage(WaterImage, 20, Image.Height - (WaterImage.Height + 20));
                }

                break;

            case WaterMarkPosition.Center:
                WaterImage = LimitSize(WaterImage, new Size((Image.Width / 3) * 2, 0));
                using (var g = Graphics.FromImage(Image))
                {
                    g.DrawImage(WaterImage, (Image.Width - WaterImage.Width) / 2, (Image.Height / 3) * 2);
                }

                break;
            }

            return((Bitmap)Image);
        }
コード例 #7
0
ファイル: ImageHelper.cs プロジェクト: led-os/game-center
        /// <summary>
        /// 给图片添加图片水印
        /// </summary>
        /// <param name="sourceImageFile">源图片路径</param>
        /// <param name="waterMarkImageFile">水印图片路径</param>
        /// <param name="resultImageFile">结果图片保存路径</param>
        /// <param name="position">水印在图片中的位置</param>
        /// <param name="deleteSouceImageFile">是否删除源图片</param>
        /// <returns>添加图片水印是否成功</returns>
        public static bool AddImageWaterMark
        (
            string sourceImageFile,
            string waterMarkImageFile,
            string resultImageFile,
            WaterMarkPosition position,
            bool deleteSouceImageFile
        )
        {
            bool result = false;

            {
                if (FileHelper.CheckExists(sourceImageFile) && FileHelper.CheckExists(waterMarkImageFile))
                {
                    using (System.Drawing.Image sourceImage = System.Drawing.Image.FromFile(sourceImageFile))
                    {
                        using (System.Drawing.Image waterMarkImage = System.Drawing.Image.FromFile(waterMarkImageFile))
                        {
                            using (Graphics g = Graphics.FromImage(sourceImage))
                            {
                                switch (position)
                                {
                                    #region LeftTop
                                case WaterMarkPosition.LeftTop:
                                    g.DrawImage
                                    (
                                        waterMarkImage,
                                        new Rectangle(0, 0, waterMarkImage.Width, waterMarkImage.Height),
                                        new Rectangle(0, 0, waterMarkImage.Width, waterMarkImage.Height),
                                        GraphicsUnit.Pixel
                                    );
                                    break;
                                    #endregion

                                    #region LeftBottom
                                case WaterMarkPosition.LeftBottom:
                                    g.DrawImage
                                    (
                                        waterMarkImage,
                                        new Rectangle(0, sourceImage.Height - waterMarkImage.Height, waterMarkImage.Width, waterMarkImage.Height),
                                        new Rectangle(0, 0, waterMarkImage.Width, waterMarkImage.Height),
                                        GraphicsUnit.Pixel
                                    );
                                    break;
                                    #endregion

                                    #region RightTop
                                case WaterMarkPosition.RightTop:
                                    g.DrawImage
                                    (
                                        waterMarkImage,
                                        new Rectangle(sourceImage.Width - waterMarkImage.Width, 0, waterMarkImage.Width, waterMarkImage.Height),
                                        new Rectangle(0, 0, waterMarkImage.Width, waterMarkImage.Height),
                                        GraphicsUnit.Pixel
                                    );
                                    break;
                                    #endregion

                                    #region RightBottom
                                case WaterMarkPosition.RightBottom:
                                    g.DrawImage
                                    (
                                        waterMarkImage,
                                        new Rectangle(sourceImage.Width - waterMarkImage.Width, sourceImage.Height - waterMarkImage.Height, waterMarkImage.Width, waterMarkImage.Height),
                                        new Rectangle(0, 0, waterMarkImage.Width, waterMarkImage.Height),
                                        GraphicsUnit.Pixel
                                    );
                                    break;
                                    #endregion
                                }

                                sourceImage.Save(resultImageFile, sourceImage.RawFormat);
                            }
                        }
                    }

                    if (deleteSouceImageFile)
                    {
                        FileHelper.DeleteFile(sourceImageFile, true);
                    }

                    result = true;
                }
            }

            return(result);
        }
コード例 #8
0
ファイル: ImageWaterMark.cs プロジェクト: liuhuachao/weixin
        /// <summary>
        ///  加水印图片
        /// </summary>
        /// <param name="picture">Image对象</param>
        /// <param name="iTheImage">Image对象(以此图片为水印)</param>
        /// <param name="_watermarkPosition">水印位置</param>
        /// <param name="_width">被加水印图片的宽</param>
        /// <param name="_height">被加水印图片的高</param>
        private void addWatermarkImage(Graphics picture, Image iTheImage, WaterMarkPosition _waterMarkPosition, int _width, int _height)
        {
            Image watermark = new Bitmap(iTheImage);
            ImageAttributes imageAttributes = new ImageAttributes();
            ColorMap colorMap = new ColorMap();
            colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
            colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
            ColorMap[] remapTable = {colorMap};
            imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);
            float[][] colorMatrixElements =
            {
                new float[] {1.0f, 0.0f, 0.0f, 0.0f, 0.0f},
                new float[] {0.0f, 1.0f, 0.0f, 0.0f, 0.0f},
                new float[] {0.0f, 0.0f, 1.0f, 0.0f, 0.0f},
                new float[] {0.0f, 0.0f, 0.0f, 0.3f, 0.0f},
                new float[] {0.0f, 0.0f, 0.0f, 0.0f, 1.0f}
            };
            ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);
            imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
            int xpos = 0;
            int ypos = 0;
            int WatermarkWidth = 0;
            int WatermarkHeight = 0;
            double bl = 1d;
            //计算水印图片的比率
            //取背景的1/4宽度来比较
            if ((_width > watermark.Width * 4) && (_height > watermark.Height * 4))
            {
                bl = 1;
            }
            else if ((_width > watermark.Width * 4) && (_height<watermark.Height * 4))
            {
                bl = Convert.ToDouble(_height / 4) / Convert.ToDouble(watermark.Height);

            }
            else if ((_width < watermark.Width * 4) && (_height > watermark.Height * 4))
            {
                bl = Convert.ToDouble(_width / 4) / Convert.ToDouble(watermark.Width);
            }
            else
            {
                if ((_width * watermark.Height) > (_height * watermark.Width))
                {
                    bl = Convert.ToDouble(_height / 4) / Convert.ToDouble(watermark.Height);

                }
                else
                {
                    bl = Convert.ToDouble(_width / 4) / Convert.ToDouble(watermark.Width);

                }

            }
            WatermarkWidth = Convert.ToInt32(watermark.Width * bl);
            WatermarkHeight = Convert.ToInt32(watermark.Height * bl);
            switch (_waterMarkPosition)
            {
                case WaterMarkPosition.LEFT_TOP:
                    xpos = 10;
                    ypos = 10;
                    break;
                case WaterMarkPosition.RIGHT_TOP:
                    xpos = _width - WatermarkWidth - 10;
                    ypos = 10;
                    break;
                case WaterMarkPosition.RIGHT_BOTTOM:
                    xpos = _width - WatermarkWidth - 10;
                    ypos = _height -WatermarkHeight - 10;
                    break;
                case WaterMarkPosition.LEFT_BOTTOM:
                    xpos = 10;
                    ypos = _height - WatermarkHeight - 10;
                    break;
            }
            picture.DrawImage( watermark, new Rectangle(xpos, ypos, WatermarkWidth, WatermarkHeight), 0, 0, watermark.Width, watermark.Height,GraphicsUnit.Pixel, imageAttributes);
            watermark.Dispose();
            imageAttributes.Dispose();
        }
コード例 #9
0
        /// <summary>
        /// 图片水印
        /// </summary>
        /// <param name="imgPath">原图绝对路径</param>
        /// <param name="savePath">生成图绝对路径</param>
        /// <param name="waterMarkPath">水印图片路径</param>
        /// <param name="wmPosition">水印位置</param>
        /// <param name="transparency">透明度(0-1)</param>
        /// <returns></returns>
        public static bool AddImageWaterMark(string imgPath, string savePath, string waterMarkPath, WaterMarkPosition wmPosition, float transparency)
        {
            Bitmap          image           = null;
            Graphics        g               = null;
            Image           watermark       = null;
            ImageAttributes imageAttributes = null;

            try
            {
                //创建一个图片对象用来装载要被添加水印的图片
                Image image1 = Image.FromStream(ByteToStream(SetImageToByteArray(imgPath)));
                image = new Bitmap(image1);
                image1.Dispose();
                g = Graphics.FromImage(image);
                //设置高质量插值法
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                //设置高质量,低速度呈现平滑程度
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;


                watermark = new Bitmap(waterMarkPath);
                //if (watermark.Height >= image.Height || watermark.Width >= image.Width)
                //{
                //    //如果水印图片高宽超过原始图片则返回

                //    g.Dispose();
                //    image.Dispose();
                //    watermark.Dispose();
                //    return false;
                //}

                imageAttributes = new ImageAttributes();
                ColorMap colorMap = new ColorMap();

                colorMap.OldColor = Color.FromArgb(255, 0, 255, 0);
                colorMap.NewColor = Color.FromArgb(0, 0, 0, 0);
                ColorMap[] remapTable = { colorMap };

                imageAttributes.SetRemapTable(remapTable, ColorAdjustType.Bitmap);



                float[][] colorMatrixElements =
                {
                    new float[] { 1.0f, 0.0f, 0.0f,         0.0f, 0.0f },
                    new float[] { 0.0f, 1.0f, 0.0f,         0.0f, 0.0f },
                    new float[] { 0.0f, 0.0f, 1.0f,         0.0f, 0.0f },
                    new float[] { 0.0f, 0.0f, 0.0f, transparency, 0.0f },
                    new float[] { 0.0f, 0.0f, 0.0f,         0.0f, 1.0f }
                };

                ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);

                imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

                int xpos = 0;
                int ypos = 0;

                switch (wmPosition)
                {
                case WaterMarkPosition.Left_Top:
                    xpos = (int)(image.Width * (float).01);
                    ypos = (int)(image.Height * (float).01);
                    break;

                case WaterMarkPosition.Right_Top:
                    xpos = (int)((image.Width * (float).99) - (watermark.Width));
                    ypos = (int)(image.Height * (float).01);
                    break;

                case WaterMarkPosition.Middle:
                    xpos = (int)((image.Width * (float).50) - (watermark.Width / 2));
                    ypos = (int)((image.Height * (float).50) - (watermark.Height / 2));
                    break;

                case WaterMarkPosition.Left_Bottom:
                    xpos = (int)(image.Width * (float).01);
                    ypos = (int)((image.Height * (float).99) - watermark.Height);
                    break;

                case WaterMarkPosition.Right_Bottom:
                    xpos = (int)((image.Width * (float).99) - (watermark.Width));
                    ypos = (int)((image.Height * (float).99) - watermark.Height);
                    break;
                }

                g.DrawImage(watermark, new Rectangle(xpos, ypos, watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes);



                ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
                ImageCodecInfo   ici    = null;
                foreach (ImageCodecInfo codec in codecs)
                {
                    if (codec.MimeType.IndexOf("jpeg") > -1)
                    {
                        ici = codec;
                    }
                }
                EncoderParameters encoderParams = new EncoderParameters();
                long[]            qualityParam  = new long[1];

                qualityParam[0] = 80;

                EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam);
                encoderParams.Param[0] = encoderParam;

                if (ici != null)
                {
                    image.Save(savePath, ici, encoderParams);
                }
                else
                {
                    image.Save(savePath);
                }

                g.Dispose();
                image.Dispose();
                watermark.Dispose();
                imageAttributes.Dispose();


                return(true);
            }
            catch
            {
                return(false);
            }
            finally
            {
                if (g != null)
                {
                    g.Dispose();
                }
                if (image != null)
                {
                    image.Dispose();
                }
                if (watermark != null)
                {
                    watermark.Dispose();
                }
                if (imageAttributes != null)
                {
                    imageAttributes.Dispose();
                }
            }
        }
コード例 #10
0
        public static Texture2D AddWatermark(Texture2D background, Texture2D watermark, WaterMarkPosition position = WaterMarkPosition.RightTop)
        {
            int startX = 0;
            int startY = 0;

            switch (position)
            {
            case WaterMarkPosition.RightTop:
                startX = 0;
                startY = background.height - watermark.height;
                break;

            case WaterMarkPosition.LeftTop:
                startX = background.width - watermark.width;
                startY = background.height - watermark.height;
                break;

            case WaterMarkPosition.RightBottom:
                startX = 0;
                startY = 0;
                break;

            case WaterMarkPosition.LeftBottom:
                startX = background.width - watermark.width;
                startY = 0;
                break;
            }


            for (int x = startX; x < background.width; x++)
            {
                for (int y = startY; y < background.height; y++)
                {
                    Color bgColor = background.GetPixel(x, y);
                    Color wmColor = watermark.GetPixel(x - startX, y - startY);

                    Color final_color = Color.Lerp(bgColor, wmColor, wmColor.a / 1.0f);

                    background.SetPixel(x, y, final_color);
                }
            }

            background.Apply();
            return(background);
        }
コード例 #11
0
ファイル: ImageTools.cs プロジェクト: xiangzuoyu/FancyFix.OA
        /// <summary>
        /// 生成图片或文字水印
        /// </summary>
        /// <param name="imgPath">原图绝对路径</param>
        /// <param name="savePath">生成图路径</param>
        /// <param name="waterMarkPathOrmarkText">水印图片路径或文字水印内容</param>
        /// <param name="wmType">水印类型,文字或者图片</param>
        /// <param name="wmPosition">水印位置</param>
        /// <param name="transparency">透明度</param>
        /// <returns>是否成功</returns>
        public static bool AddWaterMark(string imgPath, string savePath, string waterMarkPathOrmarkText, WaterMarkType wmType, WaterMarkPosition wmPosition, float transparency)
        {
            switch (wmType)
            {
            case WaterMarkType.ImageMark:
                return(AddImageWaterMark(imgPath, savePath, waterMarkPathOrmarkText, wmPosition, transparency));

            case WaterMarkType.TextMark:
                return(AddTextWaterMark(imgPath, savePath, waterMarkPathOrmarkText, wmPosition, transparency, 0f));

            default:
                return(false);
            }
        }
コード例 #12
0
        /// <summary>
        /// 文字水印
        /// </summary>
        /// <param name="imgPath">原图绝对路径</param>
        /// <param name="savePath">生成图绝对路径</param>
        /// <param name="markText">水印文字</param>
        /// <param name="wmPosition">水印位置</param>
        /// <param name="transparency">透明度(0-1)</param>
        /// <param name="fontSize">文字大小,0 则随图片自动调整大小</param>
        /// <returns></returns>
        public static bool AddTextWaterMark(string imgPath, string savePath, string markText, WaterMarkPosition wmPosition, float transparency, float fontSize)
        {
            Bitmap   image = null;
            Graphics g     = null;

            try
            {
                //创建一个图片对象用来装载要被添加水印的图片
                Image image1 = Image.FromStream(ByteToStream(SetImageToByteArray(imgPath)));
                image = new Bitmap(image1);
                image1.Dispose();
                g = Graphics.FromImage(image);
                //设置高质量插值法
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
                //设置高质量,低速度呈现平滑程度
                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;


                //************* 文字水印 *****************
                int imgWidth  = image.Width;
                int imgHeight = image.Height;


                int[] sizes = new int[] { 48, 36, 28, 24, 16, 14, 12, 10 };

                Font  font   = null;
                SizeF crSize = new SizeF();

                if (fontSize == 0)
                {
                    //利用一个循环语句来选择我们要添加文字的型号
                    //直到它的长度比图片的宽度小
                    for (int i = 0; i < sizes.Length; i++)
                    {
                        font = new Font("宋体", sizes[i], FontStyle.Bold);

                        //测量用指定的 Font 对象绘制并用指定的 StringFormat 对象格式化的指定字符串。
                        crSize = g.MeasureString(markText, font);

                        // ushort 关键字表示一种整数数据类型
                        if ((ushort)crSize.Width < (ushort)imgWidth * 0.5)
                        {
                            break;
                        }
                    }
                }
                else
                {
                    font   = new Font("宋体", fontSize, FontStyle.Bold); //定义字体 ;
                    crSize = g.MeasureString(markText, font);
                }


                int xpos = 0;
                int ypos = 0;

                switch (wmPosition)
                {
                case WaterMarkPosition.Left_Top:
                    xpos = (int)(image.Width * (float).01);
                    ypos = (int)(image.Height * (float).01);
                    break;

                case WaterMarkPosition.Right_Top:
                    xpos = (int)((image.Width * (float).99) - (crSize.Width));
                    ypos = (int)(image.Height * (float).01);
                    break;

                case WaterMarkPosition.Middle:
                    xpos = (int)((image.Width * (float).50) - (crSize.Width / 2));
                    ypos = (int)((image.Height * (float).50) - (crSize.Height / 2));
                    break;

                case WaterMarkPosition.Left_Bottom:
                    xpos = (int)(image.Width * (float).01);
                    ypos = (int)((image.Height * (float).99) - crSize.Height);
                    break;

                case WaterMarkPosition.Right_Bottom:
                    xpos = (int)((image.Width * (float).99) - (crSize.Width));
                    ypos = (int)((image.Height * (float).99) - crSize.Height);
                    break;
                }

                g.DrawString(markText, font, new SolidBrush(Color.FromArgb((int)(transparency * 255), 0, 0, 0)), xpos + 1, ypos + 1);
                g.DrawString(markText, font, new SolidBrush(Color.FromArgb((int)(transparency * 255), 255, 255, 255)), xpos, ypos);


                ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
                ImageCodecInfo   ici    = null;
                foreach (ImageCodecInfo codec in codecs)
                {
                    if (codec.MimeType.IndexOf("jpeg") > -1)
                    {
                        ici = codec;
                    }
                }
                EncoderParameters encoderParams = new EncoderParameters();
                long[]            qualityParam  = new long[1];

                qualityParam[0] = 80;

                EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam);
                encoderParams.Param[0] = encoderParam;

                if (ici != null)
                {
                    image.Save(savePath, ici, encoderParams);
                }
                else
                {
                    image.Save(savePath);
                }

                return(true);
            }
            catch
            {
                return(false);
            }
            finally
            {
                g.Dispose();
                image.Dispose();
            }
        }
コード例 #13
0
        public void AddWaterMark(Image waterMark, WaterMarkPosition position, float angle) {
            if (angle != 0) {
                Rotate(ref waterMark, angle);
            }

            var attributes = new ImageAttributes();

            float[][] colorMatrixElements = { 
												new float[] {1.0f,  0.0f,  0.0f,  0.0f, 0.0f},       
												new float[] {0.0f,  1.0f,  0.0f,  0.0f, 0.0f},        
												new float[] {0.0f,  0.0f,  1.0f,  0.0f, 0.0f},        
												new float[] {0.0f,  0.0f,  0.0f,  0.3f, 0.0f},        
												new float[] {0.0f,  0.0f,  0.0f,  0.0f, 1.0f}};
            ColorMatrix wmColorMatrix = new ColorMatrix(colorMatrixElements);

            attributes.SetColorMatrix(wmColorMatrix, ColorMatrixFlag.Default,
                ColorAdjustType.Bitmap);

            int xPosOfWm, yPosOfWm;
            switch (position) {
                case WaterMarkPosition.Center:
                    xPosOfWm = (CurrentImage.Width / 2) - (waterMark.Width / 2);
                    yPosOfWm = (CurrentImage.Height / 2) - (waterMark.Height / 2);
                    break;
                case WaterMarkPosition.RightTop:
                default:
                    xPosOfWm = CurrentImage.Width - waterMark.Width - 10;
                    yPosOfWm = 10;
                    break;
            }

            using (var graphics = Graphics.FromImage(CurrentImage)) {
                SetHighQuality(graphics);
                graphics.DrawImage(waterMark,
                    new Rectangle(xPosOfWm, yPosOfWm, waterMark.Width, waterMark.Height),  //Set the detination Position
                    0,                  // x-coordinate of the portion of the source image to draw. 
                    0,                  // y-coordinate of the portion of the source image to draw. 
                    waterMark.Width,    // Watermark Width
                    waterMark.Height,   // Watermark Height
                    GraphicsUnit.Pixel, // Unit of measurment
                    attributes);   //ImageAttributes Object
            }
        }
コード例 #14
0
        public void addWaterMark(string oldpath, string newpath, WaterMarkType wmtType, string sWaterMarkContent, WaterMarkPosition position)
        {
            try
            {
                Image    image    = Image.FromFile(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);
                switch (wmtType)
                {
                case WaterMarkType.TextMark:
                    this.addWatermarkText(graphics, sWaterMarkContent, position, image.Width, image.Height);
                    break;

                case WaterMarkType.ImageMark:
                    this.addWatermarkImage(graphics, HttpContext.Current.Server.MapPath(sWaterMarkContent), position, image.Width, image.Height);
                    break;
                }
                image.Dispose();
                if (newpath != "")
                {
                    bitmap.Save(newpath);
                }
                else
                {
                    bitmap.Save(oldpath);
                }
                bitmap.Dispose();
            }
            catch
            {
                if (newpath != "" && File.Exists(oldpath))
                {
                    File.Delete(oldpath);
                }
            }
            finally
            {
                if (newpath != "" && File.Exists(oldpath))
                {
                    File.Delete(oldpath);
                }
            }
        }
コード例 #15
0
        private void addWatermarkImage(Graphics picture, Image iTheImage, WaterMarkPosition _watermarkPosition, int _width, int _height)
        {
            Image           image           = new Bitmap(iTheImage);
            ImageAttributes imageAttributes = new ImageAttributes();

            ColorMap[] map = new ColorMap[]
            {
                new ColorMap
                {
                    OldColor = Color.FromArgb(255, 0, 255, 0),
                    NewColor = Color.FromArgb(0, 0, 0, 0)
                }
            };
            imageAttributes.SetRemapTable(map, ColorAdjustType.Bitmap);
            float[][] array    = new float[5][];
            float[][] arg_6F_0 = array;
            int       arg_6F_1 = 0;

            float[] array2 = new float[5];
            array2[0]          = 1f;
            arg_6F_0[arg_6F_1] = array2;
            float[][] arg_86_0 = array;
            int       arg_86_1 = 1;

            float[] array3 = new float[5];
            array3[1]          = 1f;
            arg_86_0[arg_86_1] = array3;
            float[][] arg_9D_0 = array;
            int       arg_9D_1 = 2;

            float[] array4 = new float[5];
            array4[2]          = 1f;
            arg_9D_0[arg_9D_1] = array4;
            float[][] arg_B4_0 = array;
            int       arg_B4_1 = 3;

            float[] array5 = new float[5];
            array5[3]          = 0.3f;
            arg_B4_0[arg_B4_1] = array5;
            array[4]           = new float[]
            {
                0f,
                0f,
                0f,
                0f,
                1f
            };
            float[][]   newColorMatrix  = array;
            ColorMatrix newColorMatrix2 = new ColorMatrix(newColorMatrix);

            imageAttributes.SetColorMatrix(newColorMatrix2, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
            int    x = 0;
            int    y = 0;
            double num;

            if (_width > image.Width * 2 && _height > image.Height * 2)
            {
                num = 1.0;
            }
            else
            {
                if (_width > image.Width * 2 && _height < image.Height * 2)
                {
                    num = Convert.ToDouble(_height / 2) / Convert.ToDouble(image.Height);
                }
                else
                {
                    if (_width < image.Width * 2 && _height > image.Height * 2)
                    {
                        num = Convert.ToDouble(_width / 2) / Convert.ToDouble(image.Width);
                    }
                    else
                    {
                        if (_width * image.Height > _height * image.Width)
                        {
                            num = Convert.ToDouble(_height / 2) / Convert.ToDouble(image.Height);
                        }
                        else
                        {
                            num = Convert.ToDouble(_width / 2) / Convert.ToDouble(image.Width);
                        }
                    }
                }
            }
            int num2 = Convert.ToInt32((double)image.Width * num);
            int num3 = Convert.ToInt32((double)image.Height * num);

            switch (_watermarkPosition)
            {
            case WaterMarkPosition.WMP_Left_Top:
                x = 10;
                y = 10;
                break;

            case WaterMarkPosition.WMP_Left_Bottom:
                x = 10;
                y = _height - num3 - 10;
                break;

            case WaterMarkPosition.WMP_Right_Top:
                x = _width - num2 - 10;
                y = 10;
                break;

            case WaterMarkPosition.WMP_Right_Bottom:
                x = _width - num2 - 10;
                y = _height - num3 - 10;
                break;
            }
            picture.DrawImage(image, new Rectangle(x, y, num2, num3), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttributes);
            image.Dispose();
            imageAttributes.Dispose();
        }
コード例 #16
0
        private void addWatermarkImage(Graphics picture, string WaterMarkPicPath, WaterMarkPosition _watermarkPosition, int _width, int _height)
        {
            Image iTheImage = new Bitmap(WaterMarkPicPath);

            this.addWatermarkImage(picture, iTheImage, _watermarkPosition, _width, _height);
        }
コード例 #17
0
ファイル: ImageWaterMark.cs プロジェクト: liuhuachao/weixin
 /// <summary>
 /// 加水印文字
 /// </summary>
 /// <param name="picture">imge 对象</param>
 /// <param name="_watermarkText">水印文字内容</param>
 /// <param name="_watermarkPosition">水印位置</param>
 /// <param name="_width">被加水印图片的宽</param>
 /// <param name="_height">被加水印图片的高</param>
 private void addWatermarkText(Graphics picture, string _watermarkText,WaterMarkPosition _waterMarkPosition, int _width, int _height)
 {
     // 确定水印文字的字体大小
     int[] sizes = new int[]{32, 30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4};
     Font crFont = null;
     SizeF crSize = new SizeF();
     for (int i = 0;i < sizes.Length; i++)
     {
         crFont = new Font("Arial Black", sizes[i], FontStyle.Bold);
         crSize = picture.MeasureString(_watermarkText, crFont);
         if((ushort)crSize.Width < (ushort)_width)
         {
             break;
         }
     }
     // 生成水印图片(将文字写到图片中)
     Bitmap floatBmp = new Bitmap((int)crSize.Width + 3, (int)crSize.Height + 3, PixelFormat.Format32bppArgb);
     Graphics fg=Graphics.FromImage(floatBmp);
     PointF pt=new PointF(0,0);
     // 画阴影文字
     Brush TransparentBrush0 = new SolidBrush(Color.FromArgb(255, Color.Black));
     Brush TransparentBrush1 = new SolidBrush(Color.FromArgb(255, Color.Black));
     fg.DrawString(_watermarkText,crFont,TransparentBrush0, pt.X, pt.Y + 1);
     fg.DrawString(_watermarkText,crFont,TransparentBrush0, pt.X + 1, pt.Y);
     fg.DrawString(_watermarkText,crFont,TransparentBrush1, pt.X + 1, pt.Y + 1);
     fg.DrawString(_watermarkText,crFont,TransparentBrush1, pt.X, pt.Y + 2);
     fg.DrawString(_watermarkText,crFont,TransparentBrush1, pt.X + 2, pt.Y);
     TransparentBrush0.Dispose();
     TransparentBrush1.Dispose();
     // 画文字
     fg.SmoothingMode=System.Drawing.Drawing2D.SmoothingMode.HighQuality;
     fg.DrawString(_watermarkText, crFont, new SolidBrush(Color.White), pt.X, pt.Y, StringFormat.GenericDefault);
     // 保存刚才的操作
     fg.Save();
     fg.Dispose();
     // 将水印图片加到原图中
     this.addWatermarkImage(picture, new Bitmap(floatBmp),_waterMarkPosition, _width, _height);
 }
コード例 #18
0
        private void buttonCreate_Click(object sender, EventArgs e)
        {
            if (textBoxPicture.Text.Trim() == "")
            {
                MessageBox.Show("原圖不存在!");
                return;
            }
            if (textBoxWatermark.Text.Trim() == "")
            {
                MessageBox.Show("水印内容不存在!");
                return;
            }
            if (!radioButtonFont.Checked && !radioButtonPicture.Checked)
            {
                MessageBox.Show("水印類型未選擇!");
                return;
            }
            if (comboBoxPosition.Text.Trim() == "")
            {
                MessageBox.Show("水印位置未選擇!");
                return;
            }

            string SourceImagePath = textBoxPicture.Text.Trim();

            string strPath = Path.GetFullPath(SourceImagePath).Replace(Path.GetFileName(SourceImagePath), "");

            string sExt = SourceImagePath.Substring(
                SourceImagePath.LastIndexOf(".")).ToLower();

            string       SaveImagePath = strPath + DateTime.Now.Ticks.ToString() + sExt;
            WaterMarkBll objWMB        = new WaterMarkBll();

            WaterMarkPosition wmpPosition = new WaterMarkPosition();

            switch (comboBoxPosition.Text)
            {
            case "左上角":
                wmpPosition = WaterMarkPosition.WMP_Left_Top;
                break;

            case "右上角":
                wmpPosition = WaterMarkPosition.WMP_Right_Top;
                break;

            case "居中":
                wmpPosition = WaterMarkPosition.WMP_Center;
                break;

            case "左下角":
                wmpPosition = WaterMarkPosition.WMP_Left_Bottom;
                break;

            case "右下角":
                wmpPosition = WaterMarkPosition.WMP_Right_Bottom;
                break;
            }

            if (radioButtonFont.Checked)
            {
                objWMB.addWaterMark(SourceImagePath, SaveImagePath, wmpPosition, WaterMarkType.TextMark, textBoxWatermark.Text);
            }
            else
            {
                objWMB.addWaterMark(SourceImagePath, SaveImagePath, wmpPosition, WaterMarkType.ImageMark, textBoxWatermark.Text);
            }

            string ThumbnailImage = strPath + DateTime.Now.Ticks.ToString() + sExt;

            objWMB.ToThumbnailImages(SaveImagePath, ThumbnailImage, pictureBoxWatermark.Width);

            pictureBoxWatermark.Image = Image.FromFile(ThumbnailImage);
        }
コード例 #19
0
 public void AddWaterMark(Image waterMark, WaterMarkPosition position) {
     AddWaterMark(waterMark, position, 0);
 }