How to Convert bytes to image and save in specified path?
In this article I am going to explain about how to convert bytes into Image in ASP.NET. I have stored images bytes in database now I want to read that bytes convert in to images and store.
Description :
In this article I have explained in detail how to convert bytes to images in ASP.NET in simple method. It can be used for your project many times. Initially I have store student image bytes in SQL server student table image field, then get back that bytes and convert into image. Example
Create table like below
create table student (sno int,simage image)
Client Side
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Convert Bytes to Image</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<table width="500" align="center">
<tr>
<td colspan="2">
<b>File upload to Database</b>
</td>
</tr>
<tr>
<td colspan="2" height="30" align="center">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td height="30">
Enter Student No
</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td height="30">
Select Image File
</td>
<td>
<asp:FileUpload ID="FileUpload1" runat="server" />
</td>
</tr>
<tr>
<td colspan="2" height="30" align="center">
<asp:Button ID="Button1" runat="server" Text="Upload to Server" OnClick="Button1_Click" />
<br />
<asp:Button ID="Button2" runat="server" Text="Read from Server and save" OnClick="Button2_Click" />
<br />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>Code Behind
sing System.Data;
using System.Data.SqlClient;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Con"].ConnectionString);
SqlCommand sqlcmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
Stream fs;
fs = FileUpload1.PostedFile.InputStream;
BinaryReader br1 = new BinaryReader(fs);
byte[] picture = br1.ReadBytes(FileUpload1.PostedFile.ContentLength);
sqlcon.Open();
SqlCommand sqlcmd = new SqlCommand("insert into student(sno,simage) values (@sno, @simage)", sqlcon);
sqlcmd.Parameters.Add("@sno", SqlDbType.Int).Value = TextBox1.Text;
sqlcmd.Parameters.Add("@simage", SqlDbType.Image).Value = picture;
sqlcmd.ExecuteNonQuery();
sqlcon.Close();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
sqlcmd = new SqlCommand("Select simage from student", sqlcon);
sqlcmd.CommandType = System.Data.CommandType.Text;
sqlcon.Open();
da = new SqlDataAdapter(sqlcmd);
da.Fill(dt);
byte[] b;
for(int i=0;i<=dt.Rows.Count-1;i++)
{
System.Drawing.Image newImage;
b = ((byte[])dt.Rows[i][0]);
MemoryStream ms = new MemoryStream(b);
newImage = System.Drawing.Image.FromStream(ms);
//Mention path here where the image to save
//mention here name or if you store name in that another field then just use it here that field value
newImage.Save("D:\\testfldrname\\test" + i + ".jpg");
}
}
}Output
Source code:
Client Side: ASP.NET
Code Behind: C#Conclusion
I hope this article is help you to know about Convert / Read Bytes to save as image in specified location