[英]XNA / Monogame Texture2D to to string
I want to store a small png image in a XML file and Load it back to Texture2D. 我想在XML文件中存储一个小的png图像,然后将其加载回Texture2D。
This is what I'm doing 这就是我在做的
Code for saving 保存代码
I'm writing the data of the Texture2D with the BinaryWriter to a MemoryStream, 我正在使用BinaryWriter将Texture2D的数据写入MemoryStream,
then converting the MemoryStream to an Array. 然后将MemoryStream转换为数组。 I have to Convert the array to a Base64String because you can't save all characters 我必须将数组转换为Base64String,因为您无法保存所有字符
in a XML file. 在XML文件中。
The string is saved in my XML file. 该字符串保存在我的XML文件中。
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. 同样在这里。我正在将Base64Str转换为数组并尝试读取它。
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. 在这里可能需要一些帮助。
Getting an exception in the reading method that the value couldnt read. 在读取方法中获取值无法读取的异常。 in line 排队
var width = reader.ReadInt32();
Just create a MemoryStream
directly from your byte array, no need to fill it with a writer: 只需直接从您的字节数组创建MemoryStream
,而无需使用writer来填充它:
byte[] arr = Convert.FromBase64String(gfxdata);
using (var ms = new MemoryStream(arr))
using (var reader = new BinaryReader(ms))
{
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);
}
// Allocate the Texture2D as before.
}
(The specific problem with your code is that you did not rewind your input
stream after writing to it.) (代码的特定问题是在写入流后没有回退input
流。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.