图像数据绑定到pictureBox [英] Image Databinding to the pictureBox

查看:160
本文介绍了图像数据绑定到pictureBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试通过两种方法将数据集 data_set中数据表申请人的详细信息中申请人的图像列中的图像进行绑定。但是,当我运行Form Application时,在图片框 imgusr中看不到任何图像。我的绑定源名称是 bindSource。

I have tried to bind the image from the column "Applicant's Image" in the datatable "Applicant's Details" in Data Set "data_set" through two methods. But When I run the Form Application I see no image displayed in the picturebox "imgusr". My binding source name is "bindSource".

假设data_set正确检索了所有内容,那么图像未加载到图片框 imgusr中可能是什么问题?

Assuming the data_set retrieves everything correctly, what could be problem for image not being loaded into the picturebox "imgusr"??

此外,sizeMode的picturebox属性为 zoom。

Also, the picturebox property of sizeMode to "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);
    }


推荐答案

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

The solution can be as simple as this:

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

请注意,您需要告知 Binding 通过将最后一个参数( enableFormatting )设置为 true 来进行格式化

Note that you need to tell the Binding to do the formatting by setting the last parameter (enableFormatting) to true. No more special code is needed to process the image.

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

Also note that I didn't try the necessary formatting to use blanks and apostrophes in the column name. I recommend using standard names!

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

And finally make sure to set the TableName property of the Table you want to use in your DataSet:

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

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

Update from your discussion it seems that you do not save the image data correctly to your dbms. Here is a version of your routine, that should work better:

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

Do make sure to use the correct file format, e.g. Png or Jpeg etc..

这篇关于图像数据绑定到pictureBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆