Error using Custom Field Property for PictureBox


Author
Message
Chris Diesel
Chris Diesel
StrataFrame User (166 reputation)StrataFrame User (166 reputation)StrataFrame User (166 reputation)StrataFrame User (166 reputation)StrataFrame User (166 reputation)StrataFrame User (166 reputation)StrataFrame User (166 reputation)StrataFrame User (166 reputation)StrataFrame User (166 reputation)
Group: StrataFrame Users
Posts: 74, Visits: 300
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
StrataFrame Team
S
StrataFrame Developer (6.3K reputation)StrataFrame Developer (6.3K reputation)StrataFrame Developer (6.3K reputation)StrataFrame Developer (6.3K reputation)StrataFrame Developer (6.3K reputation)StrataFrame Developer (6.3K reputation)StrataFrame Developer (6.3K reputation)StrataFrame Developer (6.3K reputation)StrataFrame Developer (6.3K reputation)
Group: StrataFrame Developers
Posts: 3K, Visits: 2.5K
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;
            }
        }
Chris Diesel
Chris Diesel
StrataFrame User (166 reputation)StrataFrame User (166 reputation)StrataFrame User (166 reputation)StrataFrame User (166 reputation)StrataFrame User (166 reputation)StrataFrame User (166 reputation)StrataFrame User (166 reputation)StrataFrame User (166 reputation)StrataFrame User (166 reputation)
Group: StrataFrame Users
Posts: 74, Visits: 300
Thank you so much!!!!  Smile
Chris Diesel
Chris Diesel
StrataFrame User (166 reputation)StrataFrame User (166 reputation)StrataFrame User (166 reputation)StrataFrame User (166 reputation)StrataFrame User (166 reputation)StrataFrame User (166 reputation)StrataFrame User (166 reputation)StrataFrame User (166 reputation)StrataFrame User (166 reputation)
Group: StrataFrame Users
Posts: 74, Visits: 300
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!!!
StrataFrame Team
S
StrataFrame Developer (6.3K reputation)StrataFrame Developer (6.3K reputation)StrataFrame Developer (6.3K reputation)StrataFrame Developer (6.3K reputation)StrataFrame Developer (6.3K reputation)StrataFrame Developer (6.3K reputation)StrataFrame Developer (6.3K reputation)StrataFrame Developer (6.3K reputation)StrataFrame Developer (6.3K reputation)
Group: StrataFrame Developers
Posts: 3K, Visits: 2.5K
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;
   }
}
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search