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

时间:2016-06-27 18:36:37

标签: c# .net imagemagick 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);
}

1 个答案:

答案 0 :(得分:1)

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

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