Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Asp.Net Image Upload and Preview without Saving in Folder using C#, VB.NET

Jun 13, 2016
Introduction

Here I will explain how to upload and show / display image without saving in
asp.net using c#, vb.net with example or image upload and preview without saving in folder in asp.net using c#, vb.net with example or display image in image control after upload in asp.net without saving to folder using c#, vb.net with example. By using binaryreader property in asp.net we can upload and display image without saving in folder using c#, vb.net.


To upload and display image in asp.net without saving in folder we to create new website in visual studio and open your aspx page and write the following code


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>ASP.Net Upload and Preview Image without saving in C#, VB.Net</title>
<style type="text/css">
#imgDetail
{
width:30%;
height:30%;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div style=" width:50%">
<asp:FileUpload ID="upload1" runat="server" />
<asp:Button ID="btnPreview" runat="server" Text="Upload & Preview" onclick="btnPreview_Click" />
<hr />
<asp:Image ID="imgDetail" Visible = "false" runat="server" />
</div>
</form>
</body>
</html>

After adding code in aspx page now open code behind file and add following namespaces

C# Code


using System;
using System.IO;

After completion of adding namespaces you need to write the code like as shown below


protected void btnPreview_Click(object sender, EventArgs e)
{
Stream strm = upload1.PostedFile.InputStream;
BinaryReader reader = new BinaryReader(strm);
Byte[] bytes = reader.ReadBytes(Convert.ToInt32(strm.Length));
imgDetail.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(bytes, 0, bytes.Length);
imgDetail.Visible = true;
}

VB.NET Code


Imports System.IO
Partial Class VBCode
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

End Sub
Protected Sub btnPreview_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim strm As Stream = upload1.PostedFile.InputStream
Dim reader As New BinaryReader(strm)
Dim bytes As [Byte]() = reader.ReadBytes(Convert.ToInt32(strm.Length))
imgDetail.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(bytes, 0, bytes.Length)
imgDetail.Visible = True
End Sub
End Class

Demo



If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

1 comments :

bob said...

Hi,
Good article.please add comments on the program.so that we know why we use that property.
Thanks and Regards,
Anand.

Give your Valuable Comments

Other Related Posts

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.