我正试图为城市绘制一幅高度图:天际线,并将它们作为16位灰度PNG导入。
我用PixelFormat.Format16bppGrayScale创建了一个PixelFormat.Format16bppGrayScale,但是试图通过Bitmap.Save保存它,传入ImageFormat.Png,结果是
“附加信息:GDI+中发生了一个通用错误。”
此外,从这16位灰度PNG文件中加载一个Bitmap会以Format32bppArgb的形式打开该文件,我假设会悄悄地删除一半的数据。
如何处理.Net/C#中的16位灰度PNG图像?
发布于 2016-10-15 17:35:08
更新:Magick.NET-Q16-任意has已迁移到https://github.com/dlemstra/Magick.NET。
Magick.NET-Q16-AnyCPU https://magick.codeplex.com/的https://magick.codeplex.com/包工作得很好。这是我的密码:
namespace heightmap_generator {
internal class Program {
private static int GRID_SIZE = 1081;
private static void Main(string[] args) {
using (MagickImage image = new MagickImage(@"C:\Users\Brent\AppData\Local\Colossal Order\Cities_Skylines\Addons\MapEditor\Heightmaps\benchmark.png")) {
image.Draw(
new DrawableFillColor(new MagickColor(ushort.MaxValue / 2, ushort.MaxValue / 2, ushort.MaxValue / 2)),
new DrawableRectangle(0, 0, GRID_SIZE, GRID_SIZE)
);
var drawables = new List<IDrawable>();
for (var y = 0; y < GRID_SIZE/50; ++y) {
float t = y / (GRID_SIZE / 50.0f);
t = t*t*(3 - 2*t); //cubic hermite spline h01
ushort v = (ushort)(ushort.MaxValue * (5 + (t-1)*0.3) / 10);
if (y == GRID_SIZE/50 - 1) {
v = (ushort) (ushort.MaxValue*0.501);
}
drawables.Add(new DrawableFillColor(new MagickColor(v, v, v)));
for (var x = 0; x < GRID_SIZE; ++x) {
if (x == GRID_SIZE/2) {
var v2 = (ushort) (v + ushort.MaxValue/1024);
drawables.Add(new DrawableFillColor(new MagickColor(v2, v2, v2)));
drawables.Add(new DrawableColor(x, GRID_SIZE/2 - y, PaintMethod.Point));
drawables.Add(new DrawableColor(x, GRID_SIZE/2 + y, PaintMethod.Point));
drawables.Add(new DrawableFillColor(new MagickColor(v, v, v)));
} else {
drawables.Add(new DrawableColor(x, GRID_SIZE/2 - y, PaintMethod.Point));
drawables.Add(new DrawableColor(x, GRID_SIZE/2 + y, PaintMethod.Point));
}
}
}
image.Draw(drawables);
image.Write(new FileStream(@"C:\Users\Brent\AppData\Local\Colossal Order\Cities_Skylines\Addons\MapEditor\Heightmaps\benchmark3.png", FileMode.Create));
}
}
}}
https://stackoverflow.com/questions/39865591
复制相似问题