Asp.Net MVC

ActionResultを継承して、イメージストリームを返すActionResultを例として、説明する

using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Drawing;
using System.Drawing.Imaging;

public class ImageResult : ActionResult
{
    public ImageResult() { }

    public Image Image { get; set; }
    public ImageFormat ImageFormat { get; set; }

    private readonly Dictionary<ImageFormat, string> ContentTypes = 
        new Dictionary<ImageFormat, string>()
    {
        {ImageFormat.Bmp , "image/bmp"},
        {ImageFormat.Gif , "image/gif"},
        {ImageFormat.Icon, "image/vnd.microsoft.icon"},
        {ImageFormat.Jpeg, "image/jpeg"},
        {ImageFormat.Png , "image/png"},
        {ImageFormat.Tiff, "image/tiff"},
        {ImageFormat.Wmf , "image/wmf"},
    };

    public override void ExecuteResult(ControllerContext context)
    {
        // verify properties 
        if (this.Image == null)
        {
            throw new ArgumentNullException("Image");
        }
        if (this.ImageFormat == null)
        {
            throw new ArgumentNullException("ImageFormat");
        }

        // output 
        var res = context.HttpContext.Response;
        res.Clear();
        res.ContentType = this.ContentTypes[this.ImageFormat];
        this.Image.Save(res.OutputStream, this.ImageFormat);
    }
}


フレッツ光が月額556円~【GMOとくとくBB】

コメント:



(画像の文字列を入力して下さい)

トップ   編集 凍結 差分 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2019/12/02 (月) 12:32:13 (1628d)

PCpዾyǗlgĂ܂}WŔ܂z 萔O~ył񂫁z Yahoo yV NTT-X Store

z[y[W ̃NWbgJ[h COiq ӂ邳Ɣ[ COsیI COze