将Byte []转换为图像并在asp.net中显示 [英] Convert Byte[] to image and show in asp.net

查看:50
本文介绍了将Byte []转换为图像并在asp.net中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经以字节形式在SQL SERVER中保存我的图像,我想在asp.net c#中的标签上显示它。

我已经尝试了很多但无法做到。

我是初学者。

所以请建议

解决方案

使用类 System.IO.MemoryStream :使用 byte [] 数据构造此类的实例作为构造函数参数,然后从该字符串中读取图像:

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


hi。我只用于每个..我发布了完整的编码。你可以看看有什么问题



private void bindingroodrecord()



{



sql =EXEC [sp_parent_record]; //存储过程以获取父记录



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); /////////为父节点



}



private void PopulateNodes(DataTable) dt,TreeNodeCollection节点)

{

foreach(dt.Rows中的DataRow dr)

{

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);



}





}

以上编码工作正常。



注意:image.aspx, image来自数据库(以字节形式出现),然后使用System.Drawing.Image bitmap = System.Drawing.Image.FromStream(stream)写入图像,......图像来自数据库并以tn绑定。图像正确。



所以,现在因为我有100条记录它绑定图像100次没关系。



现在下面的代码绑定子注释



protected void TreeView1_TreeNodePopulate(object sender,TreeNodeEventArgs e)

{





PopulateSubLevel(Convert.ToInt32(e.Node.Value),e.Node); //当用户在树视图上单击(填充)以形成父节点的子节点时,我将获得父节点。





} < br $>


private void PopulateSubLevel(int parentid,TreeNode parentNode)

{



SqlCommand cmd = new SqlCommand(sp_getchildrecords,objConn); ///这是使用父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) ; //形成子节点





}

这也可行。但是,



现在,考虑子节点有10条记录。一旦populateNodes函数结束,它就会调用image.aspx来获取图像。现在它再次绑定110次而不是绑定10次。


参考此链接

http://stackoverflow.com/questions/20771432/bind-an-image-in-c-sharp-using-byte-array [ ^ ]

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

解决方案

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


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.


refer this link
http://stackoverflow.com/questions/20771432/bind-an-image-in-c-sharp-using-byte-array[^]


这篇关于将Byte []转换为图像并在asp.net中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆