3

I'm just trying to build a small C# .Net 4.0 application using Windows Forms (WPF I don't know at all, Windows Forms at least a little :-) ).

Is it possible to directly bind a System.Drawing.Bitmap object to the Image property of a PictureBox? I tried to use PictureBox.DataBindings.Add(...) but this doesn't seem to work.

How can I do this?

Thanks and best regards,
Oliver

Baldewin
  • 1,613
  • 2
  • 16
  • 23
  • What are you wishing to use as your datasource? – Mark Hall Jul 01 '12 at 15:29
  • If you have a bitmap, you can just assign it to the Image property of the PictureBox at runtime - see http://msdn.microsoft.com/en-us/library/system.windows.forms.picturebox.image.aspx – dash Jul 01 '12 at 15:29

3 Answers3

4

This works for me:

Bitmap bitmapFromFile = new Bitmap("C:\\temp\\test.bmp");

pictureBox1.Image = bitmapFromFile;

or, in one line:

pictureBox1.Image = new Bitmap("C:\\temp\\test.bmp");

You might be overcomplicating this - according to the MSDN documentation, you can simple assign the bitmap directly to the PictureBox.Image property.

dash
  • 89,546
  • 4
  • 51
  • 71
  • Thanks for the answer. In fact I know this :) ... But it thought it's the ".Net way" to have data bindings!? – Baldewin Jul 01 '12 at 17:36
  • @Baldewin - it depends on your data model; data bindings are very common for actual data and are useful when you expect the underlying data to change - a control "bound" to this data will often automatically refresh. You *can* do this for image boxes but it is far less complex to manage the PictureBox.Image property directly; you can also control when the images are disposed too, which can be important in some circumtances. I've done this in the past with a file path to the image - so the DataBinding syntax for that is pictureBox.DataBindings.Add("ImageLocation", dataTable, "FilePath"); – dash Jul 01 '12 at 17:47
  • @Baldewin If you let me know the exact code you are using for your databinding then I'll try and help; be aware though that it's not necessary and is less common than setting the Image property (or the ImageLocation property) directly. – dash Jul 01 '12 at 17:51
3

You can use the PictureBox.DataBindings.Add(...)
The trick is to create a separate property on the object you are binding to to handle the conversion between null and and empty picture.

I did it this way.

In my form load I used

this.PictureBox.DataBindings.Add(new Binding("Visible", this.bindingSource1, "HasPhoto", false, DataSourceUpdateMode.OnPropertyChanged));

this.PictureBox.DataBindings.Add(new Binding("Image", this.bindingSource1, "MyPhoto",false, DataSourceUpdateMode.OnPropertyChanged));

In my object I have the following

[NotMapped]
public System.Drawing.Image MyPhoto
{
    get
    {            
        if (Photo == null)
        {
           return BlankImage;        
        }
        else
        {
           if (Photo.Length == 0)
           {
              return BlankImage;     
           }
           else
           {
              return byteArrayToImage(Photo);
           }
        }
    }
    set
    {
       if (value == null)
       {
          Photo = null;
       }
       else
       {
           if (value.Height == BlankImage.Height)  // cheating
           {
               Photo = null;
           }
           else
           {
               Photo = imageToByteArray(value);
           }
        }
    }
}

[NotMapped]
public Image BlankImage {
    get
    {
        return new Bitmap(1,1);
    }
}

public static byte[] imageToByteArray(Image imageIn)
{
    MemoryStream ms = new MemoryStream();
    imageIn.Save(ms, ImageFormat.Gif);
    return ms.ToArray();     
}

public static Image byteArrayToImage(byte[] byteArrayIn)
{
    MemoryStream ms = new MemoryStream(byteArrayIn);
    Image returnImage = Image.FromStream(ms);
    return returnImage;
}
DanielV
  • 2,076
  • 2
  • 40
  • 61
Kirsten
  • 15,730
  • 41
  • 179
  • 318
  • I have also used pictureBox.DataBindings.Add( "Image", dataSource, dataMember,true); The last parameteris important – Kirsten Sep 13 '14 at 03:02
2

You can do:

System.Drawing.Bitmap bmp = new System.Drawing.Bitmap("yourfile.bmp");
picturebox1.Image = bmp;
Habib
  • 219,104
  • 29
  • 407
  • 436