7

我正在尝试使用 Magick.Net 调整图像大小。但是我压缩的图像尺寸更大,位深度为 32,而原始图像的位深度为 2。我也想保留或减少位深度。这是我的代码。

           var imageMacig = new MagickImage(filePath);
            //Percentage p = new Percentage(60);
            //imageMacig.Threshold(p); // 60 is OK 
            imageMacig.VirtualPixelMethod = VirtualPixelMethod.Transparent;
            imageMacig.Depth = 1;
            imageMacig.FilterType = FilterType.Quadratic;
            imageMacig.Transparent(MagickColor.FromRgb(0,0,0));
            imageMacig.Format = MagickFormat.Png00;
            imageMacig.Resize(newWidth, newHeight);
            imageMacig.Write(targetPath);
            imageMacig.Dispose();
            originalBMP.Dispose();
4

1 回答 1

6

因为您正在调整图像大小,所以您获得的颜色不止两种。这将添加一个 Alpha 通道,这将导致大量的半透明像素。如果要返回 2 种颜色,则应将图像的 ColorType 更改为 BiLevel。并将格式设置为 MagickFormat.Png8 将确保您的图像将被写入 2 位 png。以下是您如何做到这一点的示例:

using (var imageMagick = new MagickImage(filePath))
{
  imageMagick.Transparent(MagickColor.FromRgb(0,0,0));
  imageMagick.FilterType = FilterType.Quadratic;
  imageMagick.Resize(newWidth, newHeight);
  imageMagick.ColorType = ColorType.Bilevel;
  imageMagick.Format = MagickFormat.Png8;
  imageMagick.Write(targetPath);
}
于 2016-12-09T21:56:01.387 回答