Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从mysql数据库c中检索blob图片#_C#_Mysql_Blob - Fatal编程技术网

C# 从mysql数据库c中检索blob图片#

C# 从mysql数据库c中检索blob图片#,c#,mysql,blob,C#,Mysql,Blob,我使用这段代码来检索我的图片,它与一个只包含blob的简单表协同工作,但当我试图为我的表用户调整它时,它包含(cin、nom、prenom….、image)异常,表明 “参数无效”(不是有效参数) 试试这个 DataTable userTable; DataTable ds; int cin; string nom; string prenom; Byte[] ImageByte; userTable = ds; if (userTable == null) return false

我使用这段代码来检索我的图片,它与一个只包含blob的简单表协同工作,但当我试图为我的表用户调整它时,它包含(cin、nom、prenom….、image)异常,表明

“参数无效”(不是有效参数)

试试这个

DataTable userTable;
DataTable ds;

int cin;
string nom;
string prenom;
Byte[] ImageByte;

userTable = ds;
if (userTable == null)
    return false;
else
{
    if (userTable.Rows.Count > 0)
    {
        foreach (DataRow userRow in userTable.Rows)
        {
            cin = Convert.ToInt32(userRow["cin"]);
            nom = userRow["nom"].ToString();
            prenom = userRow["prenom"].ToString();
            ImageByte = (Byte[])(userRow["image"]);
        }
    }
}
if (ImageByte != null)
{
    // You need to convert it in bitmap to display the imgage
    pictureBox1.Image = ByteToImage(ImageByte);
    pictureBox1.Refresh();
}

public static Bitmap ByteToImage(byte[] blob)
{
    MemoryStream mStream = new MemoryStream();
    byte[] pData = blob;
    mStream.Write(pData, 0, Convert.ToInt32(pData.Length));
    Bitmap bm = new Bitmap(mStream, false);
    mStream.Dispose();
    return bm;

}

这应该可以解决问题。

在哪一行出现异常?我认为您不必填写数据集,而是直接使用SqlDataReader。如果我删除了捕获,我不会出错,也不会有结果……谢谢,但我只需要在PictureBexpress中获得一个blob图片,请检查我更新的答案。您需要将字节数组转换为位图。请检查ByteToImage(byte[]byteArray)方法您能否在代码周围添加一些解释,而不仅仅是发布答案?试着帮助人们准确地理解哪一部分是错误的,为什么是错误的,为什么你的解决方案是他们需要应用的。
DataTable userTable;
DataTable ds;

int cin;
string nom;
string prenom;
Byte[] ImageByte;

userTable = ds;
if (userTable == null)
    return false;
else
{
    if (userTable.Rows.Count > 0)
    {
        foreach (DataRow userRow in userTable.Rows)
        {
            cin = Convert.ToInt32(userRow["cin"]);
            nom = userRow["nom"].ToString();
            prenom = userRow["prenom"].ToString();
            ImageByte = (Byte[])(userRow["image"]);
        }
    }
}
if (ImageByte != null)
{
    // You need to convert it in bitmap to display the imgage
    pictureBox1.Image = ByteToImage(ImageByte);
    pictureBox1.Refresh();
}

public static Bitmap ByteToImage(byte[] blob)
{
    MemoryStream mStream = new MemoryStream();
    byte[] pData = blob;
    mStream.Write(pData, 0, Convert.ToInt32(pData.Length));
    Bitmap bm = new Bitmap(mStream, false);
    mStream.Dispose();
    return bm;

}
byteBLOBData = ((Byte[])ds.Tables["image"].Rows[c - 1]["image"]);