其他分享
首页 > 其他分享> > 图像数据绑定到pictureBox

图像数据绑定到pictureBox

作者:互联网

我试图通过两种方法从数据集“ data_set”中的数据表“申请人的详细信息”中的“申请人的图像”列中绑定图像.但是,当我运行Form Application时,在图片框“ 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);
    }

解决方法:

解决方案可以像这样简单:

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

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

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

最后,确保设置要在数据集中使用的表的TableName属性:

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

从您的讨论更新来看,您似乎没有将图像数据正确地保存到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);
    }
}

请确保使用正确的文件格式,例如Png或Jpeg等.

标签:bindingsource,data-binding,c,net,winforms
来源: https://codeday.me/bug/20191027/1943691.html