C# HTTP 路径图片转为 byte 字节数据
输入图片Http地址,即可返回byte[] bytes
public static byte[] Url_To_Byte(String filePath)
{
//第一步:读取图片到byte数组
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(filePath);
byte[] bytes;
using (Stream stream = request.GetResponse().GetResponseStream())
{
using (MemoryStream mstream = new MemoryStream())
{
int count = 0;
byte[] buffer = new byte[1024];
int readNum = 0;
while ((readNum = stream.Read(buffer, 0,1024)) > 0)
{
count = count + readNum;
mstream.Write(buffer, 0, readNum);
}
mstream.Position = 0;
using (BinaryReader br = new BinaryReader(mstream))
{
bytes = br.ReadBytes(count);
}
}
}
return bytes;
}