你好,我在内存中有heightData,有时(当我编辑它时)我想将它保存为jpg。这是我的代码:
float multi = 0.2f;
float[,] heightData = quadTree.HeightData;
Color[] heightMapColors = new Color[heightData.Length];
for (int x = 0; x < heightData.GetLength(0); x++)
{
for (int y = 0; y < heightData.GetLength(1); y++)
{
byte colorData = (byte)(heightData[x, y] / multi);
heightMapColors[x + y * heightData.GetLength(0)].R = colorData;
heightMapColors[x + y * heightData.GetLength(0)].G = colorData;
heightMapColors[x + y * heightData.GetLength(0)].B = colorData;
}
}
Texture2D heightMap = new Texture2D(device, heightData.GetLength(0), heightData.GetLength(1), false, SurfaceFormat.Color);
heightMap.SetData<Color>(heightMapColors);
using (System.IO.Stream stream = System.IO.File.OpenWrite(@"D:\test.jpg"))
{
heightMap.SaveAsJpeg(stream, heightData.GetLength(0), heightData.GetLength(1));
}我100%确定我有heightMapColors格式的数据,但保存的jpg格式只有黑色。:/这是一个好的方法,怎么做,还是有什么问题?
发布于 2012-05-03 04:09:46
Alpha不应为零
heightMapColors[x + y * heightData.GetLength(0)].R = colorData;
heightMapColors[x + y * heightData.GetLength(0)].G = colorData;
heightMapColors[x + y * heightData.GetLength(0)].B = colorData;
heightMapColors[x + y * heightData.GetLength(0)].A = 255;发布于 2012-05-03 04:09:25
JPG可能不是存储高度图的好格式,因为它是一种有损格式。你应该把它放入BMP pr PNG。也就是说,你的“身高”的范围是多少?它看起来你的高度是一个浮点数,这意味着它可能不在正确的范围内显示,甚至没有转换为离散值。
如果您允许的高度范围是Xf到Yf,请将其转换为0- 255范围的using
byteValue =(字节)(OldValue- OldMin) * (255 - 0)) / (OldMax - OldMin)) +0
然后试一试。
https://stackoverflow.com/questions/10419885
复制相似问题