专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »DotNet » 获取本地ip:C#获取图片并保存到本地 »正文

获取本地ip:C#获取图片并保存到本地

来源: 发布时间:星期四, 2009年2月12日 浏览:248次 评论:0


C#获取图片并保存到本地
public SaveImageFromWeb( imgUrl, path, fileName) 
  ...{ 
   (path.Equals(\"\")) 
    throw Exception(\"未指定保存文件路径\"); 

   imgName = imgUrl..Sub(imgUrl..LastIndexOf(\"/\")+1); 
   defaultType = \".jpg\"; 
   imgTypes = ...{\".jpg\",\".jpeg\",\".png\",\".g\",\".bmp\"}; 
   imgType = imgUrl..Sub(imgUrl..LastIndexOf(\".\")); 
   foreach ( it in imgTypes) 
   ...{ 
    (imgType.ToLower.Equals(it)) 
    
    (it.Equals(\".bmp\")) 
     imgType = defaultType; 
   } 

   HttpWebRequest request = (HttpWebRequest)WebRequest.Create(imgUrl); 
   request.UserAgent = \"Mozilla/6.0 (MSIE 6.0; Windows NT 5.1; Natas.Robot)\"; 
   request.Timeout = 3000; 

   WebResponse response = request.GetResponse
   Stream stream = response.GetResponseStream

   ( response.ContentType.ToLower.StartsWith(\"image/\") ) 
   ...{ 
    .gif' />Byte = [1024]; 
    imgLong = ()response.ContentLength; 
    l = 0; 

    (fileName \"\") 
     fileName = imgName; 

    FileStream fso = FileStream(path+fileName+imgType,FileMode.Create); 
    while(l<imgLong) 
    ...{ 
     i = stream.Read(.gif' />Byte,0,1024); 
     fso.Write(.gif' />Byte,0,i); 
     l i;  
    } 

    fso.Close
    stream.Close
    response.Close

    1; 
   } 
    
   ...{ 
    0; 
   } 
  }


0

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: