将图像添加到datatabale [英] Add image to datatabale
本文介绍了将图像添加到datatabale的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将图像添加到数据表中,但出现错误.下面是我完成的代码.
值类型与列类型不匹配.无法存储< system.byte []>在徽标名列中.预期的类型为Byte [].
我在此行得到的错误 dt.Rows.Add(imageDataSet.Tables [0] .Rows [rowNumber] [0] .ToString());
i want to add image to datatble but getting an error.below is code i have done.
Type of value has a mismatch with column typeCouldn''t store <system.byte[]> in logoname Column. Expected type is Byte[].
this error i got at this line dt.Rows.Add(imageDataSet.Tables[0].Rows[rowNumber][0].ToString());
DataTable dt = new DataTable();
dt.Columns.Add("logoname", System.Type.GetType("System.Byte[]"));
for (int rowNumber = 0; rowNumber < imageDataSet.Tables[0].Rows.Count; rowNumber++)
{
DisplayImages(imageDataSet.Tables[0].Rows[rowNumber],"abc.jpg");
dt.Rows.Add(imageDataSet.Tables[0].Rows[rowNumber][0].ToString());
}
private void DisplayImages(DataRow row, string ImagePath)
{
FileStream imageStream = new FileStream(ImagePath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(imageStream);
row[0] = br.ReadBytes(Convert.ToInt32(br.BaseStream.Length));
}
推荐答案
您需要创建一个将图像转换为字节数组的函数.试试这个:
You need to create a function which will convert an image to byte array. Try this:
private byte[] GetByteArray(String strFileName)
{
System.IO.FileStream fs = new System.IO.FileStream(strFileName, System.IO.FileMode.Open);
// initialise the binary reader from file streamobject
System.IO.BinaryReader br = new System.IO.BinaryReader(fs);
// define the byte array of filelength
byte[] imgbyte = new byte[fs.Length + 1];
// read the bytes from the binary reader
imgbyte = br.ReadBytes(Convert.ToInt32((fs.Length)));
// add the image in bytearray
br.Close();
// close the binary reader
fs.Close();
// close the file stream
return imgbyte;
}
现在调用上面的函数,并将您的图像添加到数据表中.试试这个:
Now call the above function and add your image to datatable. Try this:
DataTable dt = new DataTable();
dt.Columns.Add("Image", typeof(byte[]));
dt.Rows[0]["Image"] = GetByteArray(strFilePath);
//this will add a row with the image. Accordingly, you need to loop and add the rows as required.
希望对您有帮助!
-授予
Hope it helps!
--Amit
这篇关于将图像添加到datatabale的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文