C# url获取图片流转字符串

//http url获取图片流转字符串
//string url = serverUrl.TrimEnd(‘/‘) + PUrl;
//WebRequest request = WebRequest.Create(url);
//WebResponse response = request.GetResponse();
//Stream stream = response.GetResponseStream();
//byte[] byteFile = null;
//List<byte> bytes = new List<byte>();
//int temp = stream.ReadByte();
//while (temp != -1)
//{
// bytes.Add((byte)temp);
// temp = stream.ReadByte();
//}
//byteFile = bytes.ToArray();
//string str = Convert.ToBase64String(byteFile);
//response.Close();

//本地url获取图片流转字符串
string path = "E:/1.png";
FileStream fs = File.OpenRead(path); //OpenRead
int filelength = 0;
filelength = (int)fs.Length; //获得文件长度
Byte[] image = new Byte[filelength]; //建立一个字节数组
fs.Read(image, 0, filelength); //按字节流读取
System.Drawing.Image result = System.Drawing.Image.FromStream(fs);
fs.Close();
string str = Convert.ToBase64String(image);

return str;

原文地址:https://www.cnblogs.com/TianXiaoLu/p/9358707.html

时间: 2024-08-15 04:54:25

C# url获取图片流转字符串的相关文章

根据Url 获取图片尺寸 iOS

// 根据图片url获取图片尺寸 +(CGSize)getImageSizeWithURL:(id)imageURL {     NSURL* URL = nil;     if([imageURL isKindOfClass:[NSURL class]]){         URL = imageURL;     }     if([imageURL isKindOfClass:[NSString class]]){         URL = [NSURL URLWithString:ima

iOS根据Url 获取图片尺寸

// 根据图片url获取图片尺寸 +(CGSize)getImageSizeWithURL:(id)imageURL { NSURL* URL = nil; if([imageURL isKindOfClass:[NSURL class]]){ URL = imageURL; } if([imageURL isKindOfClass:[NSString class]]){ URL = [NSURL URLWithString:imageURL]; } if(URL == nil) return

[django]用requests从url获取图片(数据类型是bytes)后如何在template中显示出来

今天在做一个注册页面的时候遇到了一个验证码图片在页面显示的问题. 我用requests从一个url上获取到一张图片, 没有保存到本地, 而是想直接作为render的字典参数,传到页面里进行渲染.因为requests.get(url)得到的response.content是bytes, 无法像jpg等本地图片文件直接加到<img src="">中, 我的代码是这样的: 1 def get_captcha(request): 2 CAPTCHA_URL = "http

根据URL获取图片

背景:今天因为生产环境的系统界面图片无法显示被领导叼了一波,之前用Hutool工具类解析URL获取图片的,在生产环境上跑了一个多月都正常,嘣,今天突然发现周六下午后的图片统统显示异常,之后改为用java原始HttpConnection的方式解析URL获取图片OK. 简单的说下项目的业务逻辑&我当事人的想法: 1.首先,这个URL图片地址是项目上游合作伙伴通过消息队列发送给我的,然后我再把URL转为base64,接着把转好的base64上传到一个文件存储服务器中,这时可以通过Key和Object的

通过url动态获取图片大小方法总结

很多时候再项目中,我们往往需要先获取图片的大小再加载图片,但是某些特定场景,如用过cocos2d-js的人都知道,在它那里只能按比例缩放大小,是无法设置指定大小的图片的,这就是cocos2d-js 的坑了,我们必须先获取图片大小,计算比例再对图片进行缩放. 查阅资料,我总结了两种通过url获取图片大小的方法: 1.预加载获取图片大小 var imgLoad = function (url, callback) { var img = new Image(); img.src = url; if

c#获取url中的查询字符串参数

/// <summary> /// 获取url中的查询字符串参数 /// </summary> public static NameValueCollection ExtractQueryParams(string url) { int startIndex = url.IndexOf("?"); NameValueCollection values = new NameValueCollection(); if (startIndex <= 0) ret

js 获取图片url的Blob值并预览

1)使用 XMLHttpRequest 对象获取图片url的Blob值 //获取图片的Blob值 function getImageBlob(url, cb) { var xhr = new XMLHttpRequest(); xhr.open("get", url, true); xhr.responseType = "blob"; xhr.onload = function() { if (this.status == 200) { if(cb) cb(this

OC从文件或者URL获取字符串,以及写入文件

OC读取或写入文件 1 /** 2 * initWithContentsOfFile:从文件获取内容 3 * initWithContentsOfURL:从URL获取内容 4 * writeToFile:写入文件 5 * atomically:原子性(文件写入的时候不会中断,在执行完毕之前不会被任何其它任务或事件中断.) 6 */ 7 #import <Foundation/Foundation.h> 8 9 int main(int argc, const char * argv[]) {

dom4j来解析xml,通过URL获取服务器端返回的字符串,java swing 实现的页面

1)使用dom4j来解析xml字符串内容 import org.dom4j.DocumentHelper; String domString = getDomString(); Document document = DocumentHelper.parseText(domString); List<Element> list = document.selectNodes("table/tr/td/div/div/span"); String str = list.get(