WriteableBitmap – Invalid Pointer from Constructor

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.

Advertisements
This entry was posted in Uncategorized, windows phone and tagged , , . Bookmark the permalink.

5 Responses to WriteableBitmap – Invalid Pointer from Constructor

  1. Perry Neal says:

    THIS was the ONLY thing I could find that actually worked! THANK you!

    • ciaranodonnell says:

      Glad I could help. I do most of my WinPhone coding on a bus without internet so it had me stumped for a while.

      • Perry Neal says:

        Now if I can just figure out how to turn the ChosenPhoto from the CameraCaptureTask into a bitmap so I can put some text on it and then save it. Got any pointers?

        And great job on being able to develop without the internet. Without Google/Stack Overflow I can’t do ANYTHING anymore it seems.

        Thanks again.

  2. Dzivo Jelic says:

    Nice helped but i need to download my picture from the internet using webclient and i need to save it later and thats killing me now

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s