如何将 MemoryStream 绑定到 asp:image 控件?
有没有办法将 MemoryStream 绑定到 asp:image 控件?
Is there a way to bind a MemoryStream to asp:image control?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
有没有办法将 MemoryStream 绑定到 asp:image 控件?
Is there a way to bind a MemoryStream to asp:image control?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(8)
没有。
但您可以创建一个特殊页面来流式传输该图像。 首先,将图像的 URL 设置为执行流式传输的页面,包括一些 url 参数,让您知道从哪里获取图像:
在 GetImage.aspx 中,您从 URL 获取文件名(或其他内容),加载MemoryStream 中的图像,然后将该内存流的内容直接写入 HttpResponse:
Nope.
But you can create a special page to stream that image out. First, you set the URL of the image to the page that performs the streaming, including some url parameters that let you know where to get the image:
in GetImage.aspx, you get the filename (or whatever) from the URL, load the image in your MemoryStream, and then write the content of that memory stream directly to the HttpResponse:
您可以将 Telerik 的 BinaryImage 控件用于 ASP.net。
更多信息在这里:
http://www.telerik.com/products/aspnet-ajax/binaryimage.aspx
You can use Telerik's BinaryImage control for ASP.net.
More info in here:
http://www.telerik.com/products/aspnet-ajax/binaryimage.aspx
对我来说,有必要将“buffer =“false”添加到@Page。否则我会一直得到相同的图片......
For me it was necessary to add "buffer="false" to the @Page. Otherwise I would keep getting the same picture all the time...
处理程序可以像任何其他请求一样接受 url 参数。 因此,您不必将
链接到image.ashx
,而是将其设置为image.ashx?ImageID=[您的图像 ID在这里]
。A handler can accept a url parameter like any other request. So instead of linking your
<asp:image/>
toimage.ashx
you'd set it toimage.ashx?ImageID=[Your image ID here]
.我假设您需要从 asp.net 生成动态图像
你可能很幸运
http://www.codeplex.com/aspnet/Release/ProjectReleases。 aspx?ReleaseId=16449
Hanselman 最近在博客上介绍了它
I am assuming you need to generate dynamic images from asp.net
You might be in luck
http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=16449
Hanselman blogged about it recently
http://www.hanselman.com/blog/ASPNETFuturesGeneratingDynamicImagesWithHttpHandlersGetsEasier.aspx
虽然无法将 MemoryStream 数据绑定到图像,但可以使用 Label/GenericControl、一些代码和 数据 URI 方案 在页面中嵌入图像,但该方法存在严重问题:
更好的方法是使用单独的“Image.aspx”页面来获取并输出 MemoryStream,有点像我在开始学习 ASP.net 时创建的相册软件中所做的那样:
(别笑,那是我的第一次尝试 ASP.net :-)
编辑:同意 ASHX,上面的代码只是为了展示一个示例实现。 当我更新相册时,它将使用 ASHX。
While Databinding a MemoryStream to a Image is not possible, it could be possible to use a Label/GenericControl, some Code and the data URI scheme to embed Images in Pages, but there are severe issues with that approach:
The better Approach is to use a separate "Image.aspx" Page which takes and outputs your MemoryStream, kinda like I did in my Photo Album software that i've created when I started learning ASP.net:
(Don't laugh, that was my first attempt at ASP.net :-)
Edit: Agreed on ASHX, the code above is just to show one sample implementation. When I come around to update the Photo Album, it will use ASHX for that.
@Will 和 Ben Griswald:使用“image.ashx”代替“image.aspx”。
它比完整的 ASP.Net 页面更轻量,并且专门设计用于处理除 text/html 之外的内容类型。
@Will and Ben Griswald: instead of "image.aspx" use "image.ashx".
It's more light-weight than a full ASP.Net Page, and it's specifically designed to handle content-types other than text/html.
最好的办法是创建一个返回图像的 HttpHandler。 然后将 asp:Image 上的 ImageUrl 属性绑定到 HttpHandler 的 url。
这是一些代码。
首先创建 HttpHandler:
接下来,只需在使用 asp:Image 的 aspx 页面中调用它即可。
就是这样。
Best bet is to create an HttpHandler that would return the image. Then bind the ImageUrl property on the asp:Image to the url of the HttpHandler.
Here is some code.
First create the HttpHandler:
Next, just call it inside your aspx page where you are using the asp:Image.
And that is it.