且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将图像添加到datatabale

更新时间:2023-12-06 16:32:58

您需要创建一个将图像转换为字节数组的函数.试试这个:
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