c#根据路径(url)下载图片


方法一:根据路径下载图片
1 /// <summary>
/// 图片另存为
/// </summary>
/// <param name="url">路径</param>
public void ImgSave(string url)
{
  //http://203.156.245.58/sipgl/index.jsp
  url = "http://203.156.245.58/sipgl/login/img";
  WebRequest imgRequest = WebRequest.Create(url);   HttpWebResponse res;
  try
  {
    res = (HttpWebResponse)imgRequest.GetResponse();
  }
    catch (WebException ex)
  {   res = (HttpWebResponse)ex.Response;
  }   if (res.StatusCode.ToString() == "OK")
  {
    System.Drawing.Image downImage = System.Drawing.Image.FromStream(imgRequest.GetResponse().GetResponseStream());     string deerory = string.Format(@"D:\img\{0}\", DateTime.Now.ToString("yyyy-MM-dd"));     string fileName = string.Format("{0}.png", DateTime.Now.ToString("HHmmssffff"));   if (!System.IO.Directory.Exists(deerory))
  {
    System.IO.Directory.CreateDirectory(deerory);
  }
    downImage.Save(deerory + fileName);
    downImage.Dispose();
  } }

c#根据路径(url)下载图片

方法二:根据路径下载图片

      /// <summary>
/// 下载图片
/// </summary>
/// <param name="picUrl">图片Http地址</param>
/// <param name="savePath">保存路径</param>
/// <param name="timeOut">Request最大请求时间,如果为-1则无限制</param>
/// <returns></returns>
public bool DownloadPicture(string picUrl, string savePath, int timeOut)
{
picUrl = "http://203.156.245.58/sipgl/login/img";
savePath = "D:/img/"+DateTime.Now.ToString("HHmmssffff")+".jpg";
bool value = false;
WebResponse response = null;
Stream stream = null;
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(picUrl);
if (timeOut != -) request.Timeout = timeOut;
response = request.GetResponse();
stream = response.GetResponseStream();
if (!response.ContentType.ToLower().StartsWith("text/"))
value = SaveBinaryFile(response, savePath);
}
finally
{
if (stream != null) stream.Close();
if (response != null) response.Close();
}
return value;
}
private static bool SaveBinaryFile(WebResponse response, string savePath)
{
bool value = false;
byte[] buffer = new byte[];
Stream outStream = null;
Stream inStream = null;
try
{
if (File.Exists(savePath)) File.Delete(savePath);
outStream = System.IO.File.Create(savePath);
inStream = response.GetResponseStream();
int l;
do
{
l = inStream.Read(buffer, , buffer.Length);
if (l > ) outStream.Write(buffer, , l);
} while (l > );
value = true;
}
finally
{
if (outStream != null) outStream.Close();
if (inStream != null) inStream.Close();
}
return value;
}

c#根据路径(url)下载图片

c#根据路径(url)下载图片

方法三  根据路径下载图片

     public Image GetImage(string url,out string imageStrCookie)
{ HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://203.156.245.58/sipgl/login/img");
request.Method = "GET";
WebResponse response = request.GetResponse();
imageStrCookie = "";
if (response.Headers.HasKeys()&&null !=response.Headers["Set-Cookie"])
{
imageStrCookie= response.Headers.Get("Set-Cookie");
}
return Image.FromStream(response.GetResponseStream()); }

c#根据路径(url)下载图片



上一篇:新建maven指定jdk版本-eclipse新建maven项目报错The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path的解决方案


下一篇:基于三层架构下的公共数据访问方法(Sqlite数据库)