First, create a class that inherits from ActionResult. Remember to reference the System. Web. Mvc namespace as follows:
Copy codeThe Code is as follows:
Public class ImageResult: ActionResult
{
Public ImageFormat ContentType {get; set ;}
Public Image 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 as follows:
Copy codeThe Code is as follows:
Public ActionResult GetPicture (int id)
{
ICategory server = new CategoryServer ();
Byte [] buffer = server. getCategoryPicture (id );
If (buffer! = Null)
{
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;
}
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.