zoukankan      html  css  js  c++  java
  • 二维码名片

     /// <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);
                        }
                    }
                }
            }
    

      

  • 相关阅读:
    LeetCode:Container With Most Water
    c#编写的基于Socket的异步通信系统
    关于Thread类中三个interrupt方法的研究与学习(转)
    使用svnkit 的相关实例及相关问题汇总
    创业早期,联合创始人如何避免窝里反?(转)
    程序员解决问题的60个策略(转)
    码农和程序员之间的5个关键差异(转)
    LayoutInflater的使用
    2014年中国95家企业入围世界500强,超日本
    How to get the source code of the chromium of the specified revision
  • 原文地址:https://www.cnblogs.com/zjflove/p/5026022.html
Copyright © 2011-2022 走看看