2

I am trying to create a BitmapImage from a byte array which is returned by a service.

My code is:

using (sc = new ServiceClient()) { using (MemoryStream ms = new MemoryStream(sc.GetImage())) { Display = new BitmapImage(); Display.BeginInit(); Display.StreamSource = ms; Display.EndInit(); } } 

However, an exception is thrown at the EndInit method. It says Object reference not set to an instance of an object..

It seems, that Uri is null and it causes the problem. Unfortunately, I cannot find a solution myself.

5
  • According to the documentation, do you also assign a value to Display.UriSource? If so, it will ignore the StreamSource. In addition, do you have the CacheOption property set to BitmapCacheOption.OnLoad? EDIT: Also, it appears that Display is a member (field/property), is it possible you have a threading issue here that replaces/changes Display as you're working with it? Commented May 18, 2013 at 15:30
  • @ChrisSinclair, I do not assign a vaule to UriSource. I just checked a stack trace and it seemed to me, that a null pointer exception is thrown due to this attribute. It was just a guess. Display is a property indeed, which is bound to my XAML layout. So, basically I would like to get an image from the WCF service and then display it at the WPF window. Besides this binding, I do not have code, which can cause a threading issue. Commented May 18, 2013 at 15:59
  • Can you try setting its CacheOption property to BitmapCacheOption.OnLoad? Otherwise, from what I understand, it will lazily try to access the stream which may be closed by the time it reads it. According to comments here be sure to set it after BeginInit(). EDIT: that is: Display = new BitmapImage(); Display.BeginInit(); Display.CacheMode = BitmapCacheOption.OnLoad; Display.StreamSource = ms; Display.EndInit(); Commented May 18, 2013 at 16:09
  • @ChrisSinclair, BitmapImage does not have CacheMode property, I suppose you are reffering to CacheOption one instead. Unfortunately, adding this line did not help. I still have a null pointer exception... Commented May 18, 2013 at 16:41
  • Yeah, it was a typo, sorry. Glad you managed to figure out the problem! Commented May 18, 2013 at 16:52

2 Answers 2

4

Well, it turned out, that WPF binding was causing the error.

private BitmapImage _display; public BitmapImage Display { get { return _display; } set { _display = value; RaisePropertyChanged("Display"); } } 

I resolved the issue by getting an image not in the property Display itself, but rather in the filed _display. So, the following is working fine.

using (sc = new ServiceClient()) { using (MemoryStream ms = new MemoryStream(sc.GetImage())) { _display = new BitmapImage(); _display.BeginInit(); _display.CacheOption = BitmapCacheOption.OnLoad; _display.StreamSource = ms; _display.EndInit(); } } Display = _display; 
Sign up to request clarification or add additional context in comments.

Comments

1

u are assigning memory stream directly to the bitmap source, which causes error. first u need to get that array of bytes & than convert it into the memory stream and then assign to the bitmap source, that's it !!!

using (sc = new ServiceClient()) { Byte[] array = sc.GetImage(); Display = new BitmapImage(); Display.BeginInit(); Display.StreamSource = new MemoryStream(array); Display.EndInit(); } 

3 Comments

I didn't know you could have using on a byte[]; I thought it must be IDisposable. EDIT: Quick test, I don't think you can. How is this code (assuming you fix it) any different than what's supplied? It just avoids disposing the stream?
The description in your updated answer is exactly what Dmitry is already doing. The only difference between your answer and his original code is that your answer doesn't dispose the memory stream.
Well, as @ChrisSinclair said, your code causes the same behaviour as one in my question with the only difference: in your case the memory stream stays in the memory forever. Unfortunately, the same exception is thrown.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.