Click here to Skip to main content
12,821,674 members (26,430 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked
Posted 7 Aug 2012

Previewing Image in ASP.NET Image Control using VB.NET

, 7 Aug 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
This is an alternative for "Previewing Image in ASP.NET Image Control"

This is an alternative for "Previewing Image in ASP.NET Image Control"


I have already published a tip how to preview the image in ASP.NET image control using C# language. Recently many of them asked me how to achieve this in VB.Net. I asked them to use code converter tool but still many didn't get the appropriate code. So i decided to explain how to achieve the same using VB.NET in this alternative tip

Using the Code

  1. Create a new website. Add a FileUpload control, a button, and an image control.
  2. ASPX code:

            <td class="style3">
                <asp:Label ID="Label1" runat="server" Text="Photo upload" />
            <td class="style4">
                <asp:FileUpload runat="server" ID="PhotoUpload" />
            <td class="style4">
                <asp:Button runat="server" ID="btnPhotoPreview" Text="Preview" />
            <td class="style1">
                <asp:Image runat="server" ID="ImagePreview" Height="164px" Width="125px" />
  3. Now add a button click event for btnPhotopreview.
    <asp:Button runat="server" OnClick="btnPreview_Click" ID="btnPhotoPreview" Text="Preview" />
  4. We have to add a handler class in order to show the image. We are going to pass the session variable for FileBytes for the upload control. In the new handler class, get the session variable and generate the image and the main difference between C# and vb comes here in this part in handling session variables. 
    Imports System.Web
    Imports System.Web.Services
    Public Class Handler1
        Implements System.Web.IHttpHandler, System.Web.SessionState.IRequiresSessionState
        Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
            If (context.Session("ImageBytes")) IsNot Nothing Then
                Dim image As Byte() = DirectCast(context.Session("ImageBytes"), Byte())
                context.Response.ContentType = "image/JPEG"
            End If
        End Sub
        ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
                Return False
            End Get
        End Property
    End Class
  5. Now in the button click pass the byte[] in session variable.
Protected Sub btnPhotoPreview_Click(sender As Object, e As EventArgs) Handles btnPhotoPreview.Click

        Session("ImageBytes") = PhotoUpload.FileBytes
        ImagePreview.ImageUrl = "~/Handler1.ashx"
    End Sub

I have attached the source code. It is written in Visual studio 2012, so the solution might not be work with few of your machines. But you can check the class files.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Santhosh Kumar Jayaraman
Software Developer Radiometer
India India
Currently i am trying to learn all about javascripts . I have experience in Microsoft technologies like WPF, WCF, ASPNET, WinForms,Silverlight, VB.NET, C-Sharp, Entity framework,SSRS, LINQ, Extension methods and SQL server. I started with my career in Infosys in the year 2009 and i worked with Trigent, Education First (EF) in the recent years before joining my current company Radiometer.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Christian Amado7-Aug-12 8:17
memberChristian Amado7-Aug-12 8:17 
GeneralRe: My vote of 5 Pin
dsr Murthy31-Jan-14 22:07
memberdsr Murthy31-Jan-14 22:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 7 Aug 2012
Article Copyright 2012 by Santhosh Kumar Jayaraman
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid