Image Databinding to the pictureBox

牧云@^-^@ 提交于 2019-12-01 20:23:19

The solution can be as simple as this:

imgusr.DataBindings.Add(new Binding("Image", data_set, 
                                    "yourtablename.yourcolumnname", 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!

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";

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

Testing is as simple as that:

private void test_button_Click(object sender, EventArgs e)
{
    StoreImage(someImageFile);
    using (MemoryStream ms = new MemoryStream(img_byte))
    {
        aPictureBox.Image = Image.FromStream(ms);
    }
}

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!