图像数据绑定到pictureBox

时间:2016-02-17 08:38:45

标签: c# .net winforms data-binding bindingsource

我试图通过两种方法将数据集“data_set”中数据表“申请人详细信息”中的“申请人图像”列中的图像绑定。但是当我运行表单应用程序时,我看不到图片框中显示的图像“imgusr”。我的绑定源名称是“bindSource”。

假设data_set正确检索所有内容,图像未加载到图片框中会出现什么问题“imgusr”??

此外,sizeMode的picturebox属性为“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);
    }

1 个答案:

答案 0 :(得分:4)

解决方案可以这么简单:

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

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

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

最后确保在TableName中设置要使用的Table的{​​{1}}属性:

DataSet
您的讨论中的

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

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); } } Png等。