1. 程式人生 > >c# WPF 獲取網絡圖片,驗證碼

c# WPF 獲取網絡圖片,驗證碼

har 獲取 contain buffer new streams ebr ref init

c# WPF 獲取網絡圖片,驗證碼

  public static BitmapImage getValidCodeBitmap() { 
       
        string url = "http://my.baaaahe.com/Getinterlogin/getVerifyPic?jsonCallBack=?&tmpId=1547785891276";
        BitmapImage b = new BitmapImage();
        try
        {

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.CookieContainer = Common.myCookieContainer;
            request.Referer = "http://my.baaaaihe.com/";
            HttpWebResponse resp = (HttpWebResponse)request.GetResponse();
            Common.myCookieContainer.Add(resp.Cookies);
            Stream stream = resp.GetResponseStream();
         

            MemoryStream ms = new MemoryStream();
            const int bufferLen = 4096;
            byte[] buffer = new byte[bufferLen];
            int count = 0;
            while ((count = stream.Read(buffer, 0, bufferLen)) > 0)
            {
                ms.Write(buffer, 0, count);
            }
            ms.Seek(0, SeekOrigin.Begin);
            stream.CopyTo(ms);
            b.BeginInit();
            b.StreamSource = ms;
            b.EndInit();
        }

        catch (Exception ex)
        {

            MessageBox.Show(ex.Message);

        }
        return b;

        
        }

  

c# WPF 獲取網絡圖片,驗證碼