Click here to Skip to main content
15,908,776 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have save my Image in SQL SERVER in byte form and i want to show it on label in asp.net c#.
I have tried alot but unable to do.
I m beginer.
So please suggest
Posted
Updated 5-Jun-16 19:01pm
Comments
Naresh Pansuriya 6-Dec-13 23:33pm    
First of all you can set image into label directly. So better you need set image with css by baclground-image attribute
[no name] 19-Mar-14 6:11am    
Just Handler.ashx file and Put response write in image format then it will display in aspx page

Use the class System.IO.MemoryStream: construct the instance of this class using your byte[] data as a constructor parameter and then read the image from this string:
http://msdn.microsoft.com/en-us/library/system.io.memorystream%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/e55f3s5k(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/93z9ee4x%28v=vs.110%29.aspx[^].

—SA
 
Share this answer
 
hi. i used for each only.. i posted full coding. you can see what is the problem

private void bindingroodrecord()

{

sql = "EXEC [sp_parent_record]"; //stored procedur to get parent records

SqlCommand cmd = new SqlCommand(sql, objConn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt1 = new DataTable();
da.Fill(dt1);
Session["parentnode"] = dt1;

PopulateNodes(dt1, TreeView1.Nodes); ///////// for parent node

}

private void PopulateNodes(DataTable dt, TreeNodeCollection nodes)
{
foreach (DataRow dr in dt.Rows)
{
TreeNode tn = new TreeNode();

tn.Text = dr["columnfield"].ToString().Trim();

tn.Value = dr["code_webcategory_id"].ToString().Trim();
tn.ImageUrl = string.Format("image.aspx?category={0}&width=50", tn.Value);
nodes.Add(tn);


tn.PopulateOnDemand = (Convert.ToInt32(dr["childnodecount"]) > 0);

}


}
The above coding works fine.

NOTE: image.aspx, image comes from database(which comes as byte) and then writting as image using System.Drawing.Image bitmap = System.Drawing.Image.FromStream(stream),......the image comes from database and binds in tn.imageurl correctly.

so, right now as i have 100 records it binds image 100 time which is ok.

now the below coding to bind child notes

protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{


PopulateSubLevel(Convert.ToInt32(e.Node.Value), e.Node); // i am getting parent node when user click (populate)on treeview to form a child node for parent node.


}

private void PopulateSubLevel(int parentid, TreeNode parentNode)
{

SqlCommand cmd = new SqlCommand("sp_getchildrecords", objConn);/// this is the stored procedure to get child node using parent id.
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@parentID", SqlDbType.Int).Value = parentid;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
Session["childnode"] = dt;
Session["childnodess"] = parentNode;
TreeNode childrecords = (TreeNode)Session["childnodess"];

PopulateNodes(dt, childrecords.ChildNodes); // to form child nodes


}
this also works fine. but,

Now, consider the child node has 10 records. once the populateNodes function gets over , it calls image.aspx to get an image. Now it binds 110 time again instead of binding 10 times.
 
Share this answer
 
C# Image to Byte Array and Byte Array to Image Converter Class[^]
public Image byteArrayToImage(byte[] byteArrayIn)
{
     MemoryStream ms = new MemoryStream(byteArrayIn);
     Image returnImage = Image.FromStream(ms);
     return returnImage;
}


-KR
 
Share this answer
 
Here you can use with database also remove first two lines and put your file there and convert to byte


C#
protected void btnUpload_Click(object sender, EventArgs e)
{
    System.IO.Stream fs = FileUpload1.PostedFile.InputStream;
    System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
    Byte[] bytes = br.ReadBytes((Int32)fs.Length);
    string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
    Image1.ImageUrl = "data:image/png;base64," + base64String;
    Image1.Visible = true;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900