加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 使用具有不同扩展名的自定义httphandler的MVC 3

发布时间:2020-12-16 09:22:24 所属栏目:asp.Net 来源:网络整理
导读:你能在MVC中创建一个带自定义扩展的自定义httphandler吗? 我想要一个具有以下路径的图像处理程序(如果可能), domain.com/picture/{id of the picture} 是否有可能从MVC内部或我必须在IIS 7中做到这一点? 解决方法 无需添加httphandler.你应该通过控制器在a
你能在MVC中创建一个带自定义扩展的自定义httphandler吗?

我想要一个具有以下路径的图像处理程序(如果可能),
domain.com/picture/{id of the picture}

是否有可能从MVC内部或我必须在IIS 7中做到这一点?

解决方法

无需添加httphandler.你应该通过控制器在asp.net mvc中执行此操作
例:

public class PictureController : Controller
{
    public FileResult GetImage(int pictureID)
    {
        byte[] fileContents = null;
        //Get the file here.
        return File(fileContents,"image/jpeg");
    }
}

在global.asax中你可以定义

routes.MapRoute("Picture-GetImage","picture/{pictureID}",new { controller = "Picture",action = "GetImage" }

您也可以使用System.Web.Helpers.WebImage帮助程序,或手动执行,例如:

public static byte[] ProcessCropResizeImage(string imageurl,Size outputSize)
{
    if (File.Exists(imageurl))
    {
        MemoryStream result = new MemoryStream();
        ImageCodecInfo codec = ImageCodecInfo.GetImageEncoders().FirstOrDefault(m => m.MimeType == "image/jpeg");
        if (codec == null)
            throw new ArgumentException(string.Format("Unsupported mimeType specified for encoding ({0})","image/jpeg"),"encodingMimeType");
        EncoderParameters encoderParams = new EncoderParameters(1);
        encoderParams.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality,85L);
        using (FileStream fs = File.OpenRead(imageurl))
        {
            using (Image image = Image.FromStream(fs))
            {
                using (Bitmap b = new Bitmap(outputSize.Width,outputSize.Height))
                {
                    using (Graphics g = Graphics.FromImage((Image)b))
                    {
                        g.CompositingQuality = CompositingQuality.HighQuality;
                        g.SmoothingMode = SmoothingMode.HighQuality;
                        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        g.DrawImage(image,outputSize.Width,outputSize.Height);
                        g.DrawImage(image,outputSize.Height);
                    }
                    b.Save(result,codec,encoderParams);
                }
            }
        }

        return result.GetBuffer();
    }
    return null;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读