none
Silverlight 5 Texture2D.GetData() does not exist

    General discussion

  •            I want to obtain the data array of a texture as follows:

                // Create the scene
                scene = new Scene(myDrawingSurface);

                GraphicsDevice graphicsDevice = GraphicsDeviceManager.Current.GraphicsDevice;
                graphicsDevice = scene.GraphicsDevice;

                Texture2D polygonTexture = scene.ContentManager.Load<Texture2D>("object");
                uint[] data = new uint[polygonTexture.Width * polygonTexture.Height];

                However, Silverlight 5 does not include the xna Texture2D.GetData() method :

                -> -> polygonTexture.GetData(data);     // GetData() does not exist in Xna Silverlight 5

                Does anybody has an idea about an alternate way of getting the data array?

    Friday, December 23, 2011 12:20 PM

All replies

  • I just finished my blog tutorial on solving this issue.  I did it by using the Silverlight Toolkit and extending the content pipeline to get the raw image data, then using a WritableBitmap to get all of the color data.  My utility class with the color data also has a ToTexture2D method which should return a Texture2D object from the loaded color data.  This way you can set the image you need color data for to use my content importer and still pull a texture2d from it.  Here's the link:

    http://www.dustinhorne.com/post/2011/12/28/Getting-Color-Data-for-a-Texture2D-in-Silverlight-5-XNA.aspx

    Wednesday, December 28, 2011 12:11 PM
  • check out below links it might help

    http://forums.create.msdn.com/forums/p/75709/460541.aspx

    http://stackoverflow.com/questions/3659714/has-a-cloth-simulation-ever-been-done-in-wpf-or-silverlight

    Wednesday, December 28, 2011 12:46 PM
  • Hi Dustin,

    Thanks for your answer and explanation.

    I have adapted your ideas to get the data array of a xna-silverlight 5 Texture2D as follows:         

    // Create the scene

    scene = new Scene(myDrawingSurface);

    GraphicsDevice graphicsDevice = GraphicsDeviceManager.Current.GraphicsDevice;

    graphicsDevice = scene.GraphicsDevice;

    Texture2D polygonTexture = scene.ContentManager.Load<Texture2D>("object");

    uint[] data = new uint[polygonTexture.Width * polygonTexture.Height];

    GetData(data, "/object.png");    // see GetData() below:

    //  include using System.IO and using System.Windows.Media.Imaging;

    public void GetData(uint[] data, string sImageFilePath)

    {

    var totalBytes = 0;

    byte[] bytes;

    FileStream stream = File.OpenRead(sImageFilePath);

     bytes = new byte[stream.Length];

    while (totalBytes < stream.Length)

    {

    long remainingBytes = stream.Length - totalBytes;

    int count = remainingBytes > (long)256 ? 256 : (int)remainingBytes;

    int readBytes = stream.Read(bytes, totalBytes, count);

     totalBytes += readBytes;

    }

    WriteableBitmap writeableBitmap = new WriteableBitmap(0, 0);

     writeableBitmap.SetSource(new MemoryStream(bytes));

     int[] pixelData = writeableBitmap.Pixels;

    for (int k = 0; k < pixelData.Length; k++)

    {         

    int pixel = pixelData[k];

    int red = (pixel) & 0xFF;         

    int green = (pixel >> 8) & 0xFF;

    int blue = (pixel >> 16) & 0xFF;

    int alpha = (pixel >> 24) & 0xFF;

    data[k] = new Color(red, green, blue, alpha).PackedValue;

     }

     }

    Now, data is filled with the same data obtained via the xna-only method Texture2D.GetData(data)

    Thanks again for your help

    Thursday, December 29, 2011 2:40 PM
  • You're very welcome, and yep that should work the way you're doing it as well.  The reason I extended the content pipeline was to get all the niceities of having the file packed as .xnb in the content project.  One thing to note... you're loading that image twice.  You actually could do what I did in my ToTexture2D method and create a WritableBitmap from the byte data first and then use CopyTo to push that WB onto a Texture2D.  That would save you some Disk IO.

    Friday, December 30, 2011 9:53 AM