首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

image的有关问题

2011-12-28 
image的问题SQL中的字段是image类型的如何用c#读出转换为picturebox能用的数据byte[]b(byte[])ds.Tables[

image的问题
SQL中的字段是image类型的
如何用c#读出   转换为picturebox能用的数据

byte[]   b   =   (byte[])ds.Tables[0].Rows[0][ "pic "];
MemoryStream   s   =   new   MemoryStream(b);
Bitmap   bmp   =   new   Bitmap(s);
System.Drawing.Image   image   =   bmp;
pbc.Image   =   image;
s.Close();

网上找到这个   Bitmap   bmp   =   new   Bitmap(s);//这报参数错误

请高手帮忙

[解决办法]
最好用ImageConverter来转换而不直接用MemoryStream,如下:


byte[] o=(byte[])ds.Tables[0].Rows[0][ "pic "];
ImageConverter imc = new ImageConverter();
Image _img = imc.ConvertFrom(null, System.Globalization.CultureInfo.CurrentCulture, o) as Image;
if (_img != null)
{
//转换成功!
}

[解决办法]
注:MyTools.g_PhotoField为数据库表中的图象字段名称
//将图片保存到数据库中
if(this.picPhoto.Image==null)
{
m_DataRow[MyTools.g_PhotoField]=DBNull.Value;
}
else
{
try
{
MemoryStream ms = new MemoryStream ();
picPhoto.Image.Save (ms, System.Drawing.Imaging.ImageFormat.Bmp);
byte [] myData = new Byte [ms.Length ];
ms.Position = 0;
ms.Read (myData,0,Convert.ToInt32 (ms.Length ));
m_DataRow[MyTools.g_PhotoField] = myData;

}
catch(System.Exception ee)
{
MessageBox.Show(ee.Message);
}
}//else

//读取图象
if(this.m_DataRow[MyTools.g_PhotoField]!=DBNull.Value)
{
try
{
Byte[] byteBLOBData = new Byte[0];
byteBLOBData = (Byte[])m_DataRow[MyTools.g_PhotoField];
MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
this.picPhoto.Image= Image.FromStream(stmBLOBData);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else
{
this.picPhoto.Image= null;
}

热点排行