[英]How can I create a 16-bit grayscale PNG in C#?
我正在尝试为“城市:天际线”生成高度图,并将其导入为16位灰度PNG。
我使用PixelFormat.Format16bppGrayScale
创建了一个Bitmap
,但是尝试通过Bitmap.Save
保存它,将其传递给I mageFormat.Png
,结果导致了ExternalException
与
“其他信息:GDI +中发生一般错误。”
此外,从这些16位灰度PNG文件之一加载Bitmap
Format32bppArgb
文件打开为Format32bppArgb
,我假设会静默删除一半数据。
如何使用.Net / C#中的16位灰度PNG图像?
更新: Magick.NET-Q16-AnyCPU已移至https://github.com/dlemstra/Magick.NET 。
适用于Magick.NET-Q16-AnyCPU https://magick.codeplex.com/的NuGet程序包运行良好。 这是我的代码:
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));
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.