示例#1
0
        /// <summary>
        /// 在一张图片的指定位置处加入水印文字
        /// </summary>
        /// <param name="SourceImage">指定源图片的绝对路径</param>
        /// <param name="Text">指定文本</param>
        /// <param name="fontFamily">文本字体</param>
        /// <param name="textPos">指定位置</param>
        /// <param name="SaveImage">保存图片的绝对路径</param>
        public static void GetWaterMarkTextImage(string SourceImage, string Text, string fontFamily, wmPosition textPos, string SaveImage)
        {
            // 创建一个对象用于操作需要加水印的源图片
            System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(SourceImage);
            // 获取该源图片的宽度和高度
            int phWidth  = imgPhoto.Width;
            int phHeight = imgPhoto.Height;

            // 创建一个BMP格式的空白图片(宽度和高度与源图片一致)
            Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);

            // 设置该新建空白BMP图片的分辨率
            bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

            // 将该BMP图片设置成一个图形对象
            Graphics grPhoto = Graphics.FromImage(bmPhoto);


            // 设置生成图片的质量
            grPhoto.SmoothingMode = SmoothingMode.AntiAlias;

            // 将源图片加载至新建的BMP图片中
            grPhoto.DrawImage(
                imgPhoto,                               // Photo Image object
                new Rectangle(0, 0, phWidth, phHeight), // Rectangle structure
                0,                                      // x-coordinate of the portion of the source image to draw.
                0,                                      // y-coordinate of the portion of the source image to draw.
                phWidth,                                // Width of the portion of the source image to draw.
                phHeight,                               // Height of the portion of the source image to draw.
                GraphicsUnit.Pixel);                    // Units of measure



            //------------------------------------------------------------
            // 第一步:设置插入文本信息的相关属性
            //------------------------------------------------------------

            //-------------------------------------------------------
            //to maximize the size of the Copyright message we will
            //test multiple Font sizes to determine the largest posible
            //font we can use for the width of the Photograph
            //define an array of point sizes you would like to consider as possiblities
            //-------------------------------------------------------
            int[] sizes = new int[] { 16, 14, 12, 10, 8, 6, 4 };

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

            //Loop through the defined sizes checking the length of the Copyright string
            //If its length in pixles is less then the image width choose this Font size.
            for (int i = 0; i < 7; i++)
            {
                //set a Font object to Arial (i)pt, Bold
                crFont = new Font(fontFamily, sizes[i], FontStyle.Bold);
                //Measure the Copyright string in this Font
                crSize = grPhoto.MeasureString(Text, crFont);

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

            //Since all photographs will have varying heights, determine a
            //position 5% from the bottom of the image
            int yPixlesFromBottom = (int)(phHeight * .05);

            //Now that we have a point size use the Copyrights string height
            //to determine a y-coordinate to draw the string of the photograph
            float yPosFromBottom = ((phHeight - yPixlesFromBottom) - (crSize.Height / 2));

            //Determine its x-coordinate by calculating the center of the width of the image
            float xCenterOfImg = (phWidth / 2);

            //Define the text layout by setting the text alignment to centered
            StringFormat StrFormat = new StringFormat();

            StrFormat.Alignment = StringAlignment.Center;

            //------------------------------------------------------------
            // 第二步:第一次绘制文本信息
            //------------------------------------------------------------

            // 设置字体的颜色和透明度 (透明度设置为153)
            SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(153, 0, 0, 0));

            // 绘制文本在图片中的指定位置
            grPhoto.DrawString(
                Text,                                                   // 文本信息
                crFont,                                                 // 文本字体
                semiTransBrush2,                                        // 笔刷
                new PointF(xCenterOfImg + 1, yPosFromBottom + 1),       // 文本在图片中的位置
                StrFormat);                                             // 格式化文本



            //------------------------------------------------------------
            // 第三步:重新绘制一遍文本,使文本具有阴影效果
            //------------------------------------------------------------

            // 设置字体的颜色和透明度 (透明度设置为153)
            SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));

            // 绘制文本在图片中的指定位置
            grPhoto.DrawString(
                Text,                                           // 文本信息
                crFont,                                         // 文本字体
                semiTransBrush,                                 // 笔刷
                new PointF(xCenterOfImg, yPosFromBottom),       // 文本在图片中的位置
                StrFormat);                                     // 格式化文本


            grPhoto.Dispose();
            imgPhoto.Dispose();
            imgPhoto = bmPhoto;

            //------------------------------------------------------------
            // 第四步:保存图片
            //------------------------------------------------------------
            imgPhoto.Save(SaveImage, ImageFormat.Jpeg);


            // 释放使用中的资源
            bmPhoto.Dispose();
            imgPhoto.Dispose();
        }
示例#2
0
        /// <summary>
        /// 在一张图片的指定位置处加入一张具有水印效果的图片和一段文本
        /// </summary>
        /// <param name="SourceImage"></param>
        /// <param name="WaterMarkImage"></param>
        /// <param name="Text"></param>
        /// <param name="fontFamily"></param>
        /// <param name="wmPos"></param>
        /// <param name="textPos"></param>
        /// <param name="SaveImage"></param>
        public static void GetWarterMarkPicTextImage(string SourceImage, string WaterMarkImage, string Text, string fontFamily, wmPosition wmPos, wmPosition textPos, string SaveImage)
        {
            // 创建一个对象用于操作需要加水印的源图片
            System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(SourceImage);
            // 获取该源图片的宽度和高度
            int phWidth  = imgPhoto.Width;
            int phHeight = imgPhoto.Height;

            // 创建一个BMP格式的空白图片(宽度和高度与源图片一致)
            Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);

            // 设置该新建空白BMP图片的分辨率
            bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

            // 将该BMP图片设置成一个图形对象
            Graphics grPhoto = Graphics.FromImage(bmPhoto);


            // 设置生成图片的质量
            grPhoto.SmoothingMode = SmoothingMode.AntiAlias;

            // 将源图片加载至新建的BMP图片中
            grPhoto.DrawImage(
                imgPhoto,                               // Photo Image object
                new Rectangle(0, 0, phWidth, phHeight), // Rectangle structure
                0,                                      // x-coordinate of the portion of the source image to draw.
                0,                                      // y-coordinate of the portion of the source image to draw.
                phWidth,                                // Width of the portion of the source image to draw.
                phHeight,                               // Height of the portion of the source image to draw.
                GraphicsUnit.Pixel);                    // Units of measure



            //------------------------------------------------------------
            // 第一步:设置插入文本信息的相关属性
            //------------------------------------------------------------

            //-------------------------------------------------------
            //to maximize the size of the Copyright message we will
            //test multiple Font sizes to determine the largest posible
            //font we can use for the width of the Photograph
            //define an array of point sizes you would like to consider as possiblities
            //-------------------------------------------------------
            int[] sizes = new int[] { 16, 14, 12, 10, 8, 6, 4 };

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

            //Loop through the defined sizes checking the length of the Copyright string
            //If its length in pixles is less then the image width choose this Font size.
            for (int i = 0; i < 7; i++)
            {
                //set a Font object to Arial (i)pt, Bold
                crFont = new Font(fontFamily, sizes[i], FontStyle.Bold);
                //Measure the Copyright string in this Font
                crSize = grPhoto.MeasureString(Text, crFont);

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

            //Since all photographs will have varying heights, determine a
            //position 5% from the bottom of the image
            int yPixlesFromBottom = (int)(phHeight * .05);

            //Now that we have a point size use the Copyrights string height
            //to determine a y-coordinate to draw the string of the photograph
            float yPosFromBottom = ((phHeight - yPixlesFromBottom) - (crSize.Height / 2));

            //Determine its x-coordinate by calculating the center of the width of the image
            float xCenterOfImg = (phWidth / 2);

            //Define the text layout by setting the text alignment to centered
            StringFormat StrFormat = new StringFormat();

            StrFormat.Alignment = StringAlignment.Center;

            //------------------------------------------------------------
            // 第二步:第一次绘制文本信息
            //------------------------------------------------------------

            // 设置字体的颜色和透明度 (透明度设置为153)
            SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(100, 0, 0, 0));

            // 绘制文本在图片中的指定位置
            grPhoto.DrawString(
                Text,                                                   // 文本信息
                crFont,                                                 // 文本字体
                semiTransBrush2,                                        // 笔刷
                new PointF(xCenterOfImg + 1, yPosFromBottom + 1),       // 文本在图片中的位置
                StrFormat);                                             // 格式化文本



            //------------------------------------------------------------
            // 第三步:重新绘制一遍文本,使文本具有阴影效果
            //------------------------------------------------------------

            // 设置字体的颜色和透明度 (透明度设置为153)
            SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(100, 255, 255, 255));

            // 绘制文本在图片中的指定位置
            grPhoto.DrawString(
                Text,                                           // 文本信息
                crFont,                                         // 文本字体
                semiTransBrush,                                 // 笔刷
                new PointF(xCenterOfImg, yPosFromBottom),       // 文本在图片中的位置
                StrFormat);                                     // 格式化文本


            //------------------------------------------------------------
            // 第四步: 插入水印图片
            //------------------------------------------------------------

            // 创建水印图片的 Image 对象
            System.Drawing.Image imgWatermark = new Bitmap(WaterMarkImage);

            // 获取水印图片的宽度和高度
            int wmWidth  = imgWatermark.Width;
            int wmHeight = imgWatermark.Height;

            //Create a Bitmap based on the previously modified photograph Bitmap
            Bitmap bmWatermark = new Bitmap(bmPhoto);

            bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
            //Load this Bitmap into a new Graphic Object
            Graphics grWatermark = Graphics.FromImage(bmWatermark);

            //To achieve a transulcent watermark we will apply (2) color
            //manipulations by defineing a ImageAttributes object and
            //seting (2) of its properties.
            ImageAttributes imageAttributes = new ImageAttributes();

            //The first step in manipulating the watermark image is to replace
            //the background color with one that is trasparent (Alpha=0, R=0, G=0, B=0)
            //to do this we will use a Colormap and use this to define a RemapTable
            ColorMap colorMap = new ColorMap();

            //My watermark was defined with a background of 100% Green this will
            //be the color we search for and replace with transparency
            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 achive a level of opacity
            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);

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

            //For this example we will place the watermark in the upper right
            //hand corner of the photograph. offset down 10 pixels and to the
            //left 10 pixles
            int xPosOfWm = ((phWidth - wmWidth) - 10);
            int yPosOfWm = 10;


            grWatermark.DrawImage(imgWatermark,
                                  new Rectangle(xPosOfWm, yPosOfWm, wmWidth, wmHeight), //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.
                                  wmWidth,                                              // Watermark Width
                                  wmHeight,                                             // Watermark Height
                                  GraphicsUnit.Pixel,                                   // Unit of measurment
                                  imageAttributes);                                     //ImageAttributes Object

            //Replace the original photgraphs bitmap with the new Bitmap
            imgPhoto.Dispose();
            imgPhoto = bmWatermark;
            grPhoto.Dispose();
            grWatermark.Dispose();
            bmPhoto.Dispose();


            //------------------------------------------------------------
            // 第五步:保存图片
            //------------------------------------------------------------
            imgPhoto.Save(SaveImage, ImageFormat.Jpeg);


            // 释放使用中的资源
            imgPhoto.Dispose();
            imgWatermark.Dispose();
            bmWatermark.Dispose();
        }
示例#3
0
        /// <summary>
        /// 在一张图片的指定位置处加入一张具有水印效果的图片
        /// </summary>
        /// <param name="SourceImage">指定源图片的绝对路径</param>
        /// <param name="WaterMarkImage">指定水印图片的绝对路径</param>
        /// <param name="wmPos">指定位置</param>
        /// <param name="SaveImage">保存图片的绝对路径</param>
        public static void GetWaterMarkPicImage(string SourceImage, string WaterMarkImage, wmPosition wmPos, string SaveImage)
        {
            // 创建一个对象用于操作需要加水印的源图片
            System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(SourceImage);
            // 获取该源图片的宽度和高度
            int phWidth  = imgPhoto.Width;
            int phHeight = imgPhoto.Height;

            // 创建一个BMP格式的空白图片(宽度和高度与源图片一致)
            Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);

            // 设置该新建空白BMP图片的分辨率
            bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

            // 将该BMP图片设置成一个图形对象
            Graphics grPhoto = Graphics.FromImage(bmPhoto);


            // 设置生成图片的质量
            grPhoto.SmoothingMode = SmoothingMode.AntiAlias;

            // 将源图片加载至新建的BMP图片中
            grPhoto.DrawImage(
                imgPhoto,                               // Photo Image object
                new Rectangle(0, 0, phWidth, phHeight), // Rectangle structure
                0,                                      // x-coordinate of the portion of the source image to draw.
                0,                                      // y-coordinate of the portion of the source image to draw.
                phWidth,                                // Width of the portion of the source image to draw.
                phHeight,                               // Height of the portion of the source image to draw.
                GraphicsUnit.Pixel);                    // Units of measure


            // 创建水印图片的 Image 对象
            System.Drawing.Image imgWatermark = new Bitmap(WaterMarkImage);

            // 获取水印图片的宽度和高度
            int wmWidth  = imgWatermark.Width;
            int wmHeight = imgWatermark.Height;

            //------------------------------------------------------------
            // 第一步: 插入水印图片
            //------------------------------------------------------------

            //Create a Bitmap based on the previously modified photograph Bitmap
            Bitmap bmWatermark = new Bitmap(bmPhoto);

            bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
            //Load this Bitmap into a new Graphic Object
            Graphics grWatermark = Graphics.FromImage(bmWatermark);

            //To achieve a transulcent watermark we will apply (2) color
            //manipulations by defineing a ImageAttributes object and
            //seting (2) of its properties.
            ImageAttributes imageAttributes = new ImageAttributes();

            //The first step in manipulating the watermark image is to replace
            //the background color with one that is trasparent (Alpha=0, R=0, G=0, B=0)
            //to do this we will use a Colormap and use this to define a RemapTable
            ColorMap colorMap = new ColorMap();

            //My watermark was defined with a background of 100% Green this will
            //be the color we search for and replace with transparency
            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 achive a level of opacity
            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);

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

            //For this example we will place the watermark in the upper right
            //hand corner of the photograph. offset down 10 pixels and to the
            //left 10 pixles
            int xPosOfWm = ((phWidth - wmWidth) - 10);
            int yPosOfWm = 10;


            grWatermark.DrawImage(imgWatermark,
                                  new Rectangle(xPosOfWm, yPosOfWm, wmWidth, wmHeight), //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.
                                  wmWidth,                                              // Watermark Width
                                  wmHeight,                                             // Watermark Height
                                  GraphicsUnit.Pixel,                                   // Unit of measurment
                                  imageAttributes);                                     //ImageAttributes Object

            //Replace the original photgraphs bitmap with the new Bitmap
            imgPhoto.Dispose();
            imgPhoto = bmWatermark;
            grPhoto.Dispose();
            grWatermark.Dispose();
            bmPhoto.Dispose();

            //------------------------------------------------------------
            // 第三步:保存图片
            //------------------------------------------------------------
            imgPhoto.Save(SaveImage, ImageFormat.Jpeg);

            // 释放使用中的资源
            imgPhoto.Dispose();
            imgWatermark.Dispose();
            bmWatermark.Dispose();
        }
示例#4
0
        /// <summary>
        /// 在一张图片的指定位置处加入水印文字
        /// </summary>
        /// <param name="SourceImage">指定源图片的绝对路径</param>
        /// <param name="Text">指定文本</param>
        /// <param name="fontFamily">文本字体</param>
        /// <param name="textPos">指定位置</param>
        /// <param name="SaveImage">保存图片的绝对路径</param>
        public static void GetWaterMarkTextImage(string SourceImage, string Text, string fontFamily, wmPosition textPos, string SaveImage)
        {
            // 创建一个对象用于操作需要加水印的源图片
            System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(SourceImage);
            // 获取该源图片的宽度和高度
            int phWidth = imgPhoto.Width;
            int phHeight = imgPhoto.Height;

            // 创建一个BMP格式的空白图片(宽度和高度与源图片一致)
            Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);

            // 设置该新建空白BMP图片的分辨率
            bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

            // 将该BMP图片设置成一个图形对象
            Graphics grPhoto = Graphics.FromImage(bmPhoto);


            // 设置生成图片的质量
            grPhoto.SmoothingMode = SmoothingMode.AntiAlias;

            // 将源图片加载至新建的BMP图片中
            grPhoto.DrawImage(
                imgPhoto,                               // Photo Image object
                new Rectangle(0, 0, phWidth, phHeight), // Rectangle structure
                0,                                      // x-coordinate of the portion of the source image to draw. 
                0,                                      // y-coordinate of the portion of the source image to draw. 
                phWidth,                                // Width of the portion of the source image to draw. 
                phHeight,                               // Height of the portion of the source image to draw. 
                GraphicsUnit.Pixel);                    // Units of measure 



            //------------------------------------------------------------
            // 第一步:设置插入文本信息的相关属性
            //------------------------------------------------------------

            //-------------------------------------------------------
            //to maximize the size of the Copyright message we will 
            //test multiple Font sizes to determine the largest posible 
            //font we can use for the width of the Photograph
            //define an array of point sizes you would like to consider as possiblities
            //-------------------------------------------------------
            int[] sizes = new int[] { 16, 14, 12, 10, 8, 6, 4 };

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

            //Loop through the defined sizes checking the length of the Copyright string
            //If its length in pixles is less then the image width choose this Font size.
            for (int i = 0; i < 7; i++)
            {
                //set a Font object to Arial (i)pt, Bold
                crFont = new Font(fontFamily, sizes[i], FontStyle.Bold);
                //Measure the Copyright string in this Font
                crSize = grPhoto.MeasureString(Text, crFont);

                if ((ushort)crSize.Width < (ushort)phWidth) break;
            }

            //Since all photographs will have varying heights, determine a 
            //position 5% from the bottom of the image
            int yPixlesFromBottom = (int)(phHeight * .05);

            //Now that we have a point size use the Copyrights string height 
            //to determine a y-coordinate to draw the string of the photograph
            float yPosFromBottom = ((phHeight - yPixlesFromBottom) - (crSize.Height / 2));

            //Determine its x-coordinate by calculating the center of the width of the image
            float xCenterOfImg = (phWidth / 2);

            //Define the text layout by setting the text alignment to centered
            StringFormat StrFormat = new StringFormat();
            StrFormat.Alignment = StringAlignment.Center;

            //------------------------------------------------------------
            // 第二步:第一次绘制文本信息
            //------------------------------------------------------------

            // 设置字体的颜色和透明度 (透明度设置为153)
            SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(153, 0, 0, 0));

            // 绘制文本在图片中的指定位置
            grPhoto.DrawString(
                Text,												// 文本信息
                crFont,												// 文本字体
                semiTransBrush2,									// 笔刷
                new PointF(xCenterOfImg + 1, yPosFromBottom + 1),	// 文本在图片中的位置
                StrFormat);											// 格式化文本




            //------------------------------------------------------------
            // 第三步:重新绘制一遍文本,使文本具有阴影效果
            //------------------------------------------------------------

            // 设置字体的颜色和透明度 (透明度设置为153)
            SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));

            // 绘制文本在图片中的指定位置
            grPhoto.DrawString(
                Text,										// 文本信息
                crFont,										// 文本字体
                semiTransBrush,								// 笔刷
                new PointF(xCenterOfImg, yPosFromBottom),	// 文本在图片中的位置
                StrFormat);									// 格式化文本


            grPhoto.Dispose();
            imgPhoto.Dispose();
            imgPhoto = bmPhoto;

            //------------------------------------------------------------
            // 第四步:保存图片
            //------------------------------------------------------------
            imgPhoto.Save(SaveImage, ImageFormat.Jpeg);


            // 释放使用中的资源
            bmPhoto.Dispose();
            imgPhoto.Dispose();
        }
示例#5
0
        /// <summary>
        /// 在一张图片的指定位置处加入一张具有水印效果的图片和一段文本
        /// </summary>
        /// <param name="SourceImage"></param>
        /// <param name="WaterMarkImage"></param>
        /// <param name="Text"></param>
        /// <param name="fontFamily"></param>
        /// <param name="wmPos"></param>
        /// <param name="textPos"></param>
        /// <param name="SaveImage"></param>
        public static void GetWarterMarkPicTextImage(string SourceImage, string WaterMarkImage, string Text, string fontFamily, wmPosition wmPos, wmPosition textPos, string SaveImage)
        {
            // 创建一个对象用于操作需要加水印的源图片
            System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(SourceImage);
            // 获取该源图片的宽度和高度
            int phWidth = imgPhoto.Width;
            int phHeight = imgPhoto.Height;

            // 创建一个BMP格式的空白图片(宽度和高度与源图片一致)
            Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);

            // 设置该新建空白BMP图片的分辨率
            bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

            // 将该BMP图片设置成一个图形对象
            Graphics grPhoto = Graphics.FromImage(bmPhoto);


            // 设置生成图片的质量
            grPhoto.SmoothingMode = SmoothingMode.AntiAlias;

            // 将源图片加载至新建的BMP图片中
            grPhoto.DrawImage(
                imgPhoto,                               // Photo Image object
                new Rectangle(0, 0, phWidth, phHeight), // Rectangle structure
                0,                                      // x-coordinate of the portion of the source image to draw. 
                0,                                      // y-coordinate of the portion of the source image to draw. 
                phWidth,                                // Width of the portion of the source image to draw. 
                phHeight,                               // Height of the portion of the source image to draw. 
                GraphicsUnit.Pixel);                    // Units of measure 



            //------------------------------------------------------------
            // 第一步:设置插入文本信息的相关属性
            //------------------------------------------------------------

            //-------------------------------------------------------
            //to maximize the size of the Copyright message we will 
            //test multiple Font sizes to determine the largest posible 
            //font we can use for the width of the Photograph
            //define an array of point sizes you would like to consider as possiblities
            //-------------------------------------------------------
            int[] sizes = new int[] { 16, 14, 12, 10, 8, 6, 4 };

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

            //Loop through the defined sizes checking the length of the Copyright string
            //If its length in pixles is less then the image width choose this Font size.
            for (int i = 0; i < 7; i++)
            {
                //set a Font object to Arial (i)pt, Bold
                crFont = new Font(fontFamily, sizes[i], FontStyle.Bold);
                //Measure the Copyright string in this Font
                crSize = grPhoto.MeasureString(Text, crFont);

                if ((ushort)crSize.Width < (ushort)phWidth) break;
            }

            //Since all photographs will have varying heights, determine a 
            //position 5% from the bottom of the image
            int yPixlesFromBottom = (int)(phHeight * .05);

            //Now that we have a point size use the Copyrights string height 
            //to determine a y-coordinate to draw the string of the photograph
            float yPosFromBottom = ((phHeight - yPixlesFromBottom) - (crSize.Height / 2));

            //Determine its x-coordinate by calculating the center of the width of the image
            float xCenterOfImg = (phWidth / 2);

            //Define the text layout by setting the text alignment to centered
            StringFormat StrFormat = new StringFormat();
            StrFormat.Alignment = StringAlignment.Center;

            //------------------------------------------------------------
            // 第二步:第一次绘制文本信息
            //------------------------------------------------------------

            // 设置字体的颜色和透明度 (透明度设置为153)
            SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(100, 0, 0, 0));

            // 绘制文本在图片中的指定位置
            grPhoto.DrawString(
                Text,												// 文本信息
                crFont,												// 文本字体
                semiTransBrush2,									// 笔刷
                new PointF(xCenterOfImg + 1, yPosFromBottom + 1),	// 文本在图片中的位置
                StrFormat);											// 格式化文本




            //------------------------------------------------------------
            // 第三步:重新绘制一遍文本,使文本具有阴影效果
            //------------------------------------------------------------

            // 设置字体的颜色和透明度 (透明度设置为153)
            SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(100, 255, 255, 255));

            // 绘制文本在图片中的指定位置
            grPhoto.DrawString(
                Text,										// 文本信息
                crFont,										// 文本字体
                semiTransBrush,								// 笔刷
                new PointF(xCenterOfImg, yPosFromBottom),	// 文本在图片中的位置
                StrFormat);									// 格式化文本


            //------------------------------------------------------------
            // 第四步: 插入水印图片
            //------------------------------------------------------------

            // 创建水印图片的 Image 对象
            System.Drawing.Image imgWatermark = new Bitmap(WaterMarkImage);

            // 获取水印图片的宽度和高度
            int wmWidth = imgWatermark.Width;
            int wmHeight = imgWatermark.Height;

            //Create a Bitmap based on the previously modified photograph Bitmap
            Bitmap bmWatermark = new Bitmap(bmPhoto);
            bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
            //Load this Bitmap into a new Graphic Object
            Graphics grWatermark = Graphics.FromImage(bmWatermark);

            //To achieve a transulcent watermark we will apply (2) color 
            //manipulations by defineing a ImageAttributes object and 
            //seting (2) of its properties.
            ImageAttributes imageAttributes = new ImageAttributes();

            //The first step in manipulating the watermark image is to replace 
            //the background color with one that is trasparent (Alpha=0, R=0, G=0, B=0)
            //to do this we will use a Colormap and use this to define a RemapTable
            ColorMap colorMap = new ColorMap();

            //My watermark was defined with a background of 100% Green this will
            //be the color we search for and replace with transparency
            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 achive a level of opacity
            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);

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

            //For this example we will place the watermark in the upper right
            //hand corner of the photograph. offset down 10 pixels and to the 
            //left 10 pixles
            int xPosOfWm = ((phWidth - wmWidth) - 10);
            int yPosOfWm = 10;


            grWatermark.DrawImage(imgWatermark,
                new Rectangle(xPosOfWm, yPosOfWm, wmWidth, wmHeight),  //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. 
                wmWidth,            // Watermark Width
                wmHeight,		    // Watermark Height
                GraphicsUnit.Pixel, // Unit of measurment
                imageAttributes);   //ImageAttributes Object

            //Replace the original photgraphs bitmap with the new Bitmap
            imgPhoto.Dispose();
            imgPhoto = bmWatermark;
            grPhoto.Dispose();
            grWatermark.Dispose();
            bmPhoto.Dispose();


            //------------------------------------------------------------
            // 第五步:保存图片
            //------------------------------------------------------------
            imgPhoto.Save(SaveImage, ImageFormat.Jpeg);


            // 释放使用中的资源
            imgPhoto.Dispose();
            imgWatermark.Dispose();
            bmWatermark.Dispose();
        }
示例#6
0
        /// <summary>
        /// 在一张图片的指定位置处加入一张具有水印效果的图片
        /// </summary>
        /// <param name="SourceImage">指定源图片的绝对路径</param>
        /// <param name="WaterMarkImage">指定水印图片的绝对路径</param>
        /// <param name="wmPos">指定位置</param>
        /// <param name="SaveImage">保存图片的绝对路径</param>
        public static void GetWaterMarkPicImage(string SourceImage, string WaterMarkImage, wmPosition wmPos, string SaveImage)
        {
            // 创建一个对象用于操作需要加水印的源图片
            System.Drawing.Image imgPhoto = System.Drawing.Image.FromFile(SourceImage);
            // 获取该源图片的宽度和高度
            int phWidth = imgPhoto.Width;
            int phHeight = imgPhoto.Height;

            // 创建一个BMP格式的空白图片(宽度和高度与源图片一致)
            Bitmap bmPhoto = new Bitmap(phWidth, phHeight, PixelFormat.Format24bppRgb);

            // 设置该新建空白BMP图片的分辨率
            bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

            // 将该BMP图片设置成一个图形对象
            Graphics grPhoto = Graphics.FromImage(bmPhoto);


            // 设置生成图片的质量
            grPhoto.SmoothingMode = SmoothingMode.AntiAlias;

            // 将源图片加载至新建的BMP图片中
            grPhoto.DrawImage(
                imgPhoto,                               // Photo Image object
                new Rectangle(0, 0, phWidth, phHeight), // Rectangle structure
                0,                                      // x-coordinate of the portion of the source image to draw. 
                0,                                      // y-coordinate of the portion of the source image to draw. 
                phWidth,                                // Width of the portion of the source image to draw. 
                phHeight,                               // Height of the portion of the source image to draw. 
                GraphicsUnit.Pixel);                    // Units of measure 


            // 创建水印图片的 Image 对象
            System.Drawing.Image imgWatermark = new Bitmap(WaterMarkImage);

            // 获取水印图片的宽度和高度
            int wmWidth = imgWatermark.Width;
            int wmHeight = imgWatermark.Height;

            //------------------------------------------------------------
            // 第一步: 插入水印图片
            //------------------------------------------------------------

            //Create a Bitmap based on the previously modified photograph Bitmap
            Bitmap bmWatermark = new Bitmap(bmPhoto);
            bmWatermark.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);
            //Load this Bitmap into a new Graphic Object
            Graphics grWatermark = Graphics.FromImage(bmWatermark);

            //To achieve a transulcent watermark we will apply (2) color 
            //manipulations by defineing a ImageAttributes object and 
            //seting (2) of its properties.
            ImageAttributes imageAttributes = new ImageAttributes();

            //The first step in manipulating the watermark image is to replace 
            //the background color with one that is trasparent (Alpha=0, R=0, G=0, B=0)
            //to do this we will use a Colormap and use this to define a RemapTable
            ColorMap colorMap = new ColorMap();

            //My watermark was defined with a background of 100% Green this will
            //be the color we search for and replace with transparency
            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 achive a level of opacity
            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);

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

            //For this example we will place the watermark in the upper right
            //hand corner of the photograph. offset down 10 pixels and to the 
            //left 10 pixles
            int xPosOfWm = ((phWidth - wmWidth) - 10);
            int yPosOfWm = 10;


            grWatermark.DrawImage(imgWatermark,
                new Rectangle(xPosOfWm, yPosOfWm, wmWidth, wmHeight),  //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. 
                wmWidth,            // Watermark Width
                wmHeight,		    // Watermark Height
                GraphicsUnit.Pixel, // Unit of measurment
                imageAttributes);   //ImageAttributes Object

            //Replace the original photgraphs bitmap with the new Bitmap
            imgPhoto.Dispose();
            imgPhoto = bmWatermark;
            grPhoto.Dispose();
            grWatermark.Dispose();
            bmPhoto.Dispose();

            //------------------------------------------------------------
            // 第三步:保存图片
            //------------------------------------------------------------
            imgPhoto.Save(SaveImage, ImageFormat.Jpeg);

            // 释放使用中的资源
            imgPhoto.Dispose();
            imgWatermark.Dispose();
            bmWatermark.Dispose();
        }