将图像添加到datatabale

编程入门 行业动态 更新时间:2024-10-11 13:22:48
本文介绍了将图像添加到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

本文发布于:2023-08-06 11:30:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1311985.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   datatabale

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!