1. Create the imageresult class in the models file and inherit from actionresult
Public class imageresult: actionresult
{
Private string _ path;
Public imageresult (string path)
{
_ Path = path;
}
Public override void executeresult (controllercontext context)
{
Byte [] bytes;
// No context? Stop Processing
If (context = NULL)
Throw new argumentnullexception ("context ");
// Check for file
If (file. exists (_ path) {bytes = file. readallbytes (_ path );}
Else
{
Throw new filenotfoundexception (_ path );
}
String contenttype = getcontenttypefromfile ();
Httpresponsebase response = context. httpcontext. response;
Response. contenttype = contenttype;
Memorystream imagestream = new memorystream (bytes );
Byte [] buffer = new byte [4096];
While (true)
{
Int READ = imagestream. Read (buffer, 0, buffer. Length );
If (read = 0)
Break;
Response. outputstream. Write (buffer, 0, read );
}
Response. End ();
}
Private string getcontenttypefromfile ()
{
// Get Extension from path to determine contenttype
String [] parts = _ path. Split ('.');
String extension = path. getextension (_ path). substring (1 );
String contenttype;
Switch (extension. tolower ())
{
Case "Jpeg ":
Case "jpg ":
Contenttype = "image/JPEG ";
Break;
Case "GIF ":
Contenttype = "image/GIF ";
Break;
Default:
Throw new notimplementedexception (extension + "not handled ");
}
Return contenttype;
}
}
2. Add the action in the homecontroller. CS file, named getimage, and return imageresult.
Public imageresult getimage ()
{
Return new imageresult (@ "C: \ Users \ public \ pictures \ Sample Pictures \ tuyin.jpg ");
}
Finally, access http: // localhost: 13811/home/getimage to display the image.
Further specify the image size display
1. Create an imageresizeresult class in the models folder.
Public class imageresizeresult: actionresult
{
Private string _ path;
Private int _ width;
Private int _ maximumheight;
Private bool _ nozooming;
Public imageresizeresult (string filename, int width, int maximumheight, bool nozooming = false)
{
// Put this path reference in a config file somewhere!
_ Path = @ "C: \ Users \ public \ pictures \ Sample Pictures \" + filename;
_ Width = width;
_ Maximumheight = maximumheight;
_ Nozooming = nozooming;
}
Private imageformat getimageformatfromfile ()
{
// Get Extension from path to determine contenttype
String [] parts = _ path. Split ('.');
String extension = parts [parts. Length-1];
Imageformat;
Switch (extension. tolower ())
{
Case "Jpeg ":
Case "jpg ":
Imageformat = imageformat. JPEG;
Break;
Case "GIF ":
Imageformat = imageformat. gif;
Break;
Default:
Throw new notimplementedexception (extension + "not handled ");
}
Return imageformat;
}
Public Image resizeimage (string imagepathtoresize)
{
Image fullsizeimage;
// Check for file
If (file. exists (_ path ))
{
Fullsizeimage = image. fromfile (_ path );
}
Else
{
Throw new filenotfoundexception (_ path );
}
// Load the image from the file system
Fullsizeimage = image. fromfile (_ path );
// Hack to prevent the internal thumbnail from being used!
Fullsizeimage. rotateflip (rotatefliptype. rotate180flipnone );
Fullsizeimage. rotateflip (rotatefliptype. rotate180flipnone );
// Can we zoom this image?
If (_ nozooming)
{
If (fullsizeimage. width <= _ width)
{
_ Width = fullsizeimage. width;
}
}
// Determine new height
Int newheight = fullsizeimage. Height * _ width/fullsizeimage. width;
If (newheight> _ maximumheight)
{
// Resize with height instead
_ Width = fullsizeimage. Width * _ maximumheight/
Fullsizeimage. height;
Newheight = _ maximumheight;
}
Image newimage = fullsizeimage. getthumbnailimage (_ width, newheight, null, intptr. Zero );
// Dispose of the In Memory Original
Fullsizeimage. Dispose ();
Return newimage;
}
Public override void executeresult (controllercontext context)
{
Byte [] buffer = new byte [4096];
Httpresponsebase response = context. httpcontext. response;
// No context? Stop Processing
If (context = NULL)
Throw new argumentnullexception ("context ");
// Set files content type
Response. contenttype = "image/" +
Getimageformatfromfile (). tostring ();
// Get the resized Image
Image resizedimage = resizeimage (_ path );
Memorystream MS = new memorystream ();
Resizedimage. Save (MS, getimageformatfromfiles ());
Memorystream imagestream = new memorystream (Ms. toarray ());
While (true)
{
Int READ = imagestream. Read (buffer, 0, buffer. Length );
If (read = 0)
Break;
Response. outputstream. Write (buffer, 0, read );
}
Response. End ();
Ms. Dispose ();
Imagestream. Dispose ();
}
}
2. Add the action in the homecontroller. CS file, named getimage, and return imageresult.
Public imageresizeresult getimage (string image, int width, int height, bool nozooming = false)
{
Return new imageresizeresult (image, width, height, nozooming );
}
3. Add a reference to the image in index. aspx in the home folder.
<P>
</P>
Finally, access http: // localhost: 13811/home/to see the effect.