【问题标题】:Image Databinding to the pictureBox图像数据绑定到图片框
【发布时间】:2016-02-17 08:38:44
【问题描述】:

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

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

另外,将 sizeMode 的图片框属性设置为“缩放”。

    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);
    }

【问题讨论】:

  • 您有什么样的数据源,用于您的图像?会不会,你的 Image 是一个 ByteArray 并且必须被转换?
  • @Smartis 我已作为“图像”类型存储在数据库中,并且刚刚检索到相同的内容。所以我认为是byteArray。
  • 您使用的是哪种数据库?
  • @Smartis Sql Server 数据库
  • 请注意,image 数据类型将在 Microsoft SQL Server 的未来版本中删除。 msdn.microsoft.com/de-at/library/ms187993.aspx

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


【解决方案1】:

解决办法可以这么简单:

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

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

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

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

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);
    }
}

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

【讨论】:

  • 其余一切正常,除了图像绑定!
  • 如果测试了我的代码并且它显示了表格中的第一张图片就好了。我在 mySql 中使用了一个 blob 列。你有错误吗?
  • 不使用sql server,恐怕不行。你注意到 Smartis 的评论了吗?这听起来好像至少使用新的推荐数据类型 varbinary(max) 进行测试可能不仅有帮助,而且无论如何也是更好的选择。..
  • 设置大小上限的参数,以便 dbms 可以进行更好的存储布局。您是否以任何其他方式成功提取了图像数据?我不知何故怀疑他们有问题,尤其是。按照另一个答案的cmets ..也许你可以展示你如何插入它们?你知道图片格式吗?
  • 正如我所料。实际长度是内存流报告的长度,因此您没有存储有效的图像数据。一旦你更正了你应该工作的原始代码.. fileinfo 只告诉你磁盘上的大小..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多