Congratulations to all monthly winners of May 2013 !!! They have won INR 2900 cash and INR 27497 worth prize.
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 21503 |  Welcome, Guest!   Register  Login
Home > Articles > ASP.NET > Previewing image in ASP.NET image control

Previewing image in ASP.NET image control

Article posted by San.Pblr.Gct on 7/25/2012 | Views: 5635 | Category: ASP.NET | Level: Intermediate | Points: 250 red flag

Advertisements

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

Download


 Download source code for Previewing image in ASP.NET image control


Introduction

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

Using the code

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

Aspx 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>

2.      Now add a button click event for the “btnPhotopreview”.


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

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

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

<%@ 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;

        }

    }

 

}


Just rewrite the code inside processrequest to get session variable and generate image.

Inorder to use session variable in httphandler class we also implement our class from IRequiresSessionState.

<%@ 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;

        }

    }

 

}

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

protected void btnPreview_Click(object sender, EventArgs e)

    {

        Session["ImageBytes"] = PhotoUpload.FileBytes;

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

    } 

Now run the  application and browse image and preview it.

I have attached complete source code also.

Advertisements

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Experience:3 year(s)
Home page:http://www.dotnetfunda.com
Member since:Friday, April 06, 2012
Level:Starter
Status: [Member]
Biography:I am working as a developer in EF. I have been working in c#, sql, Silverlight,WPF and ASP.NET for last 3 years
 Responses
Posted by: Hareesh | Posted on: 02 Aug 2012 06:23:13 AM | Points: 25

hi santhosh iam trying these, but iam not getting output

Thanks
Hareesh.A

Posted by: San.Pblr.Gct | Posted on: 02 Aug 2012 06:24:38 AM | Points: 25

Have you used the source code? Which version of dotnet framework you are using?

>> Write Response - Respond to this post and get points
Related Posts

I am going to explain about the reading data from xml and filtering particular element and binding into Grid view using ASP.NET ,C#.

In this article, i have tried to explain the ASP.NET 4.0 Routing, with few examples.

Here an image is displayed in each row of a GridView. And the most interesting point is these images are clickable. If one clicks on an image, he/she gets navigated to another page.

You can download and upload word document using this lines of code.

Validation is one of the most important parts in any software project. Building flexible business validation is every one’s dream. Rather than writing frameworks from scratch to do these things, Microsoft validation blocks makes it a breeze. In this article we will discuss how validation application blocks help us to build flexible validations using validation application blocks. Its just a simple sixteen step process to put our business validation in action using validation blocks.

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 6/19/2013 1:39:03 AM