如何在 Magick.NET 中创建黑白图像

How to create black and white images in Magick.NET

我想创建像黑白图像一样的 TIF、PNG、JPG 和 BMP,通过 https://magick.codeplex.com.

如果我喜欢我的代码,我发现我只能生成黑色的 TIF 以及为什么图像而不是其他类型的图像。

知道如何解决吗?

MagickReadSettings readSettings = new MagickReadSettings()
{
    UseMonochrome = true                    
};

using (MagickImage image = new MagickImage(fileInfo.FullName, readSettings))
{
    image.AddProfile(ColorProfile.SRGB);

    if (Properties.Settings.Default.ImageFileExtentionToConvert.ToLower().Contains("tif"))
    {
        image.CompressionMethod = CompressionMethod.Group4;
        image.ColorSpace = ColorSpace.Gray;

        image.Format = MagickFormat.Tif;
    }
    else if (Properties.Settings.Default.ImageFileExtentionToConvert.ToLower().Contains("png"))
    {
        // image.ColorSpace = ColorSpace.Gray;
        image.Settings.SetDefine(MagickFormat.Png, "compression-strategy", "0");
        image.Settings.SetDefine(MagickFormat.Png, "compression-filter", "0");

        image.Format = MagickFormat.Png;
    }
    else if (Properties.Settings.Default.ImageFileExtentionToConvert.ToLower().Contains("jpg"))
    {
        image.Settings.SetDefine(MagickFormat.Jpg, "compression-strategy", "0");
        image.Settings.SetDefine(MagickFormat.Jpg, "compression-filter", "0");

        image.Format = MagickFormat.Jpg;
    }
    else  
    {
        image.CompressionMethod = CompressionMethod.NoCompression;

        image.Format = MagickFormat.Bmp;
    }

    image.Write(newFileName);
}

我在这里找到了答案https://magick.codeplex.com/discussions/637181

using (MagickImage image = new MagickImage(pathToTiffFile))
{
    image.Threshold(60); // 60 is OK 
    image.Depth = 1;
    image.Write(pathToOutputFile);
}