In this article I will explain with an example, how to upload Image file and display in Image control in ASP.Net using C# and VB.Net.
First the Picture / Photo / Image file will be uploaded using FileUpload control and will be saved in a Folder (Directory) then using Relative Path, the Image file will be displayed in ASP.Net Image control using C# and VB.Net.
 
 
HTML Markup
The following HTML Markup consists of an ASP.Net FileUpload control, a Button and an Image control. The Button has been assigned a Click event handler.
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" Text="Upload" runat="server" OnClick="UploadFile" />
<hr />
<asp:Image ID="Image1" runat="server" Height = "100" Width = "100" />
 
 
Namespaces
You will need to import the following namespace.
C#
using System.IO;
 
VB.Net
Imports System.IO
 
 
Upload Image file and display in Image control in ASP.Net
When the Upload Button is clicked, first a check is performed whether the Folder (Directory) exists. If it does not then the Folder (Directory) is created.
Then the uploaded Image File is saved into the Folder (Directory).
Finally the uploaded Image is displayed in Image control using its Relative Path.
C#
protected void UploadFile(object sender, EventArgs e)
{
    string folderPath = Server.MapPath("~/Files/");
 
    //Check whether Directory (Folder) exists.
    if (!Directory.Exists(folderPath))
    {
        //If Directory (Folder) does not exists Create it.
        Directory.CreateDirectory(folderPath);
    }
 
    //Save the File to the Directory (Folder).
    FileUpload1.SaveAs(folderPath + Path.GetFileName(FileUpload1.FileName));
 
    //Display the Picture in Image control.
    Image1.ImageUrl = "~/Files/" + Path.GetFileName(FileUpload1.FileName);
}
 
VB.Net
Protected Sub UploadFile(sender As Object, e As EventArgs)
    Dim folderPath As String = Server.MapPath("~/Files/")
 
    'Check whether Directory (Folder) exists.
    If Not Directory.Exists(folderPath) Then
        'If Directory (Folder) does not exists Create it.
        Directory.CreateDirectory(folderPath)
    End If
 
    'Save the File to the Directory (Folder).
    FileUpload1.SaveAs(folderPath & Path.GetFileName(FileUpload1.FileName))
 
    'Display the Picture in Image control.
    Image1.ImageUrl = "~/Files/" & Path.GetFileName(FileUpload1.FileName)
End Sub
 
 
Screenshot
Upload Image file and display in Image control in ASP.Net using C# and VB.Net
 
 
Downloads