.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 ...
随机推荐
- Inno Setup自定义安装界面脚本
; 脚本由 Inno Setup 脚本向导 生成! ; 有关创建 Inno Setup 脚本文件的详细资料请查阅帮助文档! #define MyAppName "RemoteCard&quo ...
- 转:css知多少(1)——我来问你来答
1. 引言 各位前端或者伪前端(比如作者本人)的同志们,css对你们来说不是很陌生.比如我,在几年之前上大学的时候,给外面做网站就用css,而且必须用css.这样算下来也得六年多了,有些功能可能轻车熟 ...
- UOJ 274 温暖会指引我们前进 - LCT
Solution 更新掉路径上温暖度最小的边就可以了~ Code #include<cstdio> #include<cstring> #include<algorith ...
- BZOJ 1874 取石子游戏 - SG函数
Description $N$堆石子, $M$种取石子的方式, 最后取石子的人赢, 问先手是否必胜 $A_i <= 1000$,$ B_i <= 10$ Solution 由于数据很小, ...
- python 找出一篇文章中出现次数最多的10个单词
#!/usr/bin/python #Filename: readlinepy.py import sys,re urldir=r"C:\python27\a.txt" disto ...
- Spring 注解(一)Spring 注解编程模型
Spring 注解(一)Spring 注解编程模型 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Spring 注解系列 ...
- TP QQ 微信 微博登录
use Org\Util\QQconnect; use Org\Util\Wechatauth; use Org\Util\SaeTOAuthV2; use Org\Util\SaeTClientV2 ...
- R.java的生成规则
0x7f010000 开头的是attr 0x7f050000 开头的是anim 0x7f0b0002 开头的是bool 0x7f020000 开头的是drawable 0x7f060000 开头的是i ...
- xcode如何运行下载的demo工程
1. 首先你需要改Bundle Identifier,修改成别人没注册过的. 2. 选中 “Automatically manage signing” 3. 在Team里面添加自己的个人帐号了.
- SpringMVC学习笔记:单例与并发问题
Spring中的Bean默认都是单例(singleton),Spring中Bean的scope属性有五种类型: singleton 表示在spring容器中的单例,通过spring容器获得该bean时 ...