根据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[]; 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[]; 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, , buffer.Length);
while (bytesRead > )
{
fs.Write(buffer, , bytesRead);
bytesRead = s.Read(buffer, , 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, , imagebyte.Length);
image = Image.FromStream(imagememorystream);
return image;
}

.net 根据图片网络地址获取图片二进制字节数据流的更多相关文章

  1. Qt 打开安卓相冊选择图片并获取图片的本地路径

    Qt 打开安卓相冊选择图片并获取图片的本地路径 过程例如以下: 通过 Intent 打开安卓的系统相冊. 推荐使用 QAndroidJniObject::getStaticObjectField 获取 ...

  2. Android根据图片Uri获取图片path绝对路径的几种方法【转】

    在Android 编程中经常会用到Uri转化为文件路径,如我们从相册选择图片上传至服务器,一般上传前需要对图片进行压缩,这时候就要用到图片的绝对路径. 下面对我开发中uri转path路径遇到的问题进行 ...

  3. Android -- 加载大图片到内存,从gallery获取图片,获取图片exif信息

    1. 加载大图片到内存,从gallery获取图片 android默认的最大堆栈只有16M, 图片像素太高会导致内存不足的异常, 需要将图片等比例缩小到适合手机屏幕分辨率, 再加载. 从gallery ...

  4. Android 打开照相机、获取相册图片、获取图片并裁减

    一.调用照相机 注:surfaceView在当Activity不在前台的时候,会被销毁(onPause方法之后,执行销毁方法)当Activity回到前台时,在Activity执行onResume方法之 ...

  5. android 根据图片名字获取图片id

    public int getResource(String imageName){ Context ctx=getBaseContext(); int resId = getResources().g ...

  6. iOS获取相册/相机图片-------自定义获取图片小控件

    一.功能简介 1.封装了一个按钮,点击按钮,会提示从何处获取图片:如果设备支持相机,可以从相机获取,同时还可以从手机相册获取图片. 2.选择图片后,有一个block回调,根据需求,将获得的图片拿来使用 ...

  7. 根据图片URL获取图片的尺寸【Swift语言实现】

    import UIKit extension UIImage { /// 获取网络图片尺寸 /// /// - Parameter url: 网络图片链接 /// - Returns: 图片尺寸siz ...

  8. html5plus 从相册选择图片后获取图片的大小

    plus.gallery.pick(function (filePath) { plus.io.resolveLocalFileSystemURL(filePath, function (entry) ...

  9. iOS根据Url 获取图片尺寸

    iOS根据Url 获取图片尺寸 // 根据图片url获取图片尺寸 +(CGSize)getImageSizeWithURL:(id)imageURL { NSURL* URL = nil; if([i ...

随机推荐

  1. 本地推送UILocalNotification的一些简单方法

    1.添加本地推送,需要在app添加推送.可以根据通知的userInfo的不同的键值对来区分不同的通知 UILocalNotification *notification = [[UILocalNoti ...

  2. 如何获取google Api Key

    1.使用keytool申请获取MD5值 1.1 找到你的debug.keystore Windows Vista/7: C:\Users\<username>\.android\debug ...

  3. threejs 世界坐标转化为屏幕坐标

    网站: http://www.yanhuangxueyuan.com/Three.js_course/screen.html 方法.project 通过Vector3对象的方法project,方法的参 ...

  4. git 分支强制删除

    添加一个新功能时,你肯定不希望因为一些实验性质的代码,把主分支搞乱了,所以,每添加一个新功能,最好新建一个feature分支,在上面开发,完成后,合并,最后,删除该feature分支. 现在,你终于接 ...

  5. 2D情况下,复数的意义代表旋转

    4 x i x i = - 4 就是"4"在数轴上旋转了180度. 那么4 x i = 4i 就旋转了90度. 复数的意义就表示旋转 乘以-1,表示x正半轴的数,围绕原点,逆时针偏 ...

  6. IDEA 的 Edit 设置

    1.设置鼠标悬浮提示 General -- Show quick documentation on mouse move 2.自动导包 3.设置显示行号和方法的间隔符 4.忽略大小写  4.设置取消单 ...

  7. Jsonpath的基本使用

    JSONPath - 是xpath在json的应用. xml最大的优点就有大量的工具可以分析,转换,和选择性的提取文档中的数据.XPath是这些最强大的工具之一.   如果可以使用xpath来解析js ...

  8. Win7下Qt5的安装及使用

    1.安装Qt5 Qt5的安装比Qt4的安装简单多了,我装的是Qt5.4(qt-opensource-windows-x86-mingw491_opengl-5.4.0.exe),它集成了MinGW.Q ...

  9. VS2010错误

    1.用VS2010生成C++程序时,链接器工具错误 LNK1123: fatal error LNK1123: failure during conversion to COFF: file inva ...

  10. 201621123008 《Java程序设计》第十周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2. 书面作业 本次PTA作业题集异常 1. 常用异常 结合题集题目7-1回答 1.1 自己以前编写的代码中经常出现 ...