Public Class ImageResult
Inherits ActionResult
End Class
public class ImageResult: ActionResult
{ }
Private _ImageStream As Stream
Private _ContentType As String = ""
Public Property ImageStream() As Stream
Get
Return _ImageStream
End Get
Set ( ByVal value As Stream)
_ImageStream = value
End Set
End Property
Public Property ContentType() As String
Get
Return _ContentType
End Get
Set ( ByVal value As String )
_ContentType = value
End Set
End Property
Public Sub New( ByVal imageStream As Stream, ByVal contentType As String )
If imageStream Is Nothing Then Throw New ArgumentNullException("imageStream")
If String .IsNullOrEmpty(contentType) Then Throw New ArgumentNullException("contentType")
_ImageStream = imageStream
_ContentType = contentType
End Sub
private static Stream _ImageStream;
private static string _ContentType = "";
public static Stream ImageStream
{
get { return _ImageStream; }
set { _ImageStream = value; }
}
public static string ContentType
{
get { return _ContentType; }
set { _ContentType = value; }
}
public ImageResult(Stream imageStream, string contentType)
{
if (imageStream == null) throw new ArgumentNullException("imageStream");
if ( String .IsNullOrEmpty(contentType)) throw new ArgumentNullException("contentType");
_ImageStream = imageStream;
_ContentType = contentType;
}
Public Overrides Sub ExecuteResult( ByVal context As System.Web.Mvc.ControllerContext)
If context Is Nothing Then Throw New ArgumentNullException("context")
Dim Response As HttpResponseBase = context.HttpContext.Response
Response.ContentType = _ContentType
Dim buffer(1024) As Byte
Do
Dim read As Integer = _ImageStream.Read(buffer, 0, buffer.Length)
If read = 0 Then Exit Do
Response.OutputStream.Write(buffer, 0, read)
Loop
Response.End()
End Sub
public static override void ExecuteResult(System.Web.Mvc.ControllerContext context)
{
if (context == null) throw new ArgumentNullException("context");
HttpResponseBase Response = context.HttpContext().Response;
Response.ContentType = _ContentType;
byte [] buffer = new byte [1024];
do
{
int read = _ImageStream.Read(buffer, 0, buffer.Length);
if (read == 0) { break ; }
Response.OutputStream.Write(buffer, 0, read);
} while ( true );
Response.End();
}
Return New ImageResult(System.IO.File.OpenRead("C:\kbyte.ru.gif"), "image/gif")
return new ImageResult(System.IO.File.OpenRead("C:\\kbyte.ru.gif"), "image/gif");
Public Module ControllerExtensions
<Extension()> _
Public Function Image( ByVal controller As System.Web.Mvc.Controller, ByVal imageStream As Stream, ByVal contentType As String ) As ImageResult
Return New ImageResult(imageStream, contentType)
End Function
<Extension()> _
Public Function Image( ByVal controller As System.Web.Mvc.Controller, ByVal imageBytes() As Byte , ByVal contentType As String ) As ImageResult
Return New ImageResult( New MemoryStream(imageBytes), contentType)
End Function
<Extension()> _
Public Function Image( ByVal controller As System.Web.Mvc.Controller, ByVal fileName As String , ByVal contentType As String ) As ImageResult
Return New ImageResult(System.IO.File.OpenRead("C:\kbyte.ru.gif"), contentType)
End Function
End Module
public static class ControllerExtensions
{
[Extension()]
public static ImageResult Image(System.Web.Mvc.Controller controller, Stream imageStream, string contentType)
{
return new ImageResult(imageStream, contentType);
}
[Extension()]
public static ImageResult Image(System.Web.Mvc.Controller controller, byte [] imageBytes, string contentType)
{
return new ImageResult( new MemoryStream(imageBytes), contentType);
}
[Extension()]
public static ImageResult Image(System.Web.Mvc.Controller controller, string fileName, string contentType)
{
return new ImageResult(System.IO.File.OpenRead("C:\\kbyte.ru.gif"), contentType);
}
}
Return Image(System.IO.File.OpenRead("C:\kbyte.ru.gif"), "image/gif")
return Image(System.IO.File.OpenRead("C:\\kbyte.ru.gif"), "image/gif");
Source: https://habr.com/ru/post/63149/
All Articles