我正在尝试从存储中读取图像(.png / jpg)并在我的Windows Phone 8应用上获取像素值:
private static BitmapImage LoadBitmap(string iFilename)
{
var imgUri = new Uri(iFilename, UriKind.Relative);
var image = new BitmapImage { CreateOptions = BitmapCreateOptions.None, UriSource = imgUri };
return image;
}
public static string GetColorAttribute(string iFilename)
{
// Get Bitmap Image
var image = LoadBitmap(iFilename);
var wbmp = new WriteableBitmap(image);
}
执行时出现异常:
var wbmp = new WriteableBitmap(image);
{System.Reflection.TargetInvocationException:调用目标抛出了异常。 ---> System.TypeInitializationException:“MyProject”的类型初始值设定项引发了异常。 ---> System.NullReferenceException:未将对象引用设置为对象的实例。
是否有在Windows Phone 8上处理图像的示例?
答案 0 :(得分:2)
您应该使用SetSource
类的BitmapImage
方法:
private static WriteableBitmap LoadBitmap(string iFilename)
{
using (var stream = Application.GetResourceStream(new Uri(iFilename, UriKind.Relative)).Stream)
{
var bmpi = new BitmapImage();
bmpi.SetSource(stream);
bmpi.CreateOptions = BitmapCreateOptions.None;
WriteableBitmap bmp = new WriteableBitmap(bmpi);
bmpi.UriSource = null;
return bmp;
}
}
public static void GetColorAttribute(string iFilename)
{
// Get Bitmap Image
var wbmp = LoadBitmap(iFilename);
}
这篇文章解释了如何在Windows Phone上加载图像(该文章是为Windows Phone 7编写的,但它在WP8中没有改变):Image Tips for Windows Phone