C# .NET saves the Image image to the local disk according to the Url link

According to the Url link of an Image , an image can be displayed in the browser. If you want to save the image on the local disk through code, you can use the following methods:

First get a binary array of pictures.

static public byte[] GetBytesFromUrl(string url)
{
byte[] b;
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
WebResponse myResp = myReq.GetResponse();

Stream stream = myResp.GetResponseStream();
//int i;
using (BinaryReader br = new BinaryReader(stream))
{
//i = (int)(stream.Length);
b = br.ReadBytes(500000);
br.Close();
}
myResp.Close();
return b;
}

Save to disk file.

static public void WriteBytesToFile(string fileName, byte[] content)
{
FileStream fs = new FileStream(fileName, FileMode.Create);
BinaryWriter w = new BinaryWriter(fs);
try
{
w.Write(content);
}
finally
{
fs.Close();
w.Close();
}

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324896323&siteId=291194637