Image in a Business Object

Image in a Business Object

Old forum URL: forums.lhotka.net/forums/t/11755.aspx


st3fanus posted on Monday, December 17, 2012

Hi there..

 

I'm developing wpf application, and I used CSLA 4.3.13 and Sql Server as my Database.

Could anyone help me to give me an example : How do make a Property in a Business Object to save an Image into data base and to fetch back again into Business Object ?

and with a database field i must set on Database layer ?

 

thanks a lot

stefanus

 

skagen00 replied on Tuesday, December 18, 2012

In BO - obviously it sounds like you might use a setter too... but our usage is just to display the image associated with a record.

        public readonly static PropertyInfo<byte[]> ImageBytesProperty = RegisterProperty<byte[]>(x => x.ImageBytes);

        /// <summary>
        /// Gets the bytes for the image (jpg thumnail form).
        /// </summary>
        public byte[] ImageBytes { get { return GetProperty(ImageBytesProperty); } }

When in data access - we get ours from a file but it's trivial to get the bytes from a database too of course.  I would recommend, if your images can be large - to consider using thumbnail images.  We display a thumbnail in an area on the screen and just grab the full image if they click into it.  There's a significant size difference - a decent sized image can easily create some slowness if going over the wire.  Now, you're doing WPF so I assume you may be all within a LAN or something and it's not as big of a deal.

                         using (MemoryStream ms = new MemoryStream())
                        {
                            using (Image image = Image.FromFile(letterFileName))
                            {
                                if (image.Width >= image.Height)
                                {
                                    // Get a thumbnail image for the image stored in the file.
                                    int width = 160;
                                    int height = (int)(image.Height * 1.0 / image.Width * 160);
                                    image.GetThumbnailImage(width, height, null, System.IntPtr.Zero).Save(ms, ImageFormat.Jpeg);
                                }
                                else
                                {
                                    // Get a thumbnail image for the image stored in the file.
                                    int height = 160;
                                    int width = (int)(image.Width * 1.0 / image.Height * 160);
                                    image.GetThumbnailImage(width, height, null, System.IntPtr.Zero).Save(ms, ImageFormat.Jpeg);
                                }
                            }
                            bytes = ms.ToArray();
                            isFound = true;
                        }

In the UI - CurrentImage is basically a binding to the byte[] property.

                     <Image MaxHeight="160"  x:Name="ThumbnailImage" Margin="5,3,5,3"
                            Source="{Binding CurrentImage, Converter={StaticResource binaryArrayToUriConverter}}"
                            HorizontalAlignment="Stretch" />

The converter is like this:

    public class BinaryArrayToUriConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null) return null;

            MemoryStream ms = new MemoryStream((byte[])value);
            BitmapImage image = new BitmapImage();
            image.SetSource(ms);
            return image;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

Copyright (c) Marimer LLC