Here, I will explain how to upload and display images without saving them in a folder in ASP.NET using C# and VB.NET. By using binary reader property in ASP.NET, we can upload and display images.

To upload and display images in ASP.NET without saving in folder, we need to create a new website in Visual Studio. Open aspx page and write the following code,
  1. <html
  2. xmlns="http://www.w3.org/1999/xhtml">
  3. <head runat="server">
  4. <title>ASP.Net Upload and Preview Image without saving in C#, VB.Nettitle>
  5. <style type="text/css">
  6. #imgDetail
  7. {
  8. width: 30%;
  9. height: 30%;
  10. }
  11. <style>
  12. <head>
  13. <body>
  14. <form id="form1" runat="server">
  15. <div style=" width:50%">
  16. <asp:FileUpload ID="upload1" runat="server" />
  17. <asp:Button ID="btnPreview" runat="server" Text="Upload & Preview" onclick="btnPreview_Click" />
  18. <hr />
  19. <asp:Image ID="imgDetail" Visible="false" runat="server" />
  20. <div>
  21. </form>
  22. </body>
    </head>
    </style>
  23. </html>
After adding code in aspx page now open the code behind file and add the following namespaces.

C# Code
  1. using System;
  2. using System.IO;
After completion of adding namespaces you need to write the code as shown below,
  1. protected void btnPreview_Click(object sender, EventArgs e)
  2. {
  3. Stream strm = upload1.PostedFile.InputStream;
  4. BinaryReader reader = new BinaryReader(strm);
  5. Byte[] bytes = reader.ReadBytes(Convert.ToInt32(strm.Length));
  6. imgDetail.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(bytes, 0, bytes.Length);
  7. imgDetail.Visible = true;
  8. }
VB.NET Code
  1. Imports System.IO
  2. Partial Class VBCode
  3. Inherits System.Web.UI.Page
  4. Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
  5. End Sub
  6. Protected Sub btnPreview_Click(ByVal sender As Object, ByVal e As EventArgs)
  7. Dim strm As Stream = upload1.PostedFile.InputStream
  8. Dim reader As New BinaryReader(strm)
  9. Dim bytes As [Byte]() = reader.ReadBytes(Convert.ToInt32(strm.Length))
  10. imgDetail.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(bytes, 0, bytes.Length)
  11. imgDetail.Visible = True
  12. End Sub
  13. End Class
For Code Demo,

Demo