[Turn]asp.net MVC to read pictures from the database

Source: Internet
Author: User
Tags httpcontext

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. 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.