现在的位置: 首页 > 综合 > 正文

在MVC项目中如何显示图片

2013年10月01日 ⁄ 综合 ⁄ 共 5658字 ⁄ 字号 评论关闭

首先,有好一阵没有怎么写博客文章了.实在也是很多事情,确实没有停下来过.

这两天在讲解MVC方面的知识和项目实践,其中有一个小的细节,是有关于图片显示方面的,记录下来供大家参考

在MVC项目中,要显示一个图片,尤其是该图片是存放在数据库的话,还是可以继续使用原先Web Forms的那种ashx的方式。但也可以考虑下面的方式

 

1.创建一个ImageResult

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

namespace Extensions
{
    public class ImageResult : ActionResult
    {
        public ImageResult() { }
        public Image Image { get; set; }
        public ImageFormat ImageFormat { get; set; }
        public override void ExecuteResult(ControllerContext context)
        {
            // verify properties 
            if (Image == null)
            {
                throw new ArgumentNullException("Image");
            }
            if (ImageFormat == null)
            {
                throw new ArgumentNullException("ImageFormat");
            }
            // output 
            context.HttpContext.Response.Clear();
            if (ImageFormat.Equals(ImageFormat.Bmp)) context.HttpContext.Response.ContentType = "image/bmp";
            if (ImageFormat.Equals(ImageFormat.Gif)) context.HttpContext.Response.ContentType = "image/gif";
            if (ImageFormat.Equals(ImageFormat.Icon)) context.HttpContext.Response.ContentType = "image/vnd.microsoft.icon";
            if (ImageFormat.Equals(ImageFormat.Jpeg)) context.HttpContext.Response.ContentType = "image/jpeg";
            if (ImageFormat.Equals(ImageFormat.Png)) context.HttpContext.Response.ContentType = "image/png";
            if (ImageFormat.Equals(ImageFormat.Tiff)) context.HttpContext.Response.ContentType = "image/tiff";
            if (ImageFormat.Equals(ImageFormat.Wmf)) context.HttpContext.Response.ContentType = "image/wmf";
            Image.Save(context.HttpContext.Response.OutputStream, ImageFormat);
        }
    }

}

2,创建一个Action

 

        private string connection =ConfigurationManager.ConnectionStrings["northwind"].ConnectionString;


        public ActionResult Image(int id)
        {
            var db = new NorthwindDataContext(connection);
            var found = db.Employees.FirstOrDefault(e => e.EmployeeID == id);


            if (found != null)
            {
                var buffer = found.Photo.ToArray();
                ImageConverter converter = new ImageConverter();
                var image = (Image)converter.ConvertFrom(buffer);
                return new Extensions.ImageResult()
                {
                    Image = image,
                    ImageFormat = System.Drawing.Imaging.ImageFormat.Jpeg
                };

            }
            else
            {
                ViewData["message"] = "员工不存在";
                return View("Error");
            }

        }

 

3.在页面(View)中调用

"" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

"Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Update
"Content2" ContentPlaceHolderID="MainContent" runat="server">

    

Update

using (Html.BeginForm()) {%> true) %>
Fields
"float:right"> "/Employee/Image/" alt="" />
class="editor-label"> model.EmployeeID) %>
class="editor-field"> model.EmployeeID) %> model.EmployeeID) %>
class="editor-label"> model.LastName) %>
class="editor-field"> model.LastName) %> model.LastName) %>
class="editor-label"> model.FirstName) %>
class="editor-field"> model.FirstName) %> model.FirstName) %>
class="editor-label"> model.Title) %>
class="editor-field"> model.Title) %> model.Title) %>
class="editor-label"> model.TitleOfCourtesy) %>
class="editor-field"> model.TitleOfCourtesy) %> model.TitleOfCourtesy) %>
class="editor-label"> model.BirthDate) %>
class="editor-field"> model.BirthDate, String.Format("{0:g}", Model.BirthDate)) %> model.BirthDate) %>
class="editor-label"> model.HireDate) %>
class="editor-field"> model.HireDate, String.Format("{0:g}", Model.HireDate)) %> model.HireDate) %>
class="editor-label"> model.Address) %>
class="editor-field"> model.Address) %> model.Address) %>
class="editor-label"> model.City) %>
class="editor-field"> model.City) %> model.City) %>
class="editor-label"> model.Region) %>
class="editor-field"> model.Region) %> model.Region) %>
class="editor-label"> model.PostalCode) %>
class="editor-field"> model.PostalCode) %> model.PostalCode) %>
class="editor-label"> model.Country) %>
class="editor-field"> model.Country) %> model.Country) %>
class="editor-label"> model.HomePhone) %>
class="editor-field"> model.HomePhone) %> model.HomePhone) %>
class="editor-label"> model.Extension) %>
class="editor-field"> model.Extension) %> model.Extension) %>
class="editor-label"> model.Notes) %>
class="editor-field"> model.Notes) %> model.Notes) %>
class="editor-label"> model.ReportsTo) %>
class="editor-field"> model.ReportsTo) %> model.ReportsTo) %>
class="editor-label"> model.PhotoPath) %>
class="editor-field"> model.PhotoPath) %> model.PhotoPath) %>

"submit" value="Save" />

"Back to List", "Index") %>
最后的结果如下,大家可以参考参考

image

抱歉!评论已关闭.