An image can be written to the Response. This allows the image to be displayed only in certain cases. We use an ASPX page or an ASHX handler to display it. We write images in ASP.NET in a simple and reliable way in the C# language.


First, when you use the Response.Write method in ASP.NET, it replaces the actual web forms in your ASPX page. You need to add an image to the root of your web site project in Visual Studio. Use Website and then Add Existing Item. In the dialog, browse to your image and click Add. It appears in the right pane called Solution Explorer.
Let's examine the code-behind for writing an image to the output in ASP.NET. The hardest part here involves telling the web page where your image is located. You have to use a method called MapPath. Alternatively, you could use PhysicalApplicationPath.
This C# example outputs a binary image in ASP.NET. It uses the Response.BinaryWrite method.
Page that writes image [C#]
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string loc = Server.MapPath("~/Name.png");
Response.WriteFile(loc);
Response.ContentType = "image/png";
}
}Description. The above code is from default.aspx.cs. Most of the lines should be there already. It uses Server.MapPath. Server is an intrinsic object on the Page class, and it has a method called MapPath.

Path usage. It uses a virtual path. The string "~/Name.png" indicates a file called Name.png at the root of your application. It is processed by ASP.NET to get the physical path. Response.WriteFile is used. This method writes the contents of the file Name.png to the output. It writes the raw binary of the file.
Type of content. It sets ContentType. This property tells the browser what the type of the binary is. This part is not actually needed as browsers will infer the correct image type.
Set ContentTypeTesting the project. Navigate to the Default.aspx page and your image should appear. The entire contents of Default.aspx are replaced with the image written to the Response.

Your binary file will have a content type. It is best to usually specify this. If you do not, browsers will sometimes not be able to know what the binary is. The content types can easily be set in ASP.NET. Simply use the Response.ContentType property and assign it to one of the strings.
Content types image/png image/jpeg image/gif text/css text/html text/plain text/xml application/zip application/xhtml+xml application/x-shockwave-flash

Here we saw how you can render image data to the response buffer in an ASP.NET web application. Use this kind of code for writing images to output. This code can be improved to eliminate bandwidth leeching as well. It works well and is fast in my experience. You can combine this technique with web handler files using the ASHX extension for even simpler code.
ASHX Handler Tutorial ASP.NET Tutorials