二维码名片

 /// <summary>
        ///  给图片加水印(图片居中)
        /// </summary>
        /// <param name="srcImgPath">原始图片路径</param>
        /// <param name="waterPath">水印图片路径</param>
        /// <param name="waterWidth">水印区域的宽度</param>
        /// <param name="waterHeight">水印区域的高度</param>
        /// <param name="waterString">水印字符</param>
        /// <param name="markPosition">水印的位置</param>
 
        public static void MakeWaterPic(string srcImgPath, string waterPath, int waterWidth, int waterHeight, string waterString, string markPosition = "图片中间")
        {
            //1. 从原图片创建 Image 对象 
            using (Image img = Image.FromFile(srcImgPath))
            {
                //2.用指定的大小和格式初始化 Bitmap 类的新实例 
                using (Bitmap map = new Bitmap(img.Width, img.Height, PixelFormat.Format32bppArgb))
                {
                    //3.从指定的 Image 对象创建新 Graphics 对象 
                    using (Graphics g = Graphics.FromImage(map))
                    {

                        //4.在指定位置并且按指定大小绘制 原图片 对象 
                        g.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), new RectangleF(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);

                        //g.DrawImage(map2,new Rectangle(0, 0, map2.Width, map2.Height));
                        // 设置水印字体
                        int fHeight = img.Height / 5;
                        if (fHeight > 16) fHeight = 16;
                        Font drawFont = new Font("Arial", fHeight);

                        // 设置水印文字位置 
                        int disX = 0, disY = 0;
                        if (markPosition == "图片中间")
                        {
                            disX = (img.Width - waterWidth) / 2;
                            disY = (img.Height - waterHeight) / 2;
                        }
                        StringFormat drawFormat = new StringFormat();
                        drawFormat.FormatFlags = StringFormatFlags.NoWrap;
                        //文本居中显示
                        drawFormat.Alignment = StringAlignment.Center;
                        drawFormat.LineAlignment = StringAlignment.Center;

                        SolidBrush drawBrush = new SolidBrush(Color.White);
                        //填充矩形
                        g.FillRectangle(drawBrush, new Rectangle(disX, disY, waterWidth, waterHeight));
                        drawBrush.Color = Color.Black;
                        g.DrawString(waterString, drawFont, drawBrush, new RectangleF(disX, disY, waterWidth, waterHeight), drawFormat);


                        map.Save(waterPath, ImageFormat.Jpeg);
                    }
                }
            }
        }

  

posted @ 2015-12-07 14:40  哈哈2222  阅读(341)  评论(1编辑  收藏  举报