• Advertisement
Sign in to follow this  

XNA Texture2D to string

This topic is 991 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

 

I want to store a small png image in a XML file and Load it back to Texture2D.

 

This is what I'm doing

 

 

 

Code for saving

I'm writing the data of the Texture2D with the BinaryWriter to a MemoryStream,

then converting the MemoryStream to an Array. I have to Convert the array to a Base64String because you can't save all characters

in a XML file.

The string is saved in my XML file.

        public static string SaveTextureData(this Texture2D texture)
        {
            int width = texture.Width;
            int height = texture.Height;
            Color[] data = new Color[width * height];
            texture.GetData<Color>(data, 0, data.Length);

            MemoryStream streamOut = new MemoryStream();

            BinaryWriter writer = new BinaryWriter(streamOut);
            
               writer.Write(width);
                writer.Write(height);
                writer.Write(data.Length);

                for (int i = 0; i < data.Length; i++)
                {
                    writer.Write(data[i].R);
                    writer.Write(data[i].G);
                    writer.Write(data[i].B);
                    writer.Write(data[i].A);
                } 
            
            return Convert.ToBase64String(streamOut.ToArray());
        }

Code for Loading

Same here.. I'm converting the Base64Str to an array and trying to read it.

But I cant read it back.

        public static Texture2D LoadTextureData(this string gfxdata, GraphicsDevice gfxdev)
        {
            byte[] arr = Convert.FromBase64String(gfxdata);

            MemoryStream input = new MemoryStream();
            
            BinaryWriter bw = new BinaryWriter(input);
            bw.Write(arr);

            using (BinaryReader reader = new BinaryReader(input))
            {
                var width = reader.ReadInt32();
                var height = reader.ReadInt32();
                var length = reader.ReadInt32();
                var data = new Color[length];

                for (int i = 0; i < data.Length; i++)
                {
                    var r = reader.ReadByte();
                    var g = reader.ReadByte();
                    var b = reader.ReadByte();
                    var a = reader.ReadByte();
                    data[i] = new Color(r, g, b, a);
                }

                var texture = new Texture2D(gfxdev, width, height);
                texture.SetData<Color>(data, 0, data.Length);
                return texture;
            }
        }

Could need some help here.

Share this post


Link to post
Share on other sites
Advertisement

Have you tried to debug the converted values?

Their are identical to the starting ones? (Width, height and length?)

If no, check which Write method is used.

If yes, maybe the image is in a strange format, that corrupt the data.

There a way to check if image is a RGB or a RGBA image?

I remember there are different image formats for textures (sorry for the answer, but i used xna years ago, and i don't remember all)...

Share this post


Link to post
Share on other sites

What do you mean by "can't read that"? Does it throw an exception? Does it produce garbage data? What?

 

Are you absolutely positive the source texture is in Color format?

 

Did you try writing code that saves to the stream, then loads from the stream immediately again, and verifies width/height/length and all the Color values are the same? Should be pretty easy to do...

Share this post


Link to post
Share on other sites

I tried to read and write directly from stream but getting only half images.

 

Yes it throws an exception if I try to read it from file. at this line   var width = reader.ReadInt32();.

        public static Texture2D testCode(this Texture2D texture, GraphicsDevice gfxDev)
        {
            int width = texture.Width;
            int height = texture.Height;
            Color[] data = new Color[width * height];
            texture.GetData<Color>(data, 0, data.Length);

            MemoryStream streamOut = new MemoryStream();

            BinaryWriter writer = new BinaryWriter(streamOut);

            writer.Write(width);
            writer.Write(height);
            writer.Write(data.Length);

            for (int i = 0; i < data.Length; i++)
            {
                writer.Write(data[i].R);
                writer.Write(data[i].G);
                writer.Write(data[i].B);
                writer.Write(data[i].A);
            }
            //Write end

            streamOut.Position = 0;
            using (BinaryReader reader = new BinaryReader(streamOut))
            {
                var width2 = reader.ReadInt32();
                var height2 = reader.ReadInt32();
                var length2 = reader.ReadInt32();
                var data2 = new Color[length2];

                for (int i = 0; i < data2.Length; i++)
                {
                    var r = reader.ReadByte();
                    var g = reader.ReadByte();
                    var b = reader.ReadByte();
                    var a = reader.ReadByte();
                    data2[i] = new Color(r, g, b, a);
                }

                var texture2 = new Texture2D(gfxDev, width2, height2);
                texture.SetData<Color>(data2, 0, data2.Length);
                return texture2;
            }
        }

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement