发新话题
打印

[转贴] 把图片+文字 生成图片的代码

把图片+文字 生成图片的代码

下面是一个添加水印文字的: 看对你有帮助不

            ///   <summary>
                ///     加水印文字
                ///   </summary>
                ///   <param   name= "picture "> imge   对象 </param>
                ///   <param   name= "_watermarkText "> 水印文字内容 </param>
                ///   <param   name= "_watermarkPosition "> 水印位置 </param>
                ///   <param   name= "_width "> 被加水印图片的宽 </param>
                ///   <param   name= "_height "> 被加水印图片的高 </param>
                private   void   addWatermarkText(Graphics   picture,   string   _watermarkText,   string   _watermarkPosition,   int   _width,   int   _height)
                {
                        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,   FontStyle.Bold);
                                crSize   =   picture.MeasureString(_watermarkText,   crFont);

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

                        float   xpos   =   0;
                        float   ypos   =   0;

                        switch   (_watermarkPosition)
                        {
                                case   "WM_TOP_LEFT ":
                                        xpos   =   ((float)_width   *   (float).01)   +   (crSize.Width   /   2);
                                        ypos   =   (float)_height   *   (float).01;
                                        break;
                                case   "WM_TOP_RIGHT ":
                                        xpos   =   ((float)_width   *   (float).99)   -   (crSize.Width   /   2);
                                        ypos   =   (float)_height   *   (float).01;
                                        break;
                                case   "WM_BOTTOM_RIGHT ":
                                        xpos   =   ((float)_width   *   (float).99)   -   (crSize.Width   /   2);
                                        ypos   =   ((float)_height   *   (float).99)   -   crSize.Height;
                                        break;
                                case   "WM_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(_watermarkText,   crFont,   semiTransBrush2,   xpos   +   1,   ypos   +   1,   StrFormat);

                        SolidBrush   semiTransBrush   =   new   SolidBrush(Color.FromArgb(153,   255,   255,   255));
                        picture.DrawString(_watermarkText,   crFont,   semiTransBrush,   xpos,   ypos,   StrFormat);


                        semiTransBrush2.Dispose();
                        semiTransBrush.Dispose();


                }
AspJZY.com
ASP集中营奋斗永不放弃!

TOP

<%@   Page   language= "C# "   %>
<%@   Import   Namespace= "System.Web "   %>
<%@   Import   Namespace= "System.Drawing "   %>
<%@   Import   Namespace= "System.Drawing.Imaging "   %>


<script   runat= "server "   language= "C# ">
  
        public   static   bool   ChkCH(char   c)
        {
        string   Digit   =   "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIGKLMNOPQRSTUVWXYZ-_ ";
                     
                                if   (Digit.IndexOf(c,   0)   !=   -1)
                                {
                                        return   true;
                                }
                                return   false;
        }
        void   Page_Load(Object   sender,   EventArgs   e)
    {
        HttpContext   context   =   HttpContext.Current;
        int   charsNo   =   5;
        int   fontSize   =   30;
        int   bgWidth   =   400;   
        int   bgHeight   =300;
  
        //   Get   params   from   QueryString
        //
        fontSize   =   Spaces.SpacesContext.GetIntFromQueryString(context,   "FontSize ");
        bgWidth   =   Spaces.SpacesContext.GetIntFromQueryString(context,   "ImgWidth ");
        bgHeight   =   Spaces.SpacesContext.GetIntFromQueryString(context,   "ImgHeight ");         
        
        //   Load   defaults   if   params   are   empty
        //
        if(fontSize   ==   -1)   fontSize   =   32;
        if   (bgWidth   ==   -1)   bgWidth   =   400;
        if   (bgHeight   ==   -1)   bgHeight   =   300;
        float   x   =   110;//(bgWidth   -   (charsNo   *   (fontSize   +   0.5F)))   /   2;   //   TODO:   optimize
        float   y   =   100;//(bgHeight   -   (fontSize   *   1.7F))   /   2;   //   TODO:   optimize
        
        //   Generate   the   text
        //
        string   genText   =   GetUserName(this.Request);
        
        //   Add   the   generate   text   to   a   session   variable   
        //
        //Session.Add( "Verify ",   genText);                       
        
        //   Create   the   memory   map   
        //
        Bitmap   raster;
        System.Drawing.Imaging.PixelFormat   pixFormat   =   System.Drawing.Imaging.PixelFormat.Format32bppArgb;
        
        //   Select   an   memory   image   from   file   of   290x80   px
        //   in   the   current   dir,   NoSpamBgImgs   folder   named   bg_X.jpg
        //
        Graphics   graphicObj;
      //   string   bgFilePath   =   context.Server.MapPath(@ ".\AntiSpamBgImgs\bg_ "   +   new   Random().Next(5)   +   ".jpg ");   
        string   bgFilePath   =   context.Server.MapPath(@ ".\zq\zq.jpg ");               
        System.Drawing.Image   imgObj   =   System.Drawing.Image.FromFile(bgFilePath);
        
        //   Creating   the   raster   image   &   graphic   objects
        //
        raster   =   new   Bitmap(imgObj,   bgWidth,   bgHeight);
        graphicObj   =   Graphics.FromImage(raster);
        
        //   Creating   an   array   for   most   readable   yet   cryptic   fonts   for   OCR 's
        //   This   is   entirely   up   to   developer 's   discretion
        //   CAPTCHA   recomandation
        //
        String[]   crypticFonts   =   new   String[10];         
        crypticFonts   [0]   =   "Arial ";
        crypticFonts   [1]   =   "Verdana ";
        crypticFonts   [2]   =   "Comic   Sans   MS ";
        crypticFonts   [3]   =   "Impact ";
        crypticFonts   [4]   =   "Haettenschweiler ";
        crypticFonts   [5]   =   "Lucida   Sans   Unicode ";
        crypticFonts   [6]   =   "Garamond ";
        crypticFonts   [7]   =   "Courier   New ";
        crypticFonts   [8]   =   "Book   Antiqua ";
        crypticFonts   [9]   =   "Arial   Narrow ";

        //   Instantiate   object   of   brush   with   black   color
        //
        SolidBrush[]   brushes   =   new   SolidBrush[1];
        brushes   [0]   =   new   SolidBrush(Color.Black);

        //   Loop   to   write   the   characters   on   image   with   different   fonts
        //   CAPTCHA   method
        //

        FontFamily   ff   =   new   FontFamily( "Comic   Sans   MS ");//   new   FontFamily( "汉鼎繁颜体 ");
        Font   fontObj   =   new   Font(ff,   fontSize,FontStyle.Regular,   GraphicsUnit.Pixel);
        for(int   a   =   0;   a   <   genText.Length;   a++)   {

              //new   Font(crypticFonts[new   Random().Next(a)],   fontSize);

                //fontObj.Style   =   FontStyle.Bold;
                if   (ChkCH(genText[a]))
                        fontObj   =   new   Font( "Arial ",fontSize);
                else
                        fontObj   =   new   Font(ff,   fontSize,   FontStyle.Regular,   GraphicsUnit.Pixel);
                        graphicObj.DrawString(genText.Substring(a,   1),   fontObj,   brushes[0],   x   +   (a   *   fontSize),   y);
        }

        //   Flush   again
        //
        graphicObj.Flush();

        //   Setting   the   response   header   content   MIME   type
        //
        context.Response.ContentType   =   "image/jpeg ";
        
        //   Saving   the   file   to   output   stream   to   be   displayed   in   browser
        //
        raster.Save(context.Response.OutputStream,   System.Drawing.Imaging.ImageFormat.Jpeg);
        
        //   Flushing   to   the   Response   
        //
        context.Response.Flush();         
    }
        public   static   string   GetUserName(HttpRequest   req)
        {
                string   name=req.QueryString[ "n "];
                if   (name   ==   null)
                        return     "潇笑: ";
                if   (name.Trim()   ==   string.Empty)   return   string.Empty;
                return   name+ ": ";
        }
                public   static   String   CreateTemporaryPassword(int   length)   {

                        string   strTempPassword   =   Guid.NewGuid().ToString( "N ");
        strTempPassword   =   strTempPassword.Replace( "0 ",   " ");
        strTempPassword   =   strTempPassword.Replace( "O ",   " ");

                        for(int   i   =   0;   i   <   (length   /   32);   i++)   {
                                strTempPassword   +=   Guid.NewGuid().ToString( "N ");
                        }

                        return   strTempPassword.Substring(0,   length);
                }

</script>
代码较乱,不好意思。
copy下来,设置背景图后就可以用了,字体换成你机器上存在的字体,有些垃圾代码,自行清理
AspJZY.com
ASP集中营奋斗永不放弃!

TOP

发新话题