产生随机图片,并返回图片内的文字

/// <summary>
        
/// 产生随机图片,并返回图片内的文字
        
/// </summary>
        
/// <param name="img">要显示图片的控件</param>        
        
/// <param name="path">图片显示的文字</param>

        public string R_random(System.Web.UI.WebControls.Image img,string path)
        
{                
            
string tmp;//文字
            Random rnd=new Random();
            tmp
=rnd.Next().ToString().Substring(0,4);
            Graphics g
=null;
            Bitmap bmp
=new Bitmap(50,100);
            g
=Graphics.FromImage(bmp);
            SizeF rec
=g.MeasureString(tmp,new Font("宋体",12));
            
int nwidth=(int)rec.Width;
            
int nheight=(int)rec.Height;
            g.Dispose();
            bmp.Dispose();
            bmp
=new Bitmap(nwidth,nheight);
            g
=Graphics.FromImage(bmp);
            g.FillRectangle(
new SolidBrush(Color.FromArgb(90126220)), new Rectangle(0,0,nwidth,nheight));
            g.DrawString(tmp,
new Font("宋体",12), new SolidBrush(Color.Yellow), new PointF(00));
            
string fff=path+ @"tmp\validation.gif";
            bmp.Save(fff,System.Drawing.Imaging.ImageFormat.Gif);                
            img.ImageUrl
=fff;
            
return tmp;
原文地址:https://www.cnblogs.com/aipeli/p/251775.html