load image into writeablebitmap
Asked Answered
T

1

5

So Im trying trying to load an image into a WriteableBitmap but I'm getting a NullReferenceException. Can't figure out why because I think this used to work before

BitmapImage b = new BitmapImage(new Uri(@"images/Car.jpg", Urikind.Relative)); WriteableBitmap wb = new WriteableBitmap(b);

thats it. I have the car image as a resource in my project. It works fine as I can create a Image control and set its source to the BitmapImage and display it. Thanks

Tobi answered 2/9, 2012 at 22:16 Comment(0)
L
12

As seen from other SO questions, it seems that the reason you get the null reference exception is that BitmapImage.CreateOptions Property default value is BitmapCreateOptions.DelayCreation. You can set it to BitmapCreateOptions.None and create WriteableBitmap after image loaded:

BitmapImage img = new BitmapImage(new Uri("somepath",UriKind.Relative));
img.CreateOptions = BitmapCreateOptions.None;
img.ImageOpened += (s, e) =>
   {
       WriteableBitmap wbm = new WriteableBitmap((BitmapImage)s);
   };

Using that code, the WritableBitmap will wait until the BitmapImage is loaded, and then it will be able to be assigned to the WritableBitmap.

Lithoid answered 3/9, 2012 at 0:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.