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?
我用ASP。NET MVC,在其中一个字段中有一个具有图像(字节数组)的模型。我尝试将这个图像输出到img标签的src属性中。我想做一些类似
2 个解决方案
#1
19
Maybe Inline Images with Data URLs idea?
也许内联图像有数据url的想法?
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.
内联图像使用数据URI方案将图像直接嵌入到web页面中。根据RFC 2397的定义,数据uri的设计目的是将小数据项嵌入到“即时”数据中,就好像它们是在外部引用的一样。使用内联映像可以在外部引用的对象上保存HTTP请求。
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).
这将适用于Opera 7.2+、Firefox、Safari、Netscape、Mozilla和IE8+ (IE8最多32kb)。
For earlier version of IE there is a workaround - MHTML.
对于早期版本的IE,有一个变通方法——MHTML。
The example how to do it is here.
这个例子是怎么做的。
#2
0
The browser uses the src attribute to issue a separate request to the server, to get the contents of the image. It does not display the bytes in the src as the image itself.
浏览器使用src属性向服务器发出单独的请求,以获取图像的内容。它不将src中的字节显示为图像本身。
So you will need to remember those bytes (session?) and use a handler (the url you put into the src attribute) to let the browser request them.
因此,您将需要记住这些字节(会话?)并使用一个处理程序(将url放入src属性)让浏览器请求它们。
#1
19
Maybe Inline Images with Data URLs idea?
也许内联图像有数据url的想法?
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.
内联图像使用数据URI方案将图像直接嵌入到web页面中。根据RFC 2397的定义,数据uri的设计目的是将小数据项嵌入到“即时”数据中,就好像它们是在外部引用的一样。使用内联映像可以在外部引用的对象上保存HTTP请求。
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).
这将适用于Opera 7.2+、Firefox、Safari、Netscape、Mozilla和IE8+ (IE8最多32kb)。
For earlier version of IE there is a workaround - MHTML.
对于早期版本的IE,有一个变通方法——MHTML。
The example how to do it is here.
这个例子是怎么做的。
#2
0
The browser uses the src attribute to issue a separate request to the server, to get the contents of the image. It does not display the bytes in the src as the image itself.
浏览器使用src属性向服务器发出单独的请求,以获取图像的内容。它不将src中的字节显示为图像本身。
So you will need to remember those bytes (session?) and use a handler (the url you put into the src attribute) to let the browser request them.
因此,您将需要记住这些字节(会话?)并使用一个处理程序(将url放入src属性)让浏览器请求它们。