2

Is there a way to bind a PictureBox to a string so that when the string will change, it will call LoadAsync() with the url in the string and load an image?

Currently this is what I have in the auto generated code.

this.itemImagePictureBox.DataBindings.Add(new System.Windows.Forms.Binding("Image", this.MySource, "ItemImage", true));

What I want instead is:

this.itemImagePictureBox.DataBindings.Add(new System.Windows.Forms.Binding("String", this.MySource, "ImageUrl", true));

MySource has a property for url string, and I also tried to make it have a field of an Image, but a Bitmap for example doesn't have a load async feature so I still can't use the url string.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
shinzou
  • 5,850
  • 10
  • 60
  • 124

2 Answers2

3

Make sure the WaitOnLoad property is false (default), thus enabling asynchronous image load, and then bind to ImageLocation property:

this.itemImagePictureBox.WaitOnLoad = false;
this.itemImagePictureBox.DataBindings.Add(new System.Windows.Forms.Binding(
    "ImageLocation", this.MySource, "ImageUrl", true));
Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343
0

Does it have to be databinding? Why not:

MyImage = new Bitmap(fileToDisplay);
itemImagePictureBox.Image = (Image) MyImage ;
await itemImagePictureBox.LoadAsync();
itemImagePictureBox.Refresh();

Or, if the image is a URL:

Load an image from a url into a PictureBox

Community
  • 1
  • 1
Shannon Holsinger
  • 2,293
  • 1
  • 15
  • 21