Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
bitmap.Pixels[matrixPos] generates a "WriteableBitmap has protected content. Pixel access is not allowed." error at runtime. I am not getting byte array. Help please.

C#
 WriteableBitmap bitmap;
//screenGrab is the Image
screenGrab.Source = null;
           var aboutTheShowPage = ((App)Application.Current).RootVisual as Grid;
           var aboutTheShowMainPage = aboutTheShowPage.Children[0] as MainPage;
           var wShowListSubMenuExaboutTheShowPage = aboutTheShowMainPage.viewbox.Child as WShowListSubMenuEx;

           this.bitmap = new WriteableBitmap((int)wShowListSubMenuExaboutTheShowPage.ActualWidth, (int)wShowListSubMenuExaboutTheShowPage.ActualHeight);
           bitmap.Render(wShowListSubMenuExaboutTheShowPage, this.transform);
           bitmap.Invalidate();



           // set the source of our transition image to the WriteableBitmap
           this.screenGrab.Source = bitmap;
           long matrixSize = bitmap.PixelWidth * bitmap.PixelHeight;

            long byteSize = matrixSize * 4 + 4;

            byte[] retVal = new byte[byteSize];

            long bufferPos = 0;

            retVal[bufferPos++] = (byte)((bitmap.PixelWidth / 256) & 0xff);
            retVal[bufferPos++] = (byte)((bitmap.PixelWidth % 256) & 0xff);
            retVal[bufferPos++] = (byte)((bitmap.PixelHeight / 256) & 0xff);
            retVal[bufferPos++] = (byte)((bitmap.PixelHeight % 256) & 0xff);

            for (int matrixPos = 0; matrixPos < matrixSize; matrixPos++)
            {
                retVal[bufferPos++] = (byte)((bitmap.Pixels[matrixPos] >> 24) & 0xff);
                retVal[bufferPos++] = (byte)((bitmap.Pixels[matrixPos] >> 16) & 0xff);
                retVal[bufferPos++] = (byte)((bitmap.Pixels[matrixPos] >> 8) & 0xff);
                retVal[bufferPos++] = (byte)((bitmap.Pixels[matrixPos]) & 0xff);
            }
Posted
Updated 30-May-11 6:33am
v3

1 solution

Here a fix/solution for you

WriteableBitmap with MediaElement[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 30-May-11 15:30pm    
Frankly, I did not get why it could be the problem to save one (as WritableBitmap is ImageSource), but the linked discussion does explain how to work with it, my 5.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900