Introduction

Recently there are so many people asking in forums how to preview the uploaded image, before saving it to the database. In this article, let us see how to preview the uploaded image in ASP.NET before saving it to the database. Also this will explain how to feed a byte array into an image control. We are going to do that using IhttpHandler.

Using the code

1. Create a new website. Add a File Upload control, a button and an image control.

Aspx code

[code]

<table>

        <tr>

            <td class="style3">

                <asp:Label ID="Label1" runat="server" Text="Photo upload" />

            </td>

            <td class="style4">

                <asp:FileUpload runat="server" ID="PhotoUpload" />

            </td>

            <td class="style4">

                <asp:Button runat="server" ID="btnPhotoPreview" Text="Preview" />

            </td>

            <td class="style1">

                <asp:Image runat="server" ID="ImagePreview" Height="164px" Width="125px" />

            </td>

        </tr>

    </table>

 

[/code]

2. Now add a button click event for the "btnPhotopreview":

[code]
<asp:Button runat="server" OnClick="btnPreview_Click" ID="btnPhotoPreview" Text="Preview" />
[/code]

3. We have to add a handler class to show the image. We will a session variable for FileBytes for the upload control. In the new handler class, get the session variable and generate the image.

AspImgCntrl1.jpg

When we add the .ashx file, it will have some code inside it:

[code]
<%@ WebHandler Language="C#" Class="ImageHandler" %>
 
using System;
using System.Web;
 
public class ImageHandler : IHttpHandler {
   
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        context.Response.Write("Hello World");
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }
 
}

[Code]

<%@ WebHandler Language="C#" Class="ImageHandler" %>

 

using System;

using System.Web;

 

public class ImageHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState

{

   

    public void ProcessRequest (HttpContext context) {

       //Checking whether the imagebytes session variable have anything else not doing anything

 

        if ((context.Session["ImageBytes"]) != null)

        {

            byte[] image = (byte[])(context.Session["ImageBytes"]);

            context.Response.ContentType = "image/JPEG";

            context.Response.BinaryWrite(image);

        }

    }

 

    public bool IsReusable {

        get {

            return false;
        }
    }
 
}
 
[/Code]

4. Now inside the button click in our page, get the filebytes from the FileUpload control and save it in a session variable:

[code]

protected void btnPreview_Click(object sender, EventArgs e)

    {

        Session["ImageBytes"] = PhotoUpload.FileBytes;

        ImagePreview.ImageUrl = "~/ImageHandler.ashx";

    }

[/code]

Now run the application and browse the image and preview it:

AspImgCntrl2.jpg

I have attached the complete source code also.
 

Next Recommended Readings