.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[]; 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 根据图片网络地址获取图片二进制字节数据流的更多相关文章
- Qt 打开安卓相冊选择图片并获取图片的本地路径
Qt 打开安卓相冊选择图片并获取图片的本地路径 过程例如以下: 通过 Intent 打开安卓的系统相冊. 推荐使用 QAndroidJniObject::getStaticObjectField 获取 ...
- Android根据图片Uri获取图片path绝对路径的几种方法【转】
在Android 编程中经常会用到Uri转化为文件路径,如我们从相册选择图片上传至服务器,一般上传前需要对图片进行压缩,这时候就要用到图片的绝对路径. 下面对我开发中uri转path路径遇到的问题进行 ...
- Android -- 加载大图片到内存,从gallery获取图片,获取图片exif信息
1. 加载大图片到内存,从gallery获取图片 android默认的最大堆栈只有16M, 图片像素太高会导致内存不足的异常, 需要将图片等比例缩小到适合手机屏幕分辨率, 再加载. 从gallery ...
- Android 打开照相机、获取相册图片、获取图片并裁减
一.调用照相机 注:surfaceView在当Activity不在前台的时候,会被销毁(onPause方法之后,执行销毁方法)当Activity回到前台时,在Activity执行onResume方法之 ...
- android 根据图片名字获取图片id
public int getResource(String imageName){ Context ctx=getBaseContext(); int resId = getResources().g ...
- iOS获取相册/相机图片-------自定义获取图片小控件
一.功能简介 1.封装了一个按钮,点击按钮,会提示从何处获取图片:如果设备支持相机,可以从相机获取,同时还可以从手机相册获取图片. 2.选择图片后,有一个block回调,根据需求,将获得的图片拿来使用 ...
- 根据图片URL获取图片的尺寸【Swift语言实现】
import UIKit extension UIImage { /// 获取网络图片尺寸 /// /// - Parameter url: 网络图片链接 /// - Returns: 图片尺寸siz ...
- html5plus 从相册选择图片后获取图片的大小
plus.gallery.pick(function (filePath) { plus.io.resolveLocalFileSystemURL(filePath, function (entry) ...
- iOS根据Url 获取图片尺寸
iOS根据Url 获取图片尺寸 // 根据图片url获取图片尺寸 +(CGSize)getImageSizeWithURL:(id)imageURL { NSURL* URL = nil; if([i ...
随机推荐
- Windows到Linux的文件上传、下载
1.使用我们常用的Xshell登录工具,新建立一个远程会话,填写ip地址及用户名密码后,选择最下面的ZMODEM,填写下载的路径.加载的路径:2个路径可以一样也可以不一样: 2.在Linux主机上,安 ...
- Linux操作系统-系统安装与分区
.磁盘分区 使用分区工具在磁盘上划分几个逻辑部分,一旦分成几个分区,不同类型的目录和文件可以存储进不同的分区2.分区类型主分区:最多只能有4个扩展分区:最多只能有1个:主分区加扩展分区最多有4个:扩展 ...
- C#—Dev XtraTabControl操作总结如动态增加Tab和关闭选项卡方法等
1:显示行号 找到gridview属性 点击事件 CustomDrawRowIndicator private void gridView1_CustomDrawRowIndicator(object ...
- 说说为什么会有ssl.CertificateError报错
有一些网站没有获取浏览器的颁发的安全证书 当你在请求这个网站时浏览器会当做不安全网站处理 因此会报ssl.CertificateError 解决办法是 :将默认的证书验证模式修改为不需要验证 代码如下 ...
- 重启ngix失败
问题如下: 解决办法:杀死nginx进程,再重启 .查找nginx主进程:ps -ef|grep nginx .杀死进程:kill - .启动:cd /usr/sbin./nginx -t
- \\Device\\PhysicalMemory
从Windows Server 2003 with SP1 以后就禁用了用户态访问\\Device\\PhysicalMemory,要访读取SMBIOS的信息,请使用以下API:•EnumSystem ...
- Python之路番外(第二篇):PYTHON基本数据类型和小知识点
一.基础小知识点 1.如果一行代码过长,可以用续行符 \换行书写 例子 if (signal == "red") and \ (car == "moving") ...
- Add to Array-Form of Integer LT989
For a non-negative integer X, the array-form of X is an array of its digits in left to right order. ...
- Trapping Rain Water LT42
The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of ...
- (xxx.55).toFixed(1) 无法正确进位处理
参考:https://juejin.im/post/5a11a9fef265da43284073b4?utm_medium=fe&utm_source=weixinqun 根本原因在于2.55 ...