Subscribe to Subscribers
Talk to Webmaster Tony John


Resources » Code Snippets » ASP.NET WebForms

How to Convert bytes to image and save in specified path?


Posted Date:     Category: ASP.NET WebForms    
Author: Member Level: Diamond    Points: 60


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


ConvertBytes_Image


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

Attachments
  • ConvertBytes_Image (44057-03116-ConvertBytes-Image.rar)





  • Did you like this resource? Share it with your friends and show your love!


    Responses to "How to Convert bytes to image and save in specified path?"

    No responses found. Be the first to respond...

    Feedbacks      

    Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: What is ASP.NET State Management? How to manage state in ASP.NET?
    Previous Resource: How to read files from directory and insert into database as bytes?
    Return to Resources
    Post New Resource
    Category: ASP.NET WebForms


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    Read Bytes  .  Bytes to Image convert  .  



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.