開發基於 ASP.NET WebService 的圖片驗證碼服務

來源:互聯網
上載者:User
  1. 如何隨機產生圖片
       產生System.Drawing.Bitmap對象,使用System.Drawing.Graphics 向位元影像對象中繪圖。
    2. 如何在 WebService 的方法中通過參數傳遞圖片資料
       將Bitmap對象輸出成位元組流,WebMothod使用位元組數組返回該位元組流。

    執行個體:

    1. 用VS.NET 2003建立一個ASP.NET Webservice工程,預設的Service名為MyService,為MyService添加一個名為GenerateVerifyImage的WebMethod。該方法的代碼如下:

      /// 〈summary〉
      /// 產生圖片驗證碼
      /// 〈/summary〉
      /// 〈param name=“nLen“〉驗證碼的長度〈/param〉
      /// 〈param name=“strKey“〉輸出參數,驗證碼的內容〈/param〉
      /// 〈returns〉圖片位元組流〈/returns〉
      [WebMethod]
      public byte[] GenerateVerifyImage(int nLen,ref string strKey)
      {
       int nBmpWidth = 13*nLen+5;
       int nBmpHeight = 25;
       System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(nBmpWidth,nBmpHeight);

       // 1. 產生隨機背景顏色
       int nRed,nGreen,nBlue;  // 背景的三元色
       System.Random rd = new Random((int)System.DateTime.Now.Ticks);
       nRed = rd.Next(255)%128+128;
       nGreen = rd.Next(255)%128+128;
       nBlue = rd.Next(255)%128+128;

       // 2. 填充位元影像背景
       System.Drawing.Graphics graph = System.Drawing.Graphics.FromImage(bmp);
       graph.FillRectangle(new SolidBrush(System.Drawing.Color.FromArgb(nRed,nGreen,nBlue))
        ,0
        ,0
        ,nBmpWidth
        ,nBmpHeight);

       // 3. 繪製幹擾線條,採用比背景略深一些的顏色
       int nLines = 3;
       System.Drawing.Pen pen = new System.Drawing.Pen(System.Drawing.Color.FromArgb(nRed-17,nGreen-17,nBlue-17),2);
       for(int a =0;a〈 nLines;a++)
       {
        int x1 = rd.Next() % nBmpWidth;
        int y1 = rd.Next() % nBmpHeight;
        int x2 = rd.Next() % nBmpWidth;
        int y2 = rd.Next() % nBmpHeight;
        graph.DrawLine(pen,x1,y1,x2,y2);
       }

       // 採用的字元集,可以隨即拓展,並可以控制字元出現的幾率
       string strCode = “ABCDEFGHIJKLMNOPQRSTUVWXYZ“;

       // 4. 迴圈取得字元,並繪製
       string strResult = ““;
       for(int i=0;i〈nLen;i++)
       {
        int x = (i*13 + rd.Next(3));
        int y = rd.Next(4) + 1;

        // 確定字型
        System.Drawing.Font font = new System.Drawing.Font(“Courier New“,
         12 + rd.Next()%4,
         System.Drawing.FontStyle.Bold);
        char c = strCode[rd.Next(strCode.Length)];  // 隨機擷取字元
        strResult += c.ToString();

        // 繪製字元
        graph.DrawString(c.ToString(),
         font,
         new SolidBrush(System.Drawing.Color.FromArgb(nRed-60+y*3,nGreen-60+y*3,nBlue-40+y*3)),
         x,
         y);
       }

       // 5. 輸出位元組流
       System.IO.MemoryStream bstream = new System.IO.MemoryStream();
       bmp.Save(bstream,System.Drawing.Imaging.ImageFormat.Jpeg);
       bmp.Dispose();
       graph.Dispose();

       strKey = strResult;
       byte[] byteReturn = bstream.ToArray();
       bstream.Close();

       return byteReturn;
      }

    2. 測試WebMethod,添加一個WebForm,引用上述WebService,引用名為imagesvr。在Page_Load中添加代碼:

       ...
       imagesvr.MyService imgsvr = new imagesvr.MyService();
       string strKey = ““;
       byte[] data = imgsvr.GenerateVerifyImage(5,ref strKey);
       Response.OutputStream.Write(data,0,data.Length);
       ...

    3. 運行。每次refresh這個WebForm時,就會顯示一個新產生的圖片驗證碼,而函數的輸出參數strKey儲存的就是這個驗證碼的實際內容,可以儲存在Session中,作為驗證使用。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.