图像数据绑定到 pictureBox

Image Databinding to the pictureBox

我尝试通过两种方法绑定数据集"data_set"中数据表"Applicant's Details"中列"Applicant's Image"的图像。但是当我 运行 表单应用程序时,我没有看到图片框 "imgusr" 中显示任何图像。我的绑定源名称是 "bindSource"。

假设 data_set 正确检索了所有内容,图像未加载到图片框可能是什么问题 "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);
    }

解决方法可以这么简单:

imgusr.DataBindings.Add(new Binding("Image", data_set, 
                                    "yourtablename.yourcolumnname", true));

请注意,您需要通过将最后一个参数 (enableFormatting) 设置为 true 来告诉 Binding 进行 格式化 。不再需要特殊代码来处理图像。

另请注意,我没有尝试在列名称中使用空格和撇号的必要格式。我建议使用标准名称!

最后确保设置要在 DataSet 中使用的 TableTableName 属性:

data_set.Tables[0].TableName = "yourtablename";

Update 从您的讨论来看,您似乎没有将图像数据正确保存到您的 dbms。这是您例程的一个版本,应该会更好:

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 等..