3

我收集了来自网络服务的产品,我在网格视图中预览了这个产品,但我将产品的图像作为 Base64 字符串。如何将其转换为图像并将其绑定到网格视图中的图像?

任何可以帮助我解决这个问题的代码。

4

4 回答 4

5

这似乎对我有用:

    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 应该处理延迟加载。

于 2012-06-05T20:25:22.057 回答
2

下面是将 a 绑定Base64StringImage控件的方法

  1. 将字符串转换为 byte[] 如下

    byte[] bytes = System.Convert.FromBase64String(thebase64string);
    
  2. 传递要绑定 Image 的 byte[] 和 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;
    }
    
于 2014-05-09T10:46:21.400 回答
1

在 WPF/Metro/Silverlight 中,Image 是一个 UI 控件。它的源设置为 BitmapSource。BitmapSource 是保存图像数据的数据结构。

以下是从字节数组中检索 BitmapImage 的代码。

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

注意,API 中没有 stream.AsRandomAccessStream 是一种扩展方法。我从 IDWMaster对这个SO question的回复中找到了它

以下是扩展方法的代码

 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个方法未实现

我没有测试过,但上面应该可以正常工作(可能需要经过一些改进)。

于 2012-05-09T13:43:56.860 回答
0

尝试这样的事情:

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

我没有与地铁合作过,所以我无法帮助绑定到网格.. 最好的问候。

于 2012-05-09T13:20:51.517 回答