StrataFrame Forum

Error using Custom Field Property for PictureBox

http://forum.strataframe.net/Topic33462.aspx

By Chris Diesel - 3/17/2017

I get this error I believe during binding of a Custom Field Property to your PictureBox control.  If I break execution right before the field value is returned, it looks correct.

Error:
    An error occurred while refreshing the data from field 'IwbProduct.PrimaryImage' to property 'Image' on control '.'
Inner Exception:
    Cannot format the value to the desired type.

        
        #region Custom Field Properties

        [Description("Primary Image"), Browsable(false), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public byte[] PrimaryImage
        {
            get
            {
                byte[] primaryImage = new byte[0];

                if (this.Count > 0 && this.CurrentRow != null)
                {
                    using (IwbProductImages iwbProductImages = new IwbProductImages())
                    {
                        primaryImage = iwbProductImages.GetPrimaryImage(this.Guid);
                    }
                }

                return primaryImage;
            }
        }

        #endregion

        /// <summary>
        /// Provider property descriptors for the custom bindable fields
        /// </summary>
        protected override MicroFour.StrataFrame.Business.FieldPropertyDescriptor[] GetCustomBindablePropertyDescriptors()
        {
            //-- Return the array of property descriptors
            return new MicroFour.StrataFrame.Business.FieldPropertyDescriptor[] { new ReflectionPropertyDescriptor("PrimaryImage", typeof(IwbProduct)) };
        }




Thanks,
Chris
By StrataFrame Team - 3/20/2017

The Image property on the PictureBox is a System.Drawing.Bitmap, not a byte[].  You'll need to change the property to return a System.Drawing.Bitmap.

You'll then need to convert your byte[] to a Bitmap using by wrapping it in a MemoryStream.

public System.Drawing.Bitmap PrimaryImage
        {
            get
            {
                if (this.Count > 0 && this.CurrentRow != null)
                {
                    using (IwbProductImages iwbProductImages = new IwbProductImages())
                    {
using(MemoryStream ms = new MemoryStream(iwbProductImages.GetPrimaryImage(this.Guid)))
{
return new System.Drawing.Bitmap(ms);
}
}
            }

return null;
            }
        }
By Chris Diesel - 3/20/2017

Thank you so much!!!!  Smile
By Chris Diesel - 3/23/2017

That worked great when there is an image to display.  Having problems dealing with no image though.  I get the same error listed above when it returns null.  Any ideas on that?

Thanks!!!
By StrataFrame Team - 3/23/2017

Any time the property get throws an error during binding, it wraps it in the binding exception that you're getting.

You'll need to store off the data into a byte[] and check it's length to make sure there's actually data.  While you're at it, you might as well catch any exceptions and return a placeholder if you encounter one.

public System.Drawing.Bitmap PrimaryImage
{
   get
   {
      if (this.Count > 0 && this.CurrentRow != null)
      {
         try
         {
            using (IwbProductImages iwbProductImages = new IwbProductImages())
            {
               byte[] imageData = iwbProductImages.GetPrimaryImage(this.Guid);
               if ((imageData?.Length ?? 0) > 0)
               {
                  using(MemoryStream ms = new MemoryStream(imageData))
                  {
                      return new System.Drawing.Bitmap(ms);
                  }
               }
               else
               {
                  //-- There is no image, so return either nothing or a placeholder
                  return null;
                  //return My.Resources.NoImagePlaceholder;
               }
            }
         catch
         {
            //-- There was some error loading the image, so return either nothing or a placeholder
            return My.Resources.ErrorImagePlaceholder;
            //return null;
         }
      }
      
      //-- No current row in the BO
      return null;
   }
}