输出arrain到img标签的src属性字节 [英] Outputting a byte arrain into the src attribute of an img tag

查看:217
本文介绍了输出arrain到img标签的src属性字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用ASP.NET MVC和有有在中的一个字段的图像(字节数组)的模型。我试图输出这一形象到img标签的src属性。我希望做类似< IMG SRC ='<%= Model.VenueImage%GT;'> 。我怎样才能做到这一点?


解决方案

与数据网址<也许内嵌图像/一>想法?


  

内嵌图像使用数据URI方案
  到网页中直接嵌入图片
  页面。正如RFC 2397,数据定义
  URI是旨在嵌入小的数据
  项目,如立竿见影的数据,因为如果他们
  被外部引用。运用
  内嵌图像保存HTTP请求过
  外部引用的对象。


  System.Drawing.Image对象图像= GetImageFromSomewhere(...);字节[]为imageData = ImageToByteArray(图片);
字符串imageBase64 = Convert.ToBase64String(为imageData);
字符串imageSrc =的String.Format(数据:图像/ GIF; BASE64,{0},imageBase64);

然后在某处页:

 &LT; IMG SRC =&LT;%= ImageSrcBytes%GT; /&GT;

据我所知,这将适用于Opera 7.2+,火狐,Safari浏览器,网景,Mozilla和IE8 +(IE8高达32KB)。工作

有关IE浏览器的早期版本有一个解决方法 - 。 MHTML

如何做到这一点的例子是这里

I'm using ASP.NET MVC and have a model which has an image (byte array) in one of the fields. I'm trying to output this image into the src attribute of the img tag. I'm looking to do something like <img src='<%= Model.VenueImage %>'>. How can I do this?

解决方案

Maybe Inline Images with Data URLs idea?

Inline images use the data URI scheme to embed images directly within web pages. As defined by RFC 2397, data URIs are designed to embed small data items as "immediate" data, as if they were referenced externally. Using inline images saves HTTP requests over externally referenced objects.

System.Drawing.Image image = GetImageFromSomewhere(...);

byte[] imageData = ImageToByteArray(image);
string imageBase64 = Convert.ToBase64String(imageData);
string imageSrc = string.Format("data:image/gif;base64,{0}", imageBase64);

and then somewhere in the page:

<img src= "<%= ImageSrcBytes %>" />

AFAIK this will work for Opera 7.2+, Firefox, Safari, Netscape, Mozilla and IE8+ (IE8 up to 32kb).

For earlier version of IE there is a workaround - MHTML.

The example how to do it is here.

这篇关于输出arrain到img标签的src属性字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆