How to convert a Base64 row to an image and then bind it to a GridView in a Metro Style app?

I have a collection of products from a web service, I view this product in Grid View, but I get product images as Base64 rows. How can I convert it to images and snap it to images in grid mode?

Any piece of code that will help me with this problem.

+3
source share
4 answers

In WPF / Metro / Silverlight, an image is a user interface control. Its source is set to BitmapSource. BitmapSource is a data structure for storing image data.

Below is the code for extracting BitmapImage from an array of bytes.

public BitmapImage ImageFromBuffer(Byte[] bytes)
    {
        MemoryStream stream = new MemoryStream(bytes);
        BitmapImage image = new BitmapImage();
        image.SetSource ( stream.AsRandomAccessStream());
        return image;
    }

, stream.AsRandomAccessStream API. . IDWMaster SO

 public static class MicrosoftStreamExtensions
    {
        public static IRandomAccessStream AsRandomAccessStream(this Stream stream)
        {
            return new RandomStream(stream);
        }
    }
    class RandomStream : IRandomAccessStream
    {
        Stream internstream;
        public RandomStream(Stream underlyingstream)
        {
            internstream = underlyingstream;
        }
        public IInputStream GetInputStreamAt(ulong position)
        {
            internstream.Position = (long)position;
            return internstream.AsInputStream();
        }

        public IOutputStream GetOutputStreamAt(ulong position)
        {
            internstream.Position = (long)position;
            return internstream.AsOutputStream();
        }

        public ulong Size
        {
            get
            {
                return (ulong)internstream.Length;
            }
            set
            {
                internstream.SetLength((long)value);
            }
        }

        public bool CanRead
        {
            get { return internstream.CanRead; }
        }

        public bool CanWrite
        {
            get { return internstream.CanWrite; }
        }

        public IRandomAccessStream CloneStream()
        {
            //HACK, this is not clone, proper implementation is required, returned object will share same internal stream
            return new RandomStream(this.internstream);
        }

        public ulong Position
        {
            get { return (ulong)internstream.Position; }
        }

        public void Seek(ulong position)
        {
            internstream.Seek((long)position, SeekOrigin.Current);
        }

        public void Dispose()
        {
            internstream.Dispose();
        }

        public Windows.Foundation.IAsyncOperationWithProgress<IBuffer, uint> ReadAsync(IBuffer buffer, uint count, InputStreamOptions options)
        {
            throw new NotImplementedException();
        }

        public Windows.Foundation.IAsyncOperation<bool> FlushAsync()
        {
            throw new NotImplementedException();
        }

        public Windows.Foundation.IAsyncOperationWithProgress<uint, uint> WriteAsync(IBuffer buffer)
        {
            throw new NotImplementedException();
        }
    }

3

, ( , ).

+1

, :

    public static BitmapImage Base64StringToBitmap(string source)
    {
        var ims = new InMemoryRandomAccessStream();
        var bytes = Convert.FromBase64String(source);
        var dataWriter = new DataWriter(ims);
        dataWriter.WriteBytes(bytes);
        dataWriter.StoreAsync();
        ims.Seek(0);
        var img = new BitmapImage();
        img.SetSource(ims);
        return img;
    }

. , img.SetSource .

+5

, Base64String Image

  • []

    byte[] bytes = System.Convert.FromBase64String(thebase64string);
    
  • [] Image, .

    public async void SetImageFromByteArray(byte[] data, Windows.UI.Xaml.Controls.Image image)
    {
        InMemoryRandomAccessStream raStream =
            new InMemoryRandomAccessStream();
    
        DataWriter writer = new DataWriter(raStream);
    
        // Write the bytes to the stream
        writer.WriteBytes(data);
    
        // Store the bytes to the MemoryStream
        await writer.StoreAsync();
    
        await writer.FlushAsync();
    
        // Detach from the Memory stream so we don't close it
        writer.DetachStream();
    
        raStream.Seek(0);
    
        BitmapImage bitMapImage = new BitmapImage();
        bitMapImage.SetSource(raStream);
    
        image.Source = bitMapImage;
    }
    
+2

- :

byte[] encodedDataAsBytes = System.Convert.FromBase64String(encodedData);
MemoryStream stream = new MemoryStream(encodedDataAsBytes.Length);
stream.Write(encodedDataAsBytes, 0, encodedDataAsBytes.Length);
Image img = Image.FromStream(stream);

, ​​ . .

0
source

All Articles