c# - 如何在 C# 中创建 16 位灰度 PNG?

标签 c# .net image png

我正在尝试为《城市:天际线》生成高度图,并将其导入为 16 位灰度 PNG。

我使用 PixelFormat.Format16bppGrayScale 创建了一个 Bitmap,但尝试通过 Bitmap.Save 保存它,传入 ImageFormat .Png,导致带有

ExternalException

"Additional information: A generic error occurred in GDI+."

此外,从这些 16 位灰度 PNG 文件之一加载位图 会将文件打开为 Format32bppArgb,我假设会默默地丢弃一半数据。

如何在 .Net/C# 中处理 16 位灰度 PNG 图像?

最佳答案

更新: Magick.NET-Q16-AnyCPU 已移至 https://github.com/dlemstra/Magick.NET .

Magick.NET-Q16-AnyCPU 的 NuGet 包 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));
        }
    }
}

}

关于c# - 如何在 C# 中创建 16 位灰度 PNG?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39865591/

相关文章:

windows - 批处理文件: Processing files in alphbetical (Numerical maybe>) Order

python - 如何在 Pandas 数据框中创建多列?

c# - asp :label text using word-break on only long words? 上的断行

c# - 如何在高级安装程序 (13.3) 自定义操作中加密连接字符串

c# - 命名管道 - 异步窥视

c# - XmlSerializer 不可靠还是我做错了什么?

c# - xPath 通过 XPathNavigator 对象而不是通过表达式求值来查找祖先

c# - 获取编译时错误 CS0579 : Duplicate 'AssemblyFileVersionAttribute' attribute

c# - 如何检索多个属性的值?

javascript - 如何使用具有宽高比的 PHP 获取图像的最小可裁剪尺寸?