将图像数据绑定到pictureBox

4
我已经尝试通过两种方法将“应聘者详情”数据集中的“应聘者图像”列中的图像绑定到“bindSource”上,但是当我运行表单应用程序时,图片框“imgusr”中没有显示任何图片。假设数据集正确地检索了所有内容,那么导致图片未加载到图片框“imgusr”的问题可能是什么?此外,需要将图片框属性“sizeMode”设置为“zoom”。
    private void Update_Load(object sender, EventArgs e){
        data_set = blobj.srcforVU();
        bindSource.DataSource = data_set;
        bindSource.DataMember = "Applicant's Details";
        lbidvalue.DataBindings.Add(new Binding("Text", bindSource, "Applicant's ID", false));

        //method 1
        //Binding binding = new Binding("Image", bindSource, "Applicant's Image", true, DataSourceUpdateMode.OnPropertyChanged);
        //binding.Format += new ConvertEventHandler(binding_Format);
        //imgusr.DataBindings.Add(binding);
        //method 2
        imgusr.DataBindings.Add(new Binding("Image", bindSource, "Applicant's Image", true));

        tbfname.DataBindings.Add(new Binding("Text", bindSource, "First Name", true));
        tblname.DataBindings.Add(new Binding("Text", bindSource, "Last Name", true));
        tbgender.DataBindings.Add(new Binding("Text", bindSource, "Gender", true));
        tbbdate.DataBindings.Add(new Binding("Text", bindSource, "Birth Date", true));
        tbmob.DataBindings.Add(new Binding("Text", bindSource, "Mobile No", true));
        tbadd.DataBindings.Add(new Binding("Text", bindSource, "Address", true));
        tbcntry.DataBindings.Add(new Binding("Text", bindSource, "Country", true));
        tbmstat.DataBindings.Add(new Binding("Text", bindSource, "Is Married", true));
        tbspfname.DataBindings.Add(new Binding("Text", bindSource, "Spouse's First Name", true));
        tbsplname.DataBindings.Add(new Binding("Text", bindSource, "Spouse's Last Name", true));
        tbspage.DataBindings.Add(new Binding("Text", bindSource, "Spouse's Age", true));
        tbchild.DataBindings.Add(new Binding("Text", bindSource, "No Of Children", true));
        bindNavigator.BindingSource = bindSource;

        afterloadoptions();
    }

public void binding_Format(object sender, ConvertEventArgs e)
    {
        string path = (string)e.Value;
        e.Value = Image.FromFile(path);
    }

你的图像使用了什么类型的数据源?是不是图像是一个字节数组,需要进行转换? - Smartis
@Smartis 我已经将其存储在数据库中,类型为“image”,并且刚刚检索了相同的内容。因此,我认为它是字节数组。 - Sagar Maru
你使用哪种类型的数据库? - Smartis
@Smartis Sql Server 数据库 - Sagar Maru
1
请注意,image 数据类型将在未来的 Microsoft SQL Server 版本中被移除。https://msdn.microsoft.com/de-at/library/ms187993.aspx - Smartis
1个回答

6
解决方案可以非常简单,就像这样:
imgusr.DataBindings.Add(new Binding("Image", data_set, 
                                    "yourtablename.yourcolumnname", true));

请注意,您需要通过将最后一个参数(enableFormatting)设置为true来告诉Binding执行格式化。不需要更多的特殊代码来处理图像。
还要注意,我没有尝试使用空格和撇号来格式化列名。建议使用标准名称!
最后,请务必设置您想在DataSet中使用的TableTableName属性:
data_set.Tables[0].TableName = "yourtablename";

更新 根据您的讨论,似乎您没有正确将图像数据保存到数据库中。以下是您例程的一个版本,应该可以更好地工作:

byte[] img_byte = null;
long imgfilelength = 0;

private void StoreImage(string ChosenFile)
{
    try { 
        using (Image img = Image.FromFile(ChosenFile))
        using (MemoryStream ms = new MemoryStream())
        {
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            ms.Close();
            img_byte = ms.ToArray();
            imgfilelength = img_byte.Length;
        }
    } catch (Exception e) { MessageBox.Show(e.ToString()); }
}

测试就是这么简单:

private void test_button_Click(object sender, EventArgs e)
{
    StoreImage(someImageFile);
    using (MemoryStream ms = new MemoryStream(img_byte))
    {
        aPictureBox.Image = Image.FromStream(ms);
    }
}

请确保使用正确的文件格式,例如PngJpeg等。


除了图像绑定之外,一切工作正常! - Sagar Maru
1
如果您已经测试了我的代码,并且它可以正常显示表中的第一张图片。我在MySQL中使用了一个blob列。您是否遇到了错误? - TaW
1
很抱歉,我不能使用SQL Server。你注意到Smartis的评论了吗?这听起来好像至少测试新推荐的数据类型varbinary(max)不仅可以帮助,而且也可能是更好的选择。 - TaW
1
一个参数用于设置大小的上限,以便DBMS可以进行更好的存储布局。你成功地以其他方式提取了图像数据吗?我有点怀疑它们是有问题的,尤其是在其他答案的评论中...也许你可以展示一下如何插入它们?你知道关于图像格式的任何信息吗? - TaW
2
正如我所预料的那样。实际长度是内存流报告的长度,因此您没有存储有效的图像数据。一旦您纠正了这个问题,您原来的代码应该可以工作。fileinfo只告诉您磁盘上的大小。 - TaW
显示剩余30条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接