[英]How to convert Base64 string to image, then bind it to GridView in a Metro Style App?
我收集了來自Web服務的產品,我在網格視圖中預覽了這個產品,但是我將產品的圖像作為Base64字符串。 如何將其轉換為圖像並將其綁定到網格視圖中的圖像?
任何能幫助我解決這個問題的代碼。
這似乎對我有用:
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 [],如下所示
byte[] bytes = System.Convert.FromBase64String(thebase64string);
傳遞要將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; }
在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<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種方法未實施
我沒有測試,但上面應該在校長工作(可能是經過一些改進)。
嘗試這樣的事情:
byte[] encodedDataAsBytes = System.Convert.FromBase64String(encodedData);
MemoryStream stream = new MemoryStream(encodedDataAsBytes.Length);
stream.Write(encodedDataAsBytes, 0, encodedDataAsBytes.Length);
Image img = Image.FromStream(stream);
我沒有使用過地鐵,所以我無法幫助綁定到網格..最好的問候。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.