简体   繁体   English

返回jpeg给定参数的asp.net mvc控制器方法

[英]asp.net mvc controller method that returns jpeg given parameter

I would like to create a jpeg on the fly given some data from a database. 我想根据数据库中的一些数据即时创建一个jpeg。 The data is an array containing values which should be translated into a colour. 数据是一个数组,其中包含应转换为颜色的值。

A asp.net mvc controller method should return a jpeg on the fly given one parameter. 给定一个参数,asp.net mvc控制器方法应即时返回jpeg。

This should be fairly straight forward. 这应该是相当简单的。 Could someone please point me to some existing code? 有人可以指出一些现有代码吗?

Thanks. 谢谢。

Here are a couple of possible options that may help you get started: I think you will porbably need an handler and then call the handler from your controller. 这里有一些可能的选项可以帮助您入门:我认为您可能需要一个处理程序,然后从您的控制器调用该处理程序。

SO POst 这样
Bob Cravens post 鲍勃·克雷文斯(Bob Cravens)发布
Scott Hansleman's post 斯科特·汉斯勒曼(Scott Hansleman)的帖子

If you want this in pure mvc you can do this 如果您想在纯MVC中使用此功能,则可以执行此操作

Extending MVC: Returning an Image from a Controller Action 扩展MVC:从控制器操作返回图像

Another way is to create a HttpHandler that does that for you 另一种方法是创建一个为您执行此操作的HttpHandler

HTTP Handlers for Images in ASP.NET ASP.NET中图像的HTTP处理程序

hope this helps 希望这可以帮助

There is a tutorial on msdn on How to: Encode and Decode a JPEG Image . 在msdn上有一个有关如何:对JPEG图像进行编码和解码的教程。

Doing that in MVC3 is pretty similar, you just need a action in your controller like this: 在MVC3中执行此操作非常相似,您只需要在控制器中执行以下操作即可:

public class YourController : Controller
{
    [HttpGet]
    public ImageResult GetImage(int whatever)
    {
        stream imageStream = yourJpgFactory.GetImage(whatever)
        return (imageStream)
    }
}

and in your view 在你看来

 <img src="YourController/GetImage?whatever=42" /> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何调用返回任务的方法 <T> 从ASP.Net MVC4控制器方法? - How do I call a method that returns a Task<T> From an ASP.Net MVC4 Controller Method? 如何运行 ASP.NET MVC Web API Z594C103F2C6E04C3D18AB059F031E0 方法查询参数值? - How to run ASP.NET MVC Web API controller method by query parameter value? ASP.NET MVC参数绑定:如何将列表或数组发布到控制器方法? - ASP.NET MVC parameter binding: How to POST a list or array to a controller method? 是否可以在 ASP.NET MVC [FromBody] 控制器方法参数中使用多态? - Is it possible to use polymorphism in and ASP.NET MVC [FromBody] controller method parameter? 如何更改asp.net MVC控制器的日期格式为方法的参数..? - How to change asp.net mvc controller's date format for method's parameter..? controller中edit方法的transfer参数中的DateTime总是01.01.0001 00:00:00 asp.net mvc5 - DateTime in the transfer parameter of the edit method in the controller is always 01.01.0001 00:00:00 asp.net mvc5 从控制器asp.net mvc中的给定ID获取数据 - fetch data from the given id in the controller asp.net mvc ASP.NET MVC 4从另一个控制器调用一个控制器方法 - ASP.NET MVC 4 call a controller method from another controller ASP.NET MVC - 用于控制器的JSON.NET扩展方法 - ASP.NET MVC - JSON.NET extension method for controller ASP.NET MVC Wild Card Controller任何参数路由 - ASP.NET MVC Wild Card Controller any parameter routing
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM