BitmapImage img = new BitmapImage(new Uri("somepath",UriKind.Relative));
WriteableBitmap wbm = new WriteableBitmap(img);
我在上面那一行遇到了运行时错误:"对象引用未设置为对象的实例"。
BitmapImage img = new BitmapImage(new Uri("somepath",UriKind.Relative));
WriteableBitmap wbm = new WriteableBitmap(img);
我在上面那一行遇到了运行时错误:"对象引用未设置为对象的实例"。
BitmapCreateOptions.DelayCreation
。你可以将其设置为 BitmapCreateOptions.None
,并在图像加载后创建WriteableBitmap
:BitmapImage img = new BitmapImage(new Uri("somepath",UriKind.Relative));
img.CreateOptions = BitmapCreateOptions.None;
img.ImageOpened += (s, e) =>
{
WriteableBitmap wbm = new WriteableBitmap((BitmapImage)s);
};
Uri uri = new Uri("/ProjectName;component/Images/image.jpg", UriKind.Relative);
StreamResourceInfo resourceInfo = Application.GetResourceStream(uri);
BitmapImage img = new BitmapImage();
img.SetSource(resourceInfo.Stream);
WriteableBitmap wbm = new WriteableBitmap(img);
注意,此资源是由应用程序类定义的静态方法GetResourceStream访问的。现在,如果将文件的构建操作更改为Content而不是Resource,则可以大大简化Uri语法。
Uri uri = new Uri("Images/image.jpg", UriKind.Relative);