First, create a class that inherits from actionresult. Remember to reference the system. Web. MVC namespace as follows:
CopyCode The Code is as follows: public class imageresult: actionresult
{
Public imageformat contenttype {Get; set ;}
Public image {Get; set ;}
Public String sourcename {Get; set ;}
Public imageresult (string _ sourcename, imageformat _ contenttype)
{
This. sourcename = _ sourcename;
This. contenttype = _ contenttype;
}
Public imageresult (image _ imagebytes, imageformat _ contenttype)
{
This. contenttype = _ contenttype;
This. Image = _ imagebytes;
}
Public override void executeresult (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 );
}
}
}
Create an action in the Controller class. copy Code the code is as follows: public actionresult getpicture (int id)
{< br> icategory Server = new categoryserver ();
byte [] buffer = server. getcategorypicture (ID);
If (buffer! = NULL)
{< br> memorystream stream = new memorystream (buffer);
system. drawing. image image = system. drawing. image. fromstream (Stream);
imageresult result = new imageresult (image, system. drawing. imaging. imageformat. JPEG);
return result;
}< br> return view ();
}
In this way, the image is displayed.
The following methods can be used to display existing images:
Method 1:Copy codeThe Code is as follows: using system. IO;
Public fileresult image (){
String Path = server. mappath ("/content/images/decorative /");
String filename = request. url. segments [request. url. segments. Length-1]. tostring ();
// USS path. Combine from system. Io instead of stringbuilder.
String fullpath = path. Combine (path, filename );
Return (New fileresult (fullpath, "image/JPEG "));
}
Method 2:Copy codeThe Code is as follows: public actionresult image (string ID)
{
VaR dir = server. mappath ("/images ");
VaR Path = path. Combine (Dir, ID + ". jpg ");
Return base. File (path, "image/jpg ");
}
Method 3:Copy codeThe Code is as follows: [acceptverbs (httpverbs. Get)]
[Outputcache (cacheprofile = "mermerimages")]
Public fileresult show (INT customerid, string imagename)
{
VaR Path = string. Concat (configdata. imagesdirectory, customerid, @ "\", imagename );
Return new filestreamresult (New filestream (path, filemode. Open), "image/JPEG ");
}
These three methods can display existing images, and I think the third method can be changed to read the image display from the database.