根据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. linux命令学习之:ifup/ifdown

    ifup命令网络配置 ifup命令用于激活指定的网络接口.ifdown命令用于禁用指定的网络接口. 实时地手动修改一些网络接口参数,可以利用ifconfig来实现,如果是要直接以配置文件,亦即是在 / ...

  2. C#按制定的环境编译替换不出对应的配置项的解决措施。

    1. 比如选择的 编译或者发布 环境是 QA ,但是QA里面配置的 替换节点 实际并没有被替换 解决方案: 在项目文件.csproj中最底部加入一下代码,应该成功.成功将QA的配置节点 替换掉默认的 ...

  3. iOS.UITableView.SectionIndex

    1. 为tableview中section建立索引来加速tableview的滚动. http://nshipster.com/uilocalizedindexedcollation/ 2. 获取汉字的 ...

  4. GitHub上README.md教程 详情介绍 (修改图片连接地址错误)

    最近对它的README.md文件颇为感兴趣.便写下这贴,帮助更多的还不会编写README文件的同学们. README文件后缀名为md.md是markdown的缩写,markdown是一种编辑博客的语言 ...

  5. http协议(四)http常用状态码

    一:http状态码 表示客户端http请求的返回结果.标记服务器端的处理是否正常.通知出现的错误等工作 状态码的类别如下: http状态码种类繁多,大概有60多种,实际上经常使用的只有14种,下面为一 ...

  6. Windows 下 Phpstrom 配置git使用

    首先先去下载 git 下载链接 https://git-scm.com/download/winphpstrom 配置git    链接  http://jingyan.baidu.com/artic ...

  7. C/C++常用预处理指令

    预处理是在编译之前的处理,而编译工作的任务之一就是语法检查,预处理不做语法检查.预处理命令以符号“#”开头. 常用的预处理指令包括: 宏定义:#define 文件包含:#include 条件编译:#i ...

  8. Spring 注解驱动(一)基本使用规则

    Spring 注解驱动(一)基本使用规则 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) 一.基本使用 @Configur ...

  9. IOS初级:UIScrollView & UIPageControl

    UIScrollView其实构建的就像一列很长的火车,每滑动一个屏幕,展示一节车厢. //主屏幕高度 #define kScreenHeight [UIScreen mainScreen].bound ...

  10. IOS初级:UIView和UIButton

    AppDelegate.m - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDict ...