Error: Cannot create WriteableBitmap

Feb 15, 2011 at 8:35 PM

Hey there.

WP7BarcodeManager.ScanBarcode(new Uri("icons/Sample_Email.png", UriKind.Relative), BarcodeResults_Finished);

WP7BarcodeManager.ScanBarcode(new BitmapImage(new Uri("icons/Sample_Email.png", UriKind.Relative)), BarcodeResults_Finished);

The first overloaded method gives me an InvalidOperationException (Error: Cannot create WriteableBitmap).

The second overload gives me a NullReferenceException (BarcodeImage must be set before you can call SetupBarcodeImages).

 

Am I missing something?

Feb 16, 2011 at 1:56 PM

What I did to fix this was create the following method which sends in a stream of the image:

        public static void ScanBarcode(Stream stream, Action<BarcodeCaptureResult> Finished_Processing)
        {
            aFinished = Finished_Processing; //Store return callback
            LastCaptureResults = new BarcodeCaptureResult(stream);
            ThreadPool.QueueUserWorkItem(func => ProcessImage());
        }

 

And to call this method:

var streamResourceInfo = Application.GetResourceStream(new Uri("PhoneApp1;component/icons/Sample_Email.png", UriKind.Relative));
            WP7BarcodeManager.ScanBarcode(streamResourceInfo.Stream, BarcodeResults_Finished);

Coordinator
Feb 27, 2011 at 7:56 PM
Edited Feb 27, 2011 at 7:59 PM
WP7BarcodeManager.ScanBarcode(new Uri("icons/Sample_Email.png", UriKind.Relative), BarcodeResults_Finished);

This method will create a new BitmapImage for the given URI. BitmapImages can only be created from the UI thread, so need to make sure you may need to schedule it using the dispatcher. I'll look at adding a an auto-fix for this in the next release.


WP7BarcodeManager.ScanBarcode(new BitmapImage(new Uri("icons/Sample_Email.png", UriKind.Relative)), BarcodeResults_Finished);

This method requires that the bitmap image has already been initialized. Try using this before passing image into the method:

var bi = new BitmapImage(UriBarcode);
bi.CreateOptions = BitmapCreateOptions.None;

Glad you found a way to get it to work. The next version has a ScanBarcodeSync method that takes a BitmapImage stream and returns the results synchronously instead of asynchronously. This was added as part of a testing framework that is still being developed.