我正在尝试将base64string转换为位图。以下代码在我的Windows Phone项目中使用,它可以正常运行,但是我在Windows Store应用程序项目中重复使用此代码并获得此错误。我不知道解决此错误。
错误msg:
'windows.ui.xaml.media.imaging.bitmapsource.setsource(windows.storage.storage.storeams.irandomaccessstream)的最佳超载方法匹配'具有一些无效的参数
不能从'system.io.memorystream'转换为'windows.storage.storeams.irandomaccessstream'
datadb.cs
class DATADB
{
public class NewsObject
{
BitmapImage thumb = null;
public BitmapImage Thumb {
get {
if (thumb==null)
{
Regex rgx = new Regex("^[^,]*,");
thumb = Utilities.base64image(rgx.Replace(this.default_photo, ""));
}
return thumb;
}
}
public string date { get; set; }
public string id { get; set; }
public string info { get; set; }
}
}
Utilities.cs
class Utilities
{
public static BitmapImage base64image(string base64string)
{
if (base64string == "" || base64string == null) return null;
byte[] fileBytes = Convert.FromBase64String(base64string);
using (MemoryStream ms = new MemoryStream(fileBytes, 0, fileBytes.Length))
{
ms.Write(fileBytes, 0, fileBytes.Length);
BitmapImage bitmapImage = new BitmapImage();
bitmapImage.SetSource(ms); //Getting error message here.
return bitmapImage;
}
}
}
尝试使用InMemoryRandomAccessStream
代替MemoryStream
。
尝试使用
bitmapImage.SetSource(new MemoryRandomAccessStream(ms));
而不是
bitmapImage.SetSource(ms);