在MVC中显示图像文件
我有一个对象,其中包含:
- 图像本身(类型:{System.Drawing.Bitmap})
- 图像类型(类型:字符串,即“gif”/“jpeg”)
- 超链接(类型:字符串)
我想显示来自我的应用程序中存在此对象,当用户单击它时,它将转到同一对象中定义的超链接。
I have an object that contains:
- the image itself (type: {System.Drawing.Bitmap})
- image type (type: string, i.e. "gif" / "jpeg")
- hyperlink (type: string)
I want to display the image from this object in my application, and when a user will click on it, it will go to the hyperlink that is defined in the same object.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您处于控制页面的操作方法中时,您需要加载一次对象;当您处于返回图像本身的操作方法中时,您需要再次加载对象。
在页面请求的操作方法中,您将把超链接传递给视图(使用视图模型),并在视图中渲染图像标记,并在超链接周围添加 A 标记。 IMG 标签上的图像源需要指向一个将返回图像本身的操作方法。
如果您不需要在第二个请求之前将图像加载到位图中,那就更好了,因为将图像从一个请求传递到下一个请求会更困难(并且您不想将图像填充到会话状态中!)
请参阅 此问题了解有关如何返回图像的详细信息。
You will need to load your object once when you are in the action method that controls the page and again when you are in an action method that returns the image itself.
In the action method for the page request you will pass the hyperlink to the view (using a view model) and in the view you will render the image tag with an A tag around it for the hyperlink. The image source on the IMG tag will need to point to an action method that will return the image itself.
It would be better if you did not need to load the image into the bitmap until the second request as passing it from one request to the next is harder (and you don't want to stuff images into session state!)
See this question for details on how to return an image.