Click here to Skip to main content
Click here to Skip to main content

Tagged as

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 Education First
India India
Started my career with Infosys in 2009 and currently working with Education First. I have great passion towards Microsoft technologies. 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.

Comments and Discussions

GeneralMy vote of 5 PinmemberChristian Amado7-Aug-12 7:17 
GeneralRe: My vote of 5 Pinmemberdsr Murthy31-Jan-14 21:07 

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

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

| Advertise | Privacy | Mobile
Web04 | 2.8.141015.1 | Last Updated 7 Aug 2012
Article Copyright 2012 by Santhosh Kumar Jayaraman
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid