Example #1
0
        /// <summary>
        /// 将原始图片添加文字水印之后存储
        /// </summary>
        /// <param name="srcPath"></param>
        /// <param name="savePath"></param>
        /// <param name="words"></param>
        /// <param name="wp"></param>
        /// <param name="fontSize"></param>
        public static void MakeByText(String srcPath, String savePath, String words, WatermarkPosition wp, int fontSize)
        {
            using (Image src = Image.FromFile(srcPath)) {
                using (Bitmap bm = new Bitmap(srcPath)) {
                    using (Graphics g = Graphics.FromImage(bm)) {
                        FontAndSize  fs = FontAndSize.GetValue(g, words, "arial", fontSize, src.Width);
                        PointF       p  = getTextPoint(src, fs.size, wp);
                        StringFormat sf = getStringFormat();

                        drawText(g, words, fs.font, p, sf);

                        try {
                            bm.Save(savePath);
                        }
                        catch (Exception e) {
                            throw e;
                        }
                    }
                }
            }
        }
Example #2
0
        /// <summary>
        /// 创建验证码,返回一个 Image 对象
        /// </summary>
        /// <param name="code"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="fontFamily"></param>
        /// <returns></returns>
        public Image CreateImage(String code, int width, int height, String fontFamily)
        {
            Bitmap bm = new Bitmap(width, height);

            using (Graphics g = Graphics.FromImage(bm)) {
                g.SmoothingMode = SmoothingMode.AntiAlias;

                HatchBrush brush = new HatchBrush(HatchStyle.SmallConfetti, Color.LightGray, Color.White);
                Rectangle  rect  = new Rectangle(0, 0, width, height);
                g.FillRectangle(brush, rect);

                int         fontsize = rect.Height + 1;
                FontAndSize size     = FontAndSize.GetValue(g, code, fontFamily, fontsize, bm.Width);

                GraphicsPath gpath = getGraphicsPath(code, size.font, rect);

                Color brushColor = ColorTranslator.FromHtml("#000000");
                brush = new HatchBrush(HatchStyle.Divot, brushColor, Color.DarkGray);
                g.FillPath(brush, gpath);

                addRandomNoise(g, rect);
            }
            return(bm);
        }