我收集了来自网络服务的产品,我在网格视图中预览了这个产品,但我将产品的图像作为Base64字符串。如何将其转换为图像并将其绑定到网格视图中的图像?
任何可以帮助我解决此问题的代码。
答案 0 :(得分: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应该负责延迟加载。
答案 1 :(得分:2)
下面是将Base64String
绑定到Image
控件
将字符串转换为byte [],如下所示
byte[] bytes = System.Convert.FromBase64String(thebase64string);
传递byte []和要将Image绑定到的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 :(得分: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;
}
请注意,stream.AsRandomAccessStream在API中不可用它是一种扩展方法。我是从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个方法未实施
我没有经过测试但是上面应该在校长工作(可能经过一些改进)。
答案 3 :(得分:0)
尝试这样的事情:
byte[] encodedDataAsBytes = System.Convert.FromBase64String(encodedData);
MemoryStream stream = new MemoryStream(encodedDataAsBytes.Length);
stream.Write(encodedDataAsBytes, 0, encodedDataAsBytes.Length);
Image img = Image.FromStream(stream);
我没有使用过Metro,所以我无法帮助绑定到网格.. 最好的问候。