如何将Base64字符串转换为图像,然后将其绑定到Metro风格应用程序中的GridView?

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

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

在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; } 

请注意,stream.AsRandomAccessStream在API中不可用它是一种扩展方法。 我从IDWMaster的回答中发现了这个问题

以下是扩展方法的代码

  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 ReadAsync(IBuffer buffer, uint count, InputStreamOptions options) { throw new NotImplementedException(); } public Windows.Foundation.IAsyncOperation FlushAsync() { throw new NotImplementedException(); } public Windows.Foundation.IAsyncOperationWithProgress WriteAsync(IBuffer buffer) { throw new NotImplementedException(); } } 

最后3种方法未实施

我没有测试,但上面应该在校长工作(可能是经过一些改进)。

这似乎对我有用:

  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应该负责延迟加载。

下面是将Base64String绑定到Image控件的方法

  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; } 

尝试这样的事情:

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

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