Exemplo n.º 1
0
 /// <summary>
 /// 添加文字水印
 /// </summary>
 /// <param name="originalFile">目标文件</param>
 /// <param name="markText">水印文字</param>
 /// <param name="waterMarkPosition">水印位置</param>
 /// <param name="textFontSize">文字大小</param>
 /// <param name="textFontFamily">文字字体</param>
 /// <param name="degrees">旋转角度,以文字中心为准</param>
 /// <returns></returns>
 public bool AddWaterMarkText(string originalFile, string markText, WaterMarkPositions waterMarkPosition, int textFontSize, string textFontFamily, int degrees)
 {
     Angle        = degrees;
     FontSize     = textFontSize;
     MyFontFamily = textFontFamily;
     return(AddWaterMarkText(originalFile, markText, waterMarkPosition));
 }
Exemplo n.º 2
0
        public virtual void PlaceWaterMark(ref Graphics g, int width, int height, WaterMarkPositions position)
        {
            if (g.DpiX > this._waterMarkImage.Width)
            {
                g.SmoothingMode = SmoothingMode.HighQuality;
            }
            byte      transparency = (byte)this._settings.Transparency;
            int       width1       = this._waterMarkImage.Width;
            int       height1      = this._waterMarkImage.Height;
            Rectangle destRect     = Rectangle(width, height, width1, height1, position);
            float     num4         = (float)transparency / 100f;

            if ((double)num4 < 0.0 || (double)num4 > 1.0)
            {
                num4 = 1f;
            }
            float[][] newColorMatrix1 = new float[5][];
            float[][] numArray1       = newColorMatrix1;
            int       index1          = 0;

            float[] numArray2 = new float[5];
            numArray2[0] = 1f;
            float[] numArray3 = numArray2;
            numArray1[index1] = numArray3;
            float[][] numArray4 = newColorMatrix1;
            int       index2    = 1;

            float[] numArray5 = new float[5];
            numArray5[1] = 1f;
            float[] numArray6 = numArray5;
            numArray4[index2] = numArray6;
            float[][] numArray7 = newColorMatrix1;
            int       index3    = 2;

            float[] numArray8 = new float[5];
            numArray8[2] = 1f;
            float[] numArray9 = numArray8;
            numArray7[index3] = numArray9;
            float[][] numArray10 = newColorMatrix1;
            int       index4     = 3;

            float[] numArray11 = new float[5];
            numArray11[3] = num4;
            float[] numArray12 = numArray11;
            numArray10[index4] = numArray12;
            float[][] numArray13 = newColorMatrix1;
            int       index5     = 4;

            float[] numArray14 = new float[5];
            numArray14[4] = 1f;
            float[] numArray15 = numArray14;
            numArray13[index5] = numArray15;
            ColorMatrix     newColorMatrix2 = new ColorMatrix(newColorMatrix1);
            ImageAttributes imageAttr       = new ImageAttributes();

            imageAttr.SetColorMatrix(newColorMatrix2);
            imageAttr.SetColorKey(Color.White, Color.White);
            g.DrawImage(this._waterMarkImage, destRect, 0, 0, width1, height1, GraphicsUnit.Pixel);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 在原图上添加水印
        /// </summary>
        /// <param name="sourceStream"></param>
        /// <param name="waterMarkText"></param>
        /// <param name="WaterMarkPositions"></param>
        /// <param name="alpha">透明度,取值0-1之间的小数</param>
        /// <param name="waterMarkQuality">水印的质量,取值0-100</param>
        /// <returns></returns>
        public static Stream AddWaterMarkText(Stream sourceStream, string waterMarkText, WaterMarkPositions WaterMarkPositions, float alpha, int waterMarkQuality)
        {
            Stream newImageStream = null;
            Image sourceImage = null;
            try
            {
                sourceImage = Image.FromStream(sourceStream, true);
            }
            catch
            {
            }

            if (sourceImage != null)
            {
                newImageStream = AddWaterMarkText(sourceImage, waterMarkText, WaterMarkPositions, alpha, waterMarkQuality);
            }

            return newImageStream;
        }
Exemplo n.º 4
0
        /// <summary>
        /// 在原图上添加水印
        /// </summary>
        /// <param name="sourceImage"></param>
        /// <param name="waterMarkText"></param>
        /// <param name="WaterMarkPositions"></param>
        /// <param name="alpha">透明度,取值0-1之间的小数</param>
        /// <param name="waterMarkQuality">水印的质量,取值0-100</param>
        /// <returns></returns>
        public static Stream AddWaterMarkText(Image sourceImage, string waterMarkText, WaterMarkPositions WaterMarkPositions, float alpha, int waterMarkQuality)
        {
            Stream newImageStream = null;
            if (sourceImage == null || string.IsNullOrEmpty(waterMarkText.TrimEnd(new char[] { ' ' })))
            {
                newImageStream = null;
            }
            else
            {
                int width = sourceImage.Width;
                int height = sourceImage.Height;
                using (Bitmap image = new Bitmap(width, height, PixelFormat.Format24bppRgb))
                {
                    image.SetResolution(sourceImage.HorizontalResolution, sourceImage.VerticalResolution);
                    using (Graphics graphics = Graphics.FromImage(image))
                    {
                        graphics.DrawImage(sourceImage, new Rectangle(0, 0, width, height), 0, 0, width, height, GraphicsUnit.Pixel);
                        AddWaterMarkText(graphics, width, height, waterMarkText, WaterMarkPositions, alpha);
                        newImageStream = new MemoryStream();
                        ImageCodecInfo encoderInfo = GetEncoderInfo("image/jpeg");
                        EncoderParameters encoderParams = new EncoderParameters(1);
                        encoderParams = new EncoderParameters();
                        encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, (long)waterMarkQuality);
                        image.Save(newImageStream, encoderInfo, encoderParams);
                        image.Dispose();
                        //sourceImage.Dispose(); //这个原来的Image不应该在这里释放
                        graphics.Dispose();
                    }
                }
            }

            return newImageStream;
        }
Exemplo n.º 5
0
        /// <summary>
        /// 在已经存在的画布上水印
        /// </summary>
        /// <param name="graphics"></param>
        /// <param name="imageWidth"></param>
        /// <param name="imageHeight"></param>
        /// <param name="watermarImagePath"></param>
        /// <param name="WaterMarkPositions"></param>
        /// <param name="alpha">透明度,取值0-1之间的小数</param>
        public static Graphics AddWaterMarkImage(Graphics graphics, int imageWidth, int imageHeight, string watermarImagePath, WaterMarkPositions WaterMarkPositions, float alpha)
        {
            if (graphics != null && File.Exists(watermarImagePath))
            {
                using (Image image = new Bitmap(watermarImagePath))
                {
                    using (ImageAttributes imageAttr = new ImageAttributes())
                    {
                        ColorMap map = new ColorMap();
                        map.OldColor = Color.FromArgb(0xff, 0, 0xff, 0);
                        map.NewColor = Color.FromArgb(0, 0, 0, 0);
                        ColorMap[] mapArray = new ColorMap[] { map };
                        imageAttr.SetRemapTable(mapArray, ColorAdjustType.Bitmap);
                        float[][] numArray2 = new float[5][];
                        float[] numArray3 = new float[5];
                        numArray3[0] = 1f;
                        numArray2[0] = numArray3;
                        numArray3 = new float[5];
                        numArray3[1] = 1f;
                        numArray2[1] = numArray3;
                        numArray3 = new float[5];
                        numArray3[2] = 1f;
                        numArray2[2] = numArray3;
                        numArray3 = new float[5];
                        numArray3[3] = alpha;
                        numArray2[3] = numArray3;
                        numArray3 = new float[5];
                        numArray3[4] = 1f;
                        numArray2[4] = numArray3;
                        float[][] newColorMatrix = numArray2;
                        ColorMatrix matrix = new ColorMatrix(newColorMatrix);
                        imageAttr.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                        int x = 0;
                        int y = 0;
                        switch (WaterMarkPositions)
                        {
                            case WaterMarkPositions.LeftTop:
                                x = 10;
                                y = 10;
                                break;

                            case WaterMarkPositions.CenterTop:
                                x = (imageWidth - image.Width) / 2;
                                y = 10;
                                break;

                            case WaterMarkPositions.RightTop:
                                x = imageWidth - image.Width - 10;
                                y = 10;
                                break;

                            case WaterMarkPositions.LeftMiddle:
                                x = 10;
                                y = (imageHeight - image.Height) / 2;
                                break;

                            case WaterMarkPositions.Center:
                                x = (imageWidth - image.Width) / 2;
                                y = (imageHeight - image.Height) / 2;
                                break;

                            case WaterMarkPositions.RightMiddle:
                                x = imageWidth - image.Width - 10;
                                y = (imageHeight - image.Height) / 2;
                                break;

                            case WaterMarkPositions.LeftBottom:
                                x = 10;
                                y = imageHeight - image.Height - 10;
                                break;

                            case WaterMarkPositions.CenterBottom:
                                x = (imageWidth - image.Width) / 2;
                                y = imageHeight - image.Height - 10;
                                break;

                            case WaterMarkPositions.RightBottom:
                                x = imageWidth - image.Width - 10;
                                y = imageHeight - image.Height - 10;
                                break;
                        }
                        graphics.DrawImage(image, new Rectangle(x, y, image.Width, image.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttr);
                        image.Dispose();
                        imageAttr.Dispose();
                    }
                }
            }

            return graphics;
        }
Exemplo n.º 6
0
        /// <summary>
        /// 在已经存在的画布上水印
        /// </summary>
        /// <param name="graphics"></param>
        /// <param name="imageWidth"></param>
        /// <param name="imageHeight"></param>
        /// <param name="waterMarkText"></param>
        /// <param name="WaterMarkPositions"></param>
        /// <param name="alpha">透明度,取值0-1之间的小数</param>
        /// <returns></returns>
        public static Graphics AddWaterMarkText(Graphics graphics, int imageWidth, int imageHeight, string waterMarkText, WaterMarkPositions WaterMarkPositions, float alpha)
        {
            if (graphics != null && !string.IsNullOrEmpty(waterMarkText.TrimEnd(new char[] { ' ' })))
            {
                graphics.SmoothingMode = SmoothingMode.AntiAlias;
                int[] numArray = new int[] { 0x10, 14, 12, 10, 8, 6, 4, 3, 2, 1 };
                Font font = null;
                SizeF ef = new SizeF();
                for (int i = 0; i < numArray.Length; i++)
                {
                    font = new Font("Verdana", (float)numArray[i], FontStyle.Bold);
                    ef = graphics.MeasureString(waterMarkText, font);
                    if (((ushort)ef.Width) < ((ushort)(imageWidth * 0.8)))
                    {
                        break;
                    }
                }
                float height = ef.Height;
                float width = ef.Width;
                float x = 0f;
                float y = 0f;
                switch (WaterMarkPositions)
                {
                    case WaterMarkPositions.LeftTop:
                        x = imageWidth * 0.01f + width / 2f;
                        y = imageHeight * 0.01f;
                        break;

                    case WaterMarkPositions.CenterTop:
                        x = ((float)imageWidth) / 2f;
                        y = imageHeight * 0.01f;
                        break;

                    case WaterMarkPositions.RightTop:
                        x = imageWidth * 0.99f - width / 2f;
                        y = imageHeight * 0.01f;
                        break;

                    case WaterMarkPositions.LeftMiddle:
                        x = imageWidth * 0.01f + width / 2f;
                        y = ((float)imageHeight) / 2f - height / 2f;
                        break;

                    case WaterMarkPositions.Center:
                        x = ((float)imageWidth) / 2f;
                        y = ((float)imageHeight) / 2f - height / 2f;
                        break;

                    case WaterMarkPositions.RightMiddle:
                        x = imageWidth * 0.99f - width / 2f;
                        y = ((float)imageHeight) / 2f - height / 2f;
                        break;

                    case WaterMarkPositions.LeftBottom:
                        x = imageWidth * 0.01f + width / 2f;
                        y = imageHeight * 0.99f - height;
                        break;

                    case WaterMarkPositions.CenterBottom:
                        x = ((float)imageWidth) / 2f;
                        y = imageHeight * 0.99f - height;
                        break;

                    case WaterMarkPositions.RightBottom:
                        x = imageWidth * 0.99f - width / 2f;
                        y = imageHeight * 0.99f - height;
                        break;
                }
                StringFormat format = new StringFormat();
                format.Alignment = StringAlignment.Center;

                using (SolidBrush brush = new SolidBrush(Color.FromArgb(Convert.ToInt32((float)(255f * alpha)), 0, 0, 0)))
                {
                    graphics.DrawString(waterMarkText, font, brush, x + 1f, y + 1f, format);
                    brush.Dispose();
                }

                using (SolidBrush brush2 = new SolidBrush(Color.FromArgb(0x99, 0xff, 0xff, 0xff)))
                {
                    graphics.DrawString(waterMarkText, font, brush2, x, y, format);
                    brush2.Dispose();
                }

            }

            return graphics;
        }
Exemplo n.º 7
0
        /// <summary>
        /// 添加文字水印
        /// </summary>
        /// <param name="originalFile">目标文件</param>
        /// <param name="markText">水印文字</param>
        /// <param name="waterMarkPosition">水印位置</param>
        /// <returns></returns>
        public bool AddWaterMarkText(string originalFile, string markText, WaterMarkPositions waterMarkPosition)
        {
            try
            {
                Image    image     = new Bitmap(originalFile);
                Graphics draw      = Graphics.FromImage(image);
                int      imgWidth  = image.Width;
                int      imgHeight = image.Height;

                Font  crFont = null;
                SizeF crSize = new SizeF();
                if (FontSize != 0)
                {
                    crFont = new Font(MyFontFamily, FontSize, FontStyle.Bold);
                    crSize = draw.MeasureString(markText, crFont);
                }
                else
                {
                    #region 文字最佳适配
                    int[] sizes = new int[] { 32, 30, 28, 26, 24, 20, 18, 16, 14, 12, 10, 8, 6, 4 };
                    for (int i = 0; i < sizes.Length; i++)
                    {
                        crFont = new Font(MyFontFamily, sizes[i], FontStyle.Bold);
                        crSize = draw.MeasureString(markText, crFont);

                        if ((ushort)crSize.Width < (ushort)imgWidth && (ushort)crSize.Height < (ushort)imgHeight)
                        {
                            break;
                        }
                    }
                    #endregion
                }

                float xpos = 0;
                float ypos = 0;

                switch (waterMarkPosition)
                {
                case WaterMarkPositions.TOP_LEFT:
                    xpos = ((float)imgWidth * (float).01) + (crSize.Width / 2);
                    ypos = (float)imgHeight * (float).01;
                    break;

                case WaterMarkPositions.TOP_RIGHT:
                    xpos = ((float)imgWidth * (float).99) - (crSize.Width / 2);
                    ypos = (float)imgHeight * (float).01;
                    break;

                case WaterMarkPositions.BOTTOM_LEFT:
                    xpos = ((float)imgWidth * (float).01) + (crSize.Width / 2);
                    ypos = ((float)imgHeight * (float).99) - crSize.Height;
                    break;

                case WaterMarkPositions.BOTTOM_RIGHT:
                    xpos = ((float)imgWidth * (float).99) - (crSize.Width / 2);
                    ypos = ((float)imgHeight * (float).99) - crSize.Height;
                    break;

                case WaterMarkPositions.CENTER:
                    xpos = ((float)imgWidth * (float).50);
                    ypos = ((float)imgHeight * (float).50) - crSize.Height;
                    break;
                }

                StringFormat StrFormat = new StringFormat();
                StrFormat.Alignment = StringAlignment.Center;

                SolidBrush semiTransBrush  = new SolidBrush(Color.FromArgb(153, 255, 255, 255));
                SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(153, 0, 0, 0));

                //尝试旋转
                System.Drawing.Drawing2D.Matrix myMatrix = new System.Drawing.Drawing2D.Matrix();
                myMatrix.RotateAt(Angle, new PointF(xpos, ypos), System.Drawing.Drawing2D.MatrixOrder.Append);
                draw.Transform = myMatrix;

                //写出文字
                draw.DrawString(markText, crFont, semiTransBrush, xpos, ypos, StrFormat);
                //加写一层阴影效果
                draw.DrawString(markText, crFont, semiTransBrush2, xpos + 1, ypos + 1, StrFormat);

                semiTransBrush2.Dispose();
                semiTransBrush.Dispose();
                Image imageOut = new Bitmap(image, imgWidth, imgHeight);
                image.Dispose();
                draw.Dispose();
                //删除原始文件
                //if (File.Exists(OriginalFile))
                //{
                //  File.Delete(OriginalFile);
                //}
                //保存新文件
                imageOut.Save(originalFile, ImageFormat.Jpeg);
                imageOut.Dispose();

                if (receiveMessage != null)
                {
                    receiveMessage(string.Format("添加文字水印成功:“{0}”", originalFile));
                }

                return(true);
            }
            catch (Exception ex)
            {
                if (receiveMessage != null)
                {
                    receiveMessage(string.Format("添加文字水印失败:“{0}”,错误:{1}", originalFile, ex));
                }
                return(false);
            }
        }
Exemplo n.º 8
0
 /// <summary>
 /// 添加文字水印
 /// </summary>
 /// <param name="originalFile">目标文件</param>
 /// <param name="markText">水印文字</param>
 /// <param name="waterMarkPosition">水印位置</param>
 /// <param name="textFontSize">文字大小</param>
 /// <returns></returns>
 public bool AddWaterMarkText(string originalFile, string markText, int textFontSize, WaterMarkPositions waterMarkPosition)
 {
     FontSize = textFontSize;
     return(AddWaterMarkText(originalFile, markText, waterMarkPosition, FontSize, MyFontFamily, Angle));
 }
Exemplo n.º 9
0
 /// <summary>
 /// 添加文字水印
 /// </summary>
 /// <param name="originalFile">目标文件</param>
 /// <param name="markText">水印文字</param>
 /// <param name="waterMarkPosition">水印位置</param>
 /// <param name="degrees">旋转角度,以文字中心为准</param>
 /// <returns></returns>
 public bool AddWaterMarkText(string originalFile, string markText, WaterMarkPositions waterMarkPosition, int degrees)
 {
     Angle = degrees;
     return(AddWaterMarkText(originalFile, markText, waterMarkPosition, FontSize, MyFontFamily, Angle));
 }
Exemplo n.º 10
0
        /// <summary>
        /// 添加图片水印
        /// </summary>
        /// <param name="originalFile">目标文件</param>
        /// <param name="markImgFile">水印图片文件</param>
        /// <param name="waterMarkPosition">水印位置</param>
        /// <param name="degrees">旋转角度,以底边为准</param>
        /// <returns></returns>
        public bool AddWaterMarkImg(string originalFile, string markImgFile, WaterMarkPositions waterMarkPosition, int offset, int degrees)
        {
            try
            {
                //转化为弧度
                double dblDegrees = degrees * Math.PI / 180;

                Image    image   = new Bitmap(originalFile);
                Image    imgMark = new Bitmap(markImgFile);
                Graphics g       = Graphics.FromImage(image);
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                g.SmoothingMode     = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                int ImgWidth  = image.Width;
                int ImgHeight = image.Height;

                int ImgMarkWidth  = imgMark.Width;
                int ImgMarkHeight = imgMark.Height;

                //计算旋转后水印图片实际所占的宽高,并为了不超过原图片大小计算其自动缩放比例,再求出四角的坐标
                //底边所占宽高
                double dblHeight = 0.0;
                double dblWidth  = 0.0;
                dblHeight += ImgMarkWidth * Math.Sin(dblDegrees);
                dblHeight += ImgMarkHeight * Math.Sin(90 - dblDegrees);
                dblWidth  += ImgMarkHeight * Math.Sin(dblDegrees);
                dblWidth  += ImgMarkWidth * Math.Sin(90 - dblDegrees);
                //缩放,未加入正弦运算
                double dblScaleVal = 1.0; //私有缩放比例
                if (dblWidth > ImgWidth || dblHeight > ImgHeight)
                {
                    dblScaleVal = (dblWidth / ImgWidth > dblHeight / ImgHeight) ? dblHeight / ImgHeight : dblWidth / ImgWidth;
                }
                int newW = Convert.ToInt16(dblScaleVal * ImgMarkWidth);
                int newH = Convert.ToInt16(dblScaleVal * ImgMarkHeight);
                imgMark = new Bitmap(imgMark, newW, newH);

                #region 自己计算,有点问题
                ////四角坐标
                //Point plt = new Point(), prt = new Point(), plb = new Point(); //左上、右上、左下
                //switch (waterMarkPosition)
                //{
                //  case WaterMarkPositions.TOP_LEFT:
                //    plt = new Point(Offset, Convert.ToInt32(Offset + newW * Math.Sin(Degrees)));
                //    prt = new Point(Convert.ToInt32(newW * Math.Sin(90 - Degrees) + Offset), Convert.ToInt32(Offset));
                //    plb = new Point(Convert.ToInt32(newH * Math.Sin(Degrees) + Offset),
                //      Convert.ToInt32(Offset + newW * Math.Sin(Degrees) + newH * Math.Sin(90 - Degrees)));
                //    break;
                //  case WaterMarkPositions.TOP_RIGHT:
                //    plt = new Point(Convert.ToInt32(ImgWidth - newW * Math.Sin(90 - Degrees) - Offset), Offset);
                //    prt = new Point(Convert.ToInt32(ImgWidth - Offset), Convert.ToInt32(Offset + newW * Math.Sin(Degrees)));
                //    plb = new Point(Convert.ToInt32(ImgWidth - Offset - newH * Math.Sin(Degrees) - newW * Math.Sin(90 - Degrees)),
                //      Convert.ToInt32(Offset + newH * Math.Sin(90 - Degrees)));
                //    break;
                //  case WaterMarkPositions.BOTTOM_LEFT:
                //    plt = new Point(Convert.ToInt32(Offset + newH * Math.Sin(Degrees)),
                //      Convert.ToInt32(ImgHeight - Offset - newH * Math.Sin(90 - Degrees) - newW * Math.Sin(Degrees)));
                //    prt = new Point(Convert.ToInt32(Offset + newW * Math.Sin(90 - Degrees) + newH * Math.Sin(Degrees)),
                //      Convert.ToInt32(ImgHeight - Offset - newH * Math.Sin(90 - Degrees)));
                //    plb = new Point(Offset, Convert.ToInt32(ImgHeight - Offset - newW * Math.Sin(90 - Degrees)));
                //    break;
                //  case WaterMarkPositions.BOTTOM_RIGHT:
                //    plt = new Point(Convert.ToInt32(ImgWidth - Offset - newH * Math.Sin(Degrees) - newW * Math.Sin(90 - Degrees)),
                //      Convert.ToInt32(ImgHeight - Offset - newH * Math.Sin(90 - Degrees)));
                //    prt = new Point(Convert.ToInt32(ImgWidth - Offset - newH * Math.Sin(Degrees)),
                //      Convert.ToInt32(ImgHeight - Offset - newW * Math.Sin(Degrees) - newH * Math.Sin(90 - Degrees)));
                //    plb = new Point(Convert.ToInt32(ImgWidth - Offset - newW * Math.Sin(90 - Degrees)),
                //      Convert.ToInt32(ImgHeight - Offset));
                //    break;
                //  case WaterMarkPositions.CENTER:
                //    break;
                //}
                //Point[] destPara = { plt, prt, plb };
                //draw.DrawImage(imgMark, destPara);
                #endregion

                #region 几何变换旋转
                //对角线计算
                double dblDiagonal = Math.Sqrt(Math.Pow(newW, 2) + Math.Pow(newH, 2));
                //旋转中心点,以水印图片为准,并以中心为准计算水印左上角定点起始位置;
                PointF plt = new PointF(), pCenter = new PointF();
                switch (waterMarkPosition)
                {
                case WaterMarkPositions.TOP_LEFT:
                    //pCenter = new PointF(Offset + (newH * (float)Math.Sin(dblDegrees) + newW * (float)Math.Cos(dblDegrees) / 2),
                    //  Offset + (newH * (float)Math.Cos(dblDegrees) + newW * (float)Math.Sin(dblDegrees) / 2));
                    //plt = new PointF(pCenter.X - (float)dblDiagonal / 2 * (float)Math.Sin(Math.PI / 2 - 2 * dblDegrees),
                    //  pCenter.Y - (float)dblDiagonal / 2 * (float)Math.Cos(Math.PI / 2 - 2 * dblDegrees));
                    break;

                case WaterMarkPositions.TOP_RIGHT:
                    break;

                case WaterMarkPositions.BOTTOM_LEFT:
                    break;

                case WaterMarkPositions.BOTTOM_RIGHT:
                    plt = new PointF(ImgWidth - offset - newW, ImgHeight - offset - newH);
                    break;

                case WaterMarkPositions.CENTER:
                    plt = new PointF(ImgWidth / 2 - newW / 2 - offset, ImgHeight / 2 - newH / 2 - offset);
                    break;
                }
                //plt = new PointF(10, 10); pCenter = new PointF(162, 82);
                System.Drawing.Drawing2D.Matrix myMatrix = new System.Drawing.Drawing2D.Matrix();
                myMatrix.RotateAt(degrees, pCenter, System.Drawing.Drawing2D.MatrixOrder.Prepend);
                //draw.Transform = myMatrix;
                g.DrawImage(imgMark, plt);
                #endregion

                Image imageCopy = new Bitmap(image);
                image.Dispose();
                imgMark.Dispose();
                g.Dispose();
                //删除原始文件
                if (File.Exists(originalFile))
                {
                    File.Delete(originalFile);
                }
                //保存新文件
                imageCopy.Save(originalFile, ImageFormat.Jpeg);
                imageCopy.Dispose();

                if (receiveMessage != null)
                {
                    receiveMessage(string.Format("添加图片水印成功:“{0}”", originalFile));
                }

                return(true);
            }
            catch (Exception ex)
            {
                if (receiveMessage != null)
                {
                    receiveMessage(string.Format("添加图片水印失败:“{0}”,错误:{1}", originalFile, ex));
                }
                return(false);
            }
        }
Exemplo n.º 11
0
        private Rectangle Rectangle(int imageWidth, int imageHeight, int waterMarkWidth, int waterMarkHeight, WaterMarkPositions position)
        {
            imageWidth      = imageWidth > 0 ? imageWidth : 100;
            imageHeight     = imageHeight > 0 ? imageHeight : 100;
            waterMarkWidth  = waterMarkWidth > 0 ? waterMarkWidth : 100;
            waterMarkHeight = waterMarkHeight > 0 ? waterMarkHeight : 100;
            int left   = 0;
            int top    = 0;
            int width  = imageWidth / 100 * (int)this._settings.Scale;
            int height = waterMarkHeight * width / waterMarkWidth;

            switch (position)
            {
            case WaterMarkPositions.TopLeft:
                left = 10;
                top  = 10;
                break;

            case WaterMarkPositions.TopRight:
                left = imageWidth - width - 10;
                top  = 10;
                break;

            case WaterMarkPositions.TopMiddle:
                left = (imageWidth - width - 10) / 2;
                top  = 10;
                break;

            case WaterMarkPositions.BottomLeft:
                left = 10;
                top  = imageHeight - height - 10;
                break;

            case WaterMarkPositions.BottomRight:
                left = imageWidth - width - 10;
                top  = imageHeight - height - 10;
                break;

            case WaterMarkPositions.BottomMiddle:
                left = (imageWidth - width - 10) / 2;
                top  = imageHeight - height - 10;
                break;

            case WaterMarkPositions.MiddleLeft:
                left = 10;
                top  = (imageHeight - height - 10) / 2;
                break;

            case WaterMarkPositions.MiddleRight:
                left = imageWidth - width - 10;
                top  = (imageHeight - height - 10) / 2;
                break;

            case WaterMarkPositions.Center:
                left = (imageWidth - width - 10) / 2;
                top  = (imageHeight - height - 10) / 2;
                break;
            }
            return(new Rectangle(left > 0 ? left : 10, top > 0 ? top : 10, width > 0 ? width : 100, height > 0 ? height : 100));
        }