C# 无论如何在asp图像控件上显示动态生成的位图?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14720929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there anyway to display dynamically generated Bitmap on a asp image control?
提问by user2042023
In my code I create a bitmap dynamically, using c# and ASP.NET. Than I need to display it on the asp image control. There are anyway to do it whithout using handlers?
在我的代码中,我使用 c# 和 ASP.NET 动态创建了一个位图。比我需要在asp图像控件上显示它。无论如何都可以在不使用处理程序的情况下做到这一点?
采纳答案by nunespascal
Using a ashx handler is better cause it works on all browsers and you can cache the output images on the client.
使用 ashx 处理程序更好,因为它适用于所有浏览器,并且您可以在客户端上缓存输出图像。
However if you must do it, images can be displayed inline directly using the <img>
tag as follows:
但是,如果您必须这样做,可以直接使用<img>
标签内联显示图像,如下所示:
<img src="data:image/gif;base64,<YOUR BASE64 DATA>" width="100" height="100"/>
ASPX:
ASPX:
<img runat="server" id="imgCtrl" />
CS:
CS:
MemoryStream ms = new MemoryStream();
bitmap.Save(ms, ImageFormat.Gif);
var base64Data = Convert.ToBase64String(ms.ToArray());
imgCtrl.Src = "data:image/gif;base64," + base64Data;
Yes, you could write the bitmap directly, but compressed formats(JPEG, GIF) are better for the web.
是的,您可以直接编写位图,但压缩格式(JPEG、GIF)更适合网络。
Note:Inline images don't work on older browsers. Some versions of IE had limitations of max 32KB size.
注意:内嵌图像不适用于旧版浏览器。某些版本的 IE 有最大 32KB 大小的限制。