This article transferred from: http://www.cnblogs.com/mayt/archive/2010/05/20/1740358.html
The first is to create a class that inherits from ActionResult, remembering to refer to the SYSTEM.WEB.MVC namespace as follows: Public classImageresult:actionresult { PublicImageFormat ContentType {Get;Set; } PublicImage Image {Get;Set; } Public stringSourceName {Get;Set; } PublicImageresult (string_sourcename, ImageFormat _contenttype) { This. SourceName =_sourcename; This. ContentType =_contenttype; } PublicImageresult (Image _imagebytes, ImageFormat _contenttype) { This. ContentType =_contenttype; This. Image =_imagebytes; } Public Override voidExecuteresult (ControllerContext context) {context. HttpContext.Response.Clear (); Context. HttpContext.Response.Cache.SetCacheability (Httpcacheability.nocache); if(Contenttype.equals (imageformat.bmp)) context. HttpContext.Response.ContentType ="image/bmp"; if(Contenttype.equals (Imageformat.gif)) context. HttpContext.Response.ContentType ="Image/gif"; if(Contenttype.equals (Imageformat.icon)) context. HttpContext.Response.ContentType ="Image/vnd.microsoft.icon"; if(Contenttype.equals (IMAGEFORMAT.JPEG)) context. HttpContext.Response.ContentType ="Image/jpeg"; if(Contenttype.equals (Imageformat.png)) context. HttpContext.Response.ContentType ="Image/png"; if(Contenttype.equals (Imageformat.tiff)) context. HttpContext.Response.ContentType ="Image/tiff"; if(Contenttype.equals (IMAGEFORMAT.WMF)) context. HttpContext.Response.ContentType ="image/wmf"; if(Image! =NULL) {image. Save (context. HttpContext.Response.OutputStream, ContentType); } Else{context. HttpContext.Response.TransmitFile (SourceName); }}} and then create an action in the Controller class as follows: PublicActionResult Getpicture (intID) {icategory server=NewCategoryserver (); byte[] buffer =server.getcategorypicture (ID); if(Buffer! =NULL) {MemoryStream stream=NewMemoryStream (buffer); System.Drawing.Image Image=System.Drawing.Image.FromStream (stream); Imageresult result=NewImageresult (image, System.Drawing.Imaging.ImageFormat.Jpeg); returnresult; } returnView (); This will allow you to display the picture. Here are a few ways to show the existing picture method one:usingSystem.IO; PublicFileresult Image () {stringPath = Server.MapPath ("/content/images/decorative/"); stringfilename = request.url.segments[request.url.segments.length-1]. ToString (); //Uss Path.Combine from System.IO instead of StringBuilder. stringFullPath =path.combine (Path, filename); return(NewFileresult (FullPath,"Image/jpeg"));} Method Two: PublicActionResult Image (stringID) { vardir = Server.MapPath ("/images"); varPath = Path.Combine (dir, id +". jpg"); return Base. File (Path,"image/jpg");} Method Three: [Acceptverbs (Httpverbs.get)][outputcache (CacheProfile="customerimages")] PublicFileresult Show (intCustomerId,stringimageName) { varPath =string. Concat (Configdata.imagesdirectory, CustomerId,@"\", ImageName); return NewFilestreamresult (NewFileStream (Path, FileMode.Open),"Image/jpeg");} All three can display images that already exist and I think the third method can be modified to read the picture display from the database.