/// <summary>
        /// 加水印文字
        /// </summary>
        /// <param name="picture">imge 对象</param>
        /// <param name="_width">原始图片的宽度</param>
        /// <param name="_height">原始图片的高度.</param>
        private void addWatermarkText(Graphics picture, int _width, int _height, HTProject_Bizlogic.CommonEnum.WaterMarkPosition Pos)
        {
            int[] sizes  = new int[] { 16, 14, 12, 10, 8, 6, 4 };
            Font  crFont = null;
            SizeF crSize = new SizeF();

            for (int i = 0; i < 7; i++)
            {
                crFont = new Font("arial", sizes[i], FontStyle.Bold);
                crSize = picture.MeasureString(this.WatermarkText, crFont);

                if ((ushort)crSize.Width < (ushort)_width)
                {
                    break;
                }
            }

            float xpos = 0;
            float ypos = 0;


            switch (Pos)
            {
            case HTProject_Bizlogic.CommonEnum.WaterMarkPosition.TOP_LEFT:
                xpos = ((float)_width * (float).01) + (crSize.Width / 2);
                ypos = (float)_height * (float).01;
                break;

            case HTProject_Bizlogic.CommonEnum.WaterMarkPosition.TOP_RIGHT:
                xpos = ((float)_width * (float).99) - (crSize.Width / 2);
                ypos = (float)_height * (float).01;
                break;

            case HTProject_Bizlogic.CommonEnum.WaterMarkPosition.BOTTOM_RIGHT:
                xpos = ((float)_width * (float).99) - (crSize.Width / 2);
                ypos = ((float)_height * (float).99) - crSize.Height;
                break;

            case HTProject_Bizlogic.CommonEnum.WaterMarkPosition.BOTTOM_LEFT:
                xpos = ((float)_width * (float).01) + (crSize.Width / 2);
                ypos = ((float)_height * (float).99) - crSize.Height;
                break;
            }

            StringFormat StrFormat = new StringFormat();

            StrFormat.Alignment = StringAlignment.Center;

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

            picture.DrawString(this.WatermarkText, crFont, semiTransBrush2, xpos + 1, ypos + 1, StrFormat);

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

            picture.DrawString(this.WatermarkText, crFont, semiTransBrush, xpos, ypos, StrFormat);


            semiTransBrush2.Dispose();
            semiTransBrush.Dispose();
        }
Beispiel #2
0
        /// <summary>
        /// 加水印图片
        /// </summary>
        /// <param name="picture">imge 对象</param>
        /// <param name="_width">原始图片的宽度</param>
        /// <param name="_height">原始图片的高度.</param>
        private void addWatermarkImage(Graphics picture, int _width, int _height, HTProject_Bizlogic.CommonEnum.WaterMarkPosition Pos)
        {
            if (!System.IO.File.Exists(this.Page.MapPath(this.WatermarkImageUrl)))
            {
                return;
            }
            System.Drawing.Image watermark = new Bitmap(this.Page.MapPath(this.WatermarkImageUrl));

            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;


            WatermarkWidth  = watermark.Width;  //Convert.ToInt32(_width);
            WatermarkHeight = watermark.Height; // Convert.ToInt32(_height);


            //picture.DrawImage(watermark, new Rectangle(xpos, ypos, WatermarkWidth, WatermarkHeight), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes);
            //循环加上水印
            for (int y = 0; y < _height; y = y + WatermarkHeight * 2)
            {
                for (int x = 0; x < _width; x = x + WatermarkWidth * 2)
                {
                    xpos = x;
                    ypos = y;
                    picture.DrawImage(watermark, new Rectangle(xpos, ypos, WatermarkWidth, WatermarkHeight), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes);
                }
            }

            watermark.Dispose();
            imageAttributes.Dispose();
        }
        /// <summary>
        /// 加水印图片
        /// </summary>
        /// <param name="picture">imge 对象</param>
        /// <param name="_width">原始图片的宽度</param>
        /// <param name="_height">原始图片的高度.</param>
        private void addWatermarkImage(Graphics picture, int _width, int _height, HTProject_Bizlogic.CommonEnum.WaterMarkPosition Pos)
        {
            if (!System.IO.File.Exists(this.Page.MapPath(this.WatermarkImageUrl)))
            {
                return;
            }
            System.Drawing.Image watermark = new Bitmap(this.Page.MapPath(this.WatermarkImageUrl));

            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  = watermark.Width;  //Convert.ToInt32(_width);
            WatermarkHeight = watermark.Height; // Convert.ToInt32(_height);

            //switch (Pos)
            //{
            //    case HTProject_Bizlogic.CommonEnum.WaterMarkPosition.TOP_LEFT:
            //        xpos = 0;
            //        ypos = 0;
            //        break;
            //    case HTProject_Bizlogic.CommonEnum.WaterMarkPosition.TOP_RIGHT:
            //        xpos = _width - WatermarkWidth - 10;
            //        ypos = 10;
            //        break;
            //    case HTProject_Bizlogic.CommonEnum.WaterMarkPosition.BOTTOM_RIGHT:
            //        xpos = _width - WatermarkWidth - 10;
            //        ypos = _height - WatermarkHeight - 10;
            //        break;
            //    case HTProject_Bizlogic.CommonEnum.WaterMarkPosition.BOTTOM_LEFT:
            //        xpos = 0;
            //        ypos = _height - WatermarkHeight - 10;
            //        break;
            //}

            //picture.DrawImage(watermark, new Rectangle(xpos, ypos, WatermarkWidth, WatermarkHeight), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes);
            //循环加上水印
            for (int y = 0; y < _height; y = y + WatermarkHeight)
            {
                for (int x = 0; x < _width; x = x + WatermarkWidth)
                {
                    //switch (Pos)
                    //{
                    //    case HTProject_Bizlogic.CommonEnum.WaterMarkPosition.TOP_LEFT:
                    //        xpos = 0;
                    //        ypos = 0;
                    //        break;
                    //    case HTProject_Bizlogic.CommonEnum.WaterMarkPosition.TOP_RIGHT:
                    //        xpos = _width - WatermarkWidth - 10;
                    //        ypos = 10;
                    //        break;
                    //    case HTProject_Bizlogic.CommonEnum.WaterMarkPosition.BOTTOM_RIGHT:
                    //        xpos = _width - WatermarkWidth - 10;
                    //        ypos = _height - WatermarkHeight - 10;
                    //        break;
                    //    case HTProject_Bizlogic.CommonEnum.WaterMarkPosition.BOTTOM_LEFT:
                    //        xpos = 0;
                    //        ypos = _height - WatermarkHeight - 10;
                    //        break;
                    //}
                    xpos = x;
                    ypos = y;
                    picture.DrawImage(watermark, new Rectangle(xpos, ypos, WatermarkWidth, WatermarkHeight), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel, imageAttributes);
                }
            }

            watermark.Dispose();
            imageAttributes.Dispose();
        }