Silverlight: BitmapImage for WriteableBitmap

I can successfully load the next Bitmap like this and display it in the Image control on the view.

var bitmapImage = new BitmapImage { UriSource = new Uri("../Images/Test.JPG", UriKind.Relative) }; 

However, as soon as I add this line to create a WriteableBitmap from a bitmap,

  var w = new WriteableBitmap(bitmapImage); 

I get a runtime error in the line above: "The reference to the object is not installed in the object instance."

It seems that the creation of BitmapImage is delayed, maybe? How to fix it?

Update:

Now I'm trying to do this, but openImage seems to never get it. (without even trying to make it synchronous, it still doesn't work) What is wrong here?

 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(); 

Thanks,

+7
source share
3 answers
  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); } 
+3
source

Link: http://www.blog.ingenuitynow.net/Silverlight+Creating+A+WriteableBitmap+From+A+Uri+Source.aspx

Basically, a bitmap has a CreateOptions dependency property, which defaults to DelayCreation. This leads to the fact that the bitmap is delayed for rendering until it is needed. Therefore, this leads to the error "the reference to the object is not installed on the instance of the object." To fix this, we need to break the creation of the bitmap from the writeablebitmap constructor, change this parameter, and then paste it again. In vb.net, it looks like this:

  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) 
+6
source
 img1 = new BitmapImage(new Uri("/PrjName;component/Images/image01.jpg", UriKind.RelativeOrAbsolute)); img2 = new BitmapImage(new Uri("/PrjName;component/Images/image02.jpg", UriKind.RelativeOrAbsolute)); img1.CreateOptions = BitmapCreateOptions.None; img2.CreateOptions = BitmapCreateOptions.None; img1.ImageOpened += new EventHandler<RoutedEventArgs>(img1_ImageOpened); img2.ImageOpened += new EventHandler<RoutedEventArgs>(img2_ImageOpened); void img2_ImageOpened(object sender, RoutedEventArgs e) { load2 = true; } void img1_ImageOpened(object sender, RoutedEventArgs e) { load1 = true; } private void LayoutRoot_Loaded(object sender, RoutedEventArgs e) { while (!load1 && !load2) { } WriteableBitmap x = new WriteableBitmap(img1); WriteableBitmap y = new WriteableBitmap(img2); } 

That should work. it was for me ..! It makes it difficult, but how does it work!

+1
source

All Articles