2

how can I handle and bind a PictureBox-Control from Windows-Forms to a binary CFE-Type i.e. image-type? Am I supposed to take another type for this?

Regards, Mykola

2 Answers2

1

You can load the image using the GetInputStream method:

using (var stream = _customer.Photo.GetInputStream()
{
    pictureBox1.Image = Image.FromStream(stream);
}
meziantou
  • 20,589
  • 7
  • 64
  • 83
0

Using Extension-Methods from the ImageConverter-Class save and load of an image-value could be very easy, i.e.:

    pictureBoxLogo.Image.saveImage(obj.Photo);
    pictureBoxLogo.Image = ((Image)null).loadImage(obj, obj.Photo);

Here how the Converter-Class could look like:

...
using System.IO;
using CodeFluent.Runtime.BinaryServices;

public static class ImageConverter
{
     public static byte[] toByteArray(this Image image)
    {
        using (var ms = new System.IO.MemoryStream())
        {
            image.Save(ms, image.RawFormat);
            return ms.ToArray();
        }
    }

    public static Image toImage(this byte[] bytesArr)
    {
        MemoryStream memstr = new MemoryStream(bytesArr);
        Image img = Image.FromStream(memstr);
        return img;
    }

    public static Image loadImage(object entity, BinaryLargeObject image)
    {
        if (entity != null && image != null)
        {
            using (var stream = image.GetInputStream())
            {
                if (stream.Length > 0)
                    return Image.FromStream(stream);
                else
                    return null;
            }
        }
        else
            return null;
    }

    public static Image loadImage(this Image owner, object entity, BinaryLargeObject image)
    {
        return loadImage(entity, image);
    }

    public static void saveImage(this Image owner, BinaryLargeObject image)
    {
        if (owner != null && image != null)
            image.Save(owner.toByteArray());
    }
}