Click here to Skip to main content
15,891,943 members
Articles / Programming Languages / Visual Basic
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
7 Aug 2012CPOL1 min read 31.4K   1.1K   4   2
This is an alternative for "Previewing Image in ASP.NET Image Control"

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

Introduction

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:

    XML
    <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>
  3. Now add a button click event for btnPhotopreview.
    ASP.NET
    <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. 
    VB
    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"
                context.Response.BinaryWrite(image)
            End If
    
        End Sub
    
        ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
            Get
                Return False
            End Get
        End Property
    
    End Class
  5. Now in the button click pass the byte[] in session variable.
VB.NET
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.

License

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


Written By
Technical Lead EF (Education First)
India India
I graduated as Production Engineer and started my career as Software Developer then worked as tester for a while before moving into Windows application development using Microsoft Technologies. But for the last few years i am working on javascript, React, Node, AWS, Azure Chatbots

Comments and Discussions

 
GeneralMy vote of 5 Pin
Christian Amado7-Aug-12 7:17
professionalChristian Amado7-Aug-12 7:17 
GeneralRe: My vote of 5 Pin
dsr Murthy31-Jan-14 21:07
dsr Murthy31-Jan-14 21: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.