gpt4 book ai didi

c# - 从 SQL Server 2008 到 PictureBox 的图像加载失败

转载 作者:太空宇宙 更新时间:2023-11-03 20:25:57 25 4
gpt4 key购买 nike

<分区>

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

//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 block 时,它显示了以下异常:
System.ArgumentException:参数无效。在 System.Drawing.Image.FromStream(流流, bool useEmbeddedColorManagement, bool validateImageData) String desig, String doj, String address, String ecode, Byte[] pic) in D:\O Projects\EM\SimpleReport\EmployeeCardForm.cs:line 60

这就是我将图像保存到数据库中的方式:

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);
}


请帮帮我!

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com