.net 根据图片网络地址获取图片二进制字节数据流
根据html路径获取图片的字节
/// <summary> ///根据html路径获取图片的字节 /// </summary> /// <param name="picSize">图片尺寸,原图:1,大图:2,中图:3,小图:4</param> /// <param name="serverPath">图片服务器地址</param> /// <returns></returns> public static byte[] GetImageByteByHtmlImgUrl(string imgSize, string serverPath) { byte[] bt = new byte[4096]; try { serverPath = serverPath.Replace("_1.", string.Format("_{0}.", imgSize)); Image obj = Image.FromStream(System.Net.WebRequest.Create(serverPath).GetResponse().GetResponseStream()); bt = ImageToByteArray(obj); //使用ImageToByteArray()函数 将Image类型转成Byte[]类型 obj.Dispose(); return bt; } catch //如果获取异常 则使用资源中的图片显示 { //Image obj = Image.FromStream(""); //byte[] bt = ImageToByteArray(obj); //obj.Dispose(); //return bt; } return bt; }
关键代码:
Image obj = Image.FromStream(System.Net.WebRequest.Create(serverPath).GetResponse().GetResponseStream());
从网络上先存储成本地图片,再从本地图片中转成二进制数据流
/// <summary> /// 获取商品图片的二进制数据流 /// </summary> /// <param name="picSize">图片尺寸,原图:1,大图:2,中图:3,小图:4</param> /// <param name="filePath">本地路径</param> /// <param name="serverPath">图片服务器地址</param> public static byte[] GetServerFile(string picSize, string filePath, string serverPath) { byte[] buffer = new byte[4096]; System.Net.HttpWebRequest hr; FileStream fs = null; Stream s = null; try { filePath = System.IO.Directory.GetCurrentDirectory() + "\\" + "byteimg" + "\\" + filePath; filePath = filePath.Replace("_1.", string.Format("_{0}.", picSize)); hr = (HttpWebRequest)HttpWebRequest.Create(serverPath); if (!File.Exists(filePath)) { string path = System.IO.Path.GetDirectoryName(filePath); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } // _FileDir = str; // Flag = G_Method.GetServerFile(LoadFileName, _ImageAddress + NVRGoodsImageFile.Replace("\\", "//")); } //if (File.Exists(filePath)) //{ // return true; //} fs = new FileStream(filePath, FileMode.Create); s = hr.GetResponse().GetResponseStream(); int bytesRead = s.Read(buffer, 0, buffer.Length); while (bytesRead > 0) { fs.Write(buffer, 0, bytesRead); bytesRead = s.Read(buffer, 0, buffer.Length); } // return true; } catch (Exception e) { //throw new ApplicationException("Could not download file " + serverPath, e); // return false; } finally { if (s != null) s.Close(); if (fs != null) fs.Close(); } return buffer; }
Code By 博客园-曹永思
根据image获取图片的字节
/// <summary> /// 根据image获取图片的字节 /// </summary> /// <param name="image"></param> /// <returns></returns> private static byte[] ImageToByteArray(Image image) { System.IO.MemoryStream mStream = new System.IO.MemoryStream(); image.Save(mStream, System.Drawing.Imaging.ImageFormat.Png); byte[] ret = mStream.ToArray(); mStream.Close(); return ret; }
根据图片字节流获取Image实例
/// <summary> /// 根据图片字节流获取Image实例 /// </summary> /// <param name="imagebyte"></param> /// <returns></returns> public static Image SetByteToImage(byte[] imagebyte) { Image image; MemoryStream imagememorystream = new MemoryStream(imagebyte, 0, imagebyte.Length); image = Image.FromStream(imagememorystream); return image; }