我从网络服务的产品集合,我预览网格视图这个产品,但我得到的产品为Base64字符串的图像。 我怎样才能将其转换为图像,并将其绑定到网格视图的图像?
任何一段代码,这将帮助我在这个问题上。
我从网络服务的产品集合,我预览网格视图这个产品,但我得到的产品为Base64字符串的图像。 我怎样才能将其转换为图像,并将其绑定到网格视图的图像?
任何一段代码,这将帮助我在这个问题上。
在WPF /地铁/ Silverlight中,图像是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这个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种方法未实现
我没有测试过,但上面的应该主要工作(可能是一些细化后)。
这似乎为我工作:
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
控制
转换的字符串字节[]如下
byte[] bytes = System.Convert.FromBase64String(thebase64string);
通过字节[],并要结合图像发送到图像控制。
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);
我还没有和地铁工作过,所以我不能用结合网格帮助..最好的问候。