GetPixel throws an AccessViolationException

Apr 12, 2013 at 12:20 AM
Hi, I am quite new to WriteableBitmapEx. I tried to load a png file in a WinRT Project by using a WriteableBitmap object and WriteableBitmapEx included via nuget.
The Image is http://commons.wikimedia.org/wiki/File:Plan.cathedrale.Amiens.png

My code is
        async void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            WriteableBitmap image = new WriteableBitmap(100, 100);

            StorageFile file = await Package.Current.InstalledLocation.GetFileAsync("Assets\\Plan_cathedrale_Amiens.png");

            IRandomAccessStreamWithContentType randomAccessStream = await file.OpenReadAsync();
            image.SetSource(randomAccessStream);
            this.Transform(image);
        }
        private void Transform(WriteableBitmap source)
        {
            Color bottomleft = source.GetPixel(5, 5);
        }
The line Color bottomleft = source.GetPixel(5, 5); is the one goind bad and throwing an AccessViolationException.

I would very much appreciate any help.

Best Regards,
Andreas
Coordinator
Apr 12, 2013 at 8:47 AM
It's simply not loaded at the time you want to access the pixels. Just check the PixelWidth and Height properties.
You should use the WBX methods to load your stuff:
Uri imageUri = new Uri(BaseUri, "///Assets/Plan_cathedrale_Amiens.png");
image = await BitmapFactory.New(1, 1).FromContent(imageUri);
  • Rene