I have had a really irritating issue with my windows phone app that I am currently working on. I am trying to create live tiles with numbers bigger than 99 shown for the Count property of the TileData.
To do this I have decided to draw my own images with the Count on them and then save them in Isolated Storage for the tile. There are many problems trying to get this done, but they all seem to stem from the fact that Windows Phone or Silverlight in general perhaps does all its image loading / rendering in a lazy or at least background fashion. The problem with this is loading an image and trying to do stuff straight away with it seems to sometimes mean the image isn’t loaded in time.
A common problem this causes and it certainly caused me was that from this code:
var source = new BitmapImage(new Uri(imagePath, UriKind.Relative)); bitmap = new WriteableBitmap(source);
I kept getting an Exception with the message “Invalid Pointer”. This irritating message seems to be that the Bitmap named ‘source’ hasn’t loaded the actual Bitmap data when the WriteableBitmap tries to use it.
The common suggestion was to set the CreateOptions on the Bitmap to None before you “use it”. However the actual fact is you have to set it before you set the source. In my code the source was being passed in as a Uri to the constructor. Therefore I figured I’d try setting the options first, then setting the Uri. However I still had the issue ONLY when doing the logic immediately on app startup. The actual solution that works is:
var source = new BitmapImage(); source.CreateOptions = BitmapCreateOptions.None; var imageUri = new Uri(imagePath, UriKind.Relative); System.Windows.Resources.StreamResourceInfo s = Application.GetResourceStream(imageUri); source.SetSource(s.Stream); bitmap = new WriteableBitmap(source);
Passing the stream is the ONLY thing that seems to work reliably.
That got me passed that little issue so I thought I’d post it here as most other places I found information it was for loading the image from Isolated Storage. I’ll post more info on that soon enough.