我可以像这样成功加载下面的Bitmap,并在视图的Image控件中显示它.
var bitmapImage = new BitmapImage
{
UriSource =
new Uri("../Images/Test.JPG", UriKind.Relative)
};
Run Code Online (Sandbox Code Playgroud)
但是,只要我添加此行以从位图创建WriteableBitmap,
var w = new WriteableBitmap(bitmapImage);
Run Code Online (Sandbox Code Playgroud)
我在上面的行中收到运行时错误:"对象引用未设置为对象的实例."
似乎BitmapImage的创建被延迟了,可能是吗?我该怎么解决这个问题?
更新:
我现在正在尝试这个,但openImage似乎永远不会被击中.(即使没有试图让它同步,它仍然失败)这里有什么问题?
var image = new BitmapImage();
image.ImageOpened += (sender, args) => resetEventBitmap.Set();
image.ImageFailed += (o, eventArgs) =>
{
resetEventBitmap.Set();
throw eventArgs.ErrorException;
};
image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
image.UriSource = uri;
resetEventBitmap.WaitOne();
Run Code Online (Sandbox Code Playgroud)
谢谢,
参考:http: //www.blog.ingenuitynow.net/Silverlight+Creating+A+WriteableBitmap+From+A+Uri+Source.aspx
基本上,位图图像具有依赖属性"CreateOptions",默认情况下,它设置为"DelayCreation".这会导致位图延迟渲染,直到需要它为止.因此,这会导致我们的"对象引用未设置为对象的实例"错误.要解决这个问题,我们必须打破writeablebitmap构造函数中的位图创建,更改此选项,然后将其重新放入.在vb.net中,这看起来像:
Dim tmpUri As New Uri(yourpath.ToString)
Dim bmp As New BitmapImage
bmp.CreateOptions = BitmapCreateOptions.None
bmp.UriSource = tmpUri
Dim wb As New WriteableBitmap(bmp)
Run Code Online (Sandbox Code Playgroud)
BitmapImage _classField;
void LoadImageFunction()
{
_classField = new BitmapImage();
_classField.ImageOpened += new EventHandler<RoutedEventArgs>(bi_ImageOpened);
_classField.ImageFailed += new EventHandler<ExceptionRoutedEventArgs>(bi_ImageFailed);
//sorry.. totally forgot about order :)
_classField.UriSource = new Uri("../some/uri", UriKind.Relative);
}
void bi_ImageFailed(object sender, ExceptionRoutedEventArgs e)
{
//something has happend
throw e.ErrorException;
}
void bi_ImageOpened(object sender, RoutedEventArgs e)
{
//image is loaded.. now we can work with it..
var w = new WriteableBitmap(_classField);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8740 次 |
| 最近记录: |