Wednesday, March 10, 2010

Silverlight: show image from database

            // image1 is Image SL control
            // blob is table entity
            // BLOBASIMAGE has type byte[] (db type: image)
            using (MemoryStream stream = new MemoryStream(blob.BLOBASIMAGE))
            {
                try
                {
                    BitmapImage im = new BitmapImage();
                    im.SetSource(stream);
                    image1.Stretch = Stretch.Uniform;
                    image1.Source = im;
                }
                catch (Exception)
                {   // major reason: wrong picture format. JPG is OK
                    image1.Source = null;
                }

            }

No comments:

Post a Comment