一個可設定背景圖片的驗證碼圖片的產生方法

來源:互聯網
上載者:User

參數說明:

sourceFile:背景圖片檔案

text:要顯示的文本

public void MakeCheckImage(string sourceFile,string text)
  {
   Response.Clear();
   try
   {
    //原圖
    Bitmap sImage = new Bitmap(sourceFile);    
   
    //驗證碼圖
    Bitmap wImage = new Bitmap(100,30,System.Drawing.Imaging.PixelFormat.Format24bppRgb);    
    Graphics wg = Graphics.FromImage(wImage);
    wg.Clear(Color.White);
    wg.DrawString(text,new Font("Verdana",18),new SolidBrush(Color.Black),0,0);
    wg.Save();
    
    //make Graphics.
    Graphics g = Graphics.FromImage(sImage);
    int x; //臨時變數
    int y; //監時變數    
    int w = 100; //驗證圖的寬度
    int h = 30; //驗證圖的高度
    int al; //alpha
    int rl; //Red
    int gl; //Green
    int bl; //Blue
    
    //開始繪圖
    for(x = 1; x < w; x++)
    {
     for(y = 1; y < h; y++)
     {
      al = wImage.GetPixel(x,y).A;
      rl = wImage.GetPixel(x,y).R;
      gl = wImage.GetPixel(x,y).G;
      bl = wImage.GetPixel(x,y).B;
      al = 70;
     
      if(rl - 50 > 0)
       rl -= 50;
      if(gl - 50 > 0)
       gl -= 50;
      if(bl - 50 > 0)
       bl -= 50;
      g.DrawEllipse(new Pen(new SolidBrush(Color.FromArgb(al,rl,gl,bl))),x,y,1,1);           
     }
    }  
    g.Save();
    sImage.Save(Response.OutputStream,ImageFormat.Jpeg);    
    wg.Dispose();
    g.Dispose();
    sImage.Dispose();
    wImage.Dispose();    
   }
   catch(Exception e)
   {
    Response.Write(e.Message);
   } 
   Response.End();
  }


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.