• 获取指定网址下的html页面中的图片文件


            // 获取指定网址下的html页面中的图片文件路径列表
            private static List<string> getImageUrlList(string strSource)
            {
                Regex reg = new Regex(@"http://\S+\.(gif|bmp|png|jpg)");
                MatchCollection mc = reg.Matches(strSource);

                List<string> strList = new List<string>();

                for (int i = 0; i < mc.Count; i++)
                {
                    strList.Add(String.Format("{0}\r\n", mc[i].Value));
                }
                List<string> newList = new List<string>();
                newList.AddRange(strList.Distinct());
                return newList;
            }

            // 获取指定url路径的图片,保存下来
            private static void getUrlImage(string imageUrl)
            {
                try
                {
                    int lastIndex = imageUrl.LastIndexOf('/');
                    string fileName = imageUrl.Substring(lastIndex + 1); // 文件名称

                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(imageUrl);
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    Stream receiveStream = response.GetResponseStream(); // 网络stream, 不可获取长度
                   
                    BinaryReader reader = new BinaryReader(receiveStream);
                    string imagePath = @".\img\";
                    if (!Directory.Exists(imagePath))
                        Directory.CreateDirectory(imagePath);
                    using (FileStream stream = new FileStream( imagePath + fileName, FileMode.Create))
                    {
                        byte[] buffer = new byte[256]; // 通过缓存取得数据
                        int count = reader.Read(buffer, 0, 256);
                        while (count > 0)
                        {
                            stream.Write(buffer, 0, count);
                            count = reader.Read(buffer, 0, 256);
                        }
                        stream.Flush();
                    }
                    receiveStream.Close();
                }
                catch { }

            }

            // 调用示例
            private void captureImageByUrl(string urlStr)
            {
                string strSource = getUrlHtml(urlStr);

                if (strSource == String.Empty) return;

                List<string> strList = getImageUrlList(strSource);

                StringBuilder sb = new StringBuilder();       
                foreach (string item in strList)
                {
                    sb.Append(item);
                    getUrlImage(item.TrimEnd(new char[] { '\r', '\n' }));
                }
                MessageBox.Show(sb.ToString());
            }

    ~做事情贵在坚持~
  • 相关阅读:
    STL的二分查找binary_search
    转_HDU_1907&2509 博弈(Nim博弈变形)
    HDU3589_Jacobi symbol_二次剩余
    转载_模运算_归纳得很精华啊
    HDU3501_calculation2_欧拉函数求和
    【转】求小于等于N的与N互质的数的和
    HDU3328_Flipper_纸牌翻转_模拟题
    转_求逆元的两种方法
    HDU2541_Simple Addition Expression
    MySql数据同步(双机热备)已正式应用上平台
  • 原文地址:https://www.cnblogs.com/csMapx/p/2207276.html
Copyright © 2020-2023  润新知