turgut
turgut

Reputation: 1

Show image from database as byte[] in silverlight

i get image from client convert it to byte[] and send it to server. And convert byte[] to Base64String and insert into database.

And i do reverse to show image. But i cant see the image. Why???

//Convert to byte array
 public static byte[] ImageToByteArray(WriteableBitmap bitmap)

{
            int[] p = bitmap.Pixels;
            int len = p.Length << 2;
            byte[] result = new byte[len];
            Buffer.BlockCopy(p, 0, result, 0, len);
            return result;
}

//Converter
public object Convert(object value, Type targetType, object parameter,System.Globalization.CultureInfo culture)
        {
            if (value == null)
            {
                return null;
            }

            BitmapImage image = new BitmapImage();

            MemoryStream stream = new MemoryStream();
            stream.Write((byte[])value, 0, ((byte[])value).Length);
            image.SetSource(stream);


            return image;
        }

//While writing to database
else if (value.GetType() == typeof(byte[]))
            {
                return "'" + Convert.ToBase64String((byte[])value) + "'";
            }

else if ((type == typeof(byte[])))
            {
                return Convert.FromBase64String((string)value);
            }

Upvotes: 0

Views: 2322

Answers (3)

Jack Hogue
Jack Hogue

Reputation: 11

public class Base64StringToImageConverter : IValueConverter {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {
            if (!(value is string)) return DependencyProperty.UnsetValue;

            BitmapImage bitmapImage = new BitmapImage();
            bitmapImage.SetSource(new MemoryStream(System.Convert.FromBase64String((string)value)));
            return bitmapImage;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
            // TODO: Implement this method
            throw new NotImplementedException();
        }
    }

Upvotes: 1

turgut
turgut

Reputation: 1

I used BinaryReader and solved the problem.

My problem was not reading the image correct. iused BinaryReader to read the image and solved the problem.

                BinaryReader reader = new BinaryReader(fileInfo.OpenRead());

                byte[] tempImage = new byte[reader.BaseStream.Length];

                reader.Read(tempImage, 0, tempImage.Length); 

Upvotes: 0

ChrisF
ChrisF

Reputation: 137148

I have the following code to convert a byte array directly to an image:

var bitmapImage = new BitmapImage();
bitmapImage.SetSource(new MemoryStream(imageData));
newImage.Source = bitmapImage;

So as long as the conversion to and from the Base64String is working this should be all you need.

As an aside, you don't need to convert to a Base64String to store in the database. You just need to set the column type to image (assuming you are using MS SQL Server)

Upvotes: 1

Related Questions