C# 从SQL Server 2008向PictureBox加载图像失败

C# 从SQL Server 2008向PictureBox加载图像失败,c#,.net,winforms,visual-studio-2010,sql-server-2008,C#,.net,Winforms,Visual Studio 2010,Sql Server 2008,我无法从SQL Server 2008数据库读取图像并将其加载到Windows窗体PictureBox。以下是从DB检索图像的代码: //UI Button Binded Code private void LoadImage_Click(object sender, RoutedEventArgs e) { DataSet data = DBClient.GetEmployee(_EID[0]); //... //... byte[] pic = (byte[])dat

我无法从SQL Server 2008数据库读取图像并将其加载到Windows窗体
PictureBox
。以下是从DB检索图像的代码:

//UI Button Binded Code
private void LoadImage_Click(object sender, RoutedEventArgs e)
{
   DataSet data = DBClient.GetEmployee(_EID[0]);
   //...
   //...
   byte[] pic = (byte[])data.Tables[0].Rows[0]["Picture"];

   EmployeeCardForm ef = new EmployeeCardForm(name, fname, nic, deptt, desig, doj, address, ecode, pic);
   ef.Show();
}

 //EmployeeCardForm Constructor File Code
 public EmployeeCardForm(String name, String fname, String nic, String deptt, String desig, String doj, String address, String ecode, byte[] pic)
 {
        InitializeComponent();
        this.MaximizeBox = false;
        label18.Text = "Record has been successfully Saved. Please take Print out by pressing Print Button below!";
        try
        {
            pictureBox1.Image = Image.FromStream(new MemoryStream(pic));
            //Some code here
        }
        catch (IOException e)
        {
            MessageBox.Show("Some Error occurred!");
        }
        //declare event handler for printing in constructor
        printdoc1.PrintPage += new PrintPageEventHandler(printdoc1_PrintPage);
    }
当我运行此程序并单击按钮加载图像时,应用程序变得无响应。调试后,我发现以下代码行有问题:

pictureBox1.Image = Image.FromStream(new MemoryStream(pic));

当我使用try/catch块时,它显示了以下异常:
System.ArgumentException:参数无效。在System.Drawing.Image.FromStream(Stream Stream,Boolean useEmbeddedColorManagement,Boolean validateImageData)在System.Drawing.Image.FromStream(Stream Stream)在SimpleReport.EmployeeCardForm..ctor(字符串名称,字符串fname,字符串nic,字符串deptt,字符串设计,字符串doj,字符串地址,字符串ecode,字节[]pic)在D:\O Projects\EM\SimpleReport\EmployeeCardForm.cs中:第60行


以下是我将图像保存到DB的方式:

private void SaveImage_Click(object sender, RoutedEventArgs e)
{
     Picture_Path= filename;
     FileStream fs;
     fs = new FileStream(@Picture_Path, FileMode.Open, FileAccess.Read);
     //a byte array to read the image
     byte[] picbyte = new byte[fs.Length];
     fs.Read(picbyte, 0, System.Convert.ToInt32(fs.Length));
     fs.Close();
     DBClient.AddEmployee(ecode, emp_name.Text, fname, nic, deptt, desig, doj, address, picbyte);
}


请帮帮我

在一些聊天之后,问题是图片二进制文件以不正确的格式存储在数据库中。这可能有几个问题:

  • 表字段的类型/大小不正确
  • 不正确的字节读取
  • 首先检查是否有数据表字段的
    varbinary(max)
    type\size

    第二,以下是如何读取文件的所有字节:

    private void SaveImage_Click(object sender, RoutedEventArgs e)
    {
        byte[] allFileBytes = File.ReadAllBytes(filename); 
        DBClient.AddEmployee(ecode, emp_name.Text, fname, nic, deptt, desig, doj, address, allFileBytes);
    }
    

    在一些聊天之后,问题是图片二进制文件以不正确的格式存储在数据库中。这可能有几个问题:

  • 表字段的类型/大小不正确
  • 不正确的字节读取
  • 首先检查是否有数据表字段的
    varbinary(max)
    type\size

    第二,以下是如何读取文件的所有字节:

    private void SaveImage_Click(object sender, RoutedEventArgs e)
    {
        byte[] allFileBytes = File.ReadAllBytes(filename); 
        DBClient.AddEmployee(ecode, emp_name.Text, fname, nic, deptt, desig, doj, address, allFileBytes);
    }
    


    停在那一行,永远不会通过,没有错误?是的,它挂在上面一行,程序崩溃!!!pic变量中有数据吗?什么意思?有例外吗?尝试在catch块中将IOException更改为Exception,也许您可以捕获特定的异常。正如我所说,您获取ArgumentException,从中可以看到
    流没有有效的图像格式。另外,我无法想象13字节的图像:)所以你需要在数据库中有正确的二进制图像数据,第一次停在那一行,永远不会通过,没有错误?是的,它挂在上面一行,程序崩溃!!!pic变量中有数据吗?什么意思?有例外吗?尝试在catch块中将IOException更改为Exception,也许您可以捕获特定的异常。正如我所说,您获取ArgumentException,从中可以看到
    流没有有效的图像格式。此外,我无法想象13字节的图像:),所以您需要在数据库中首先有正确的二进制图像数据(几乎是+1;)请添加上下文,不要看评论,看起来你的答案与此无关question@Reniuz同样的结果,它总是使用以下数据将图像存储到DB中:0x53797374656D2E427974655B5D@Antonio巴库拉至少在这里帮助我。你只是坐着浪费SO评论框。@NidaSulheri这是给未来的谷歌人的,引用杰夫·阿特伍德的话:“SO的最终目的是集体增加世界上优秀编程知识的总和”。@Antonio Bakula你在这里只是在浪费我的时间!!!几乎+1;)请添加上下文,不要看评论,看起来你的答案与此无关question@Reniuz同样的结果,它总是使用以下数据将图像存储到DB中:0x53797374656D2E427974655B5D@Antonio巴库拉至少在这里帮助我。你只是坐着浪费SO评论框。@NidaSulheri这是给未来的谷歌人的,引用杰夫·阿特伍德的话:“SO的最终目的是集体增加世界上优秀编程知识的总和”。@Antonio Bakula你在这里只是在浪费我的时间!!!