Manij Rai
Manij Rai

Reputation: 189

Image resizing in Magick.Net

I am trying to resize image using Magick.Net. But the image I compressed has greater size and bitdepth of 32 where as original image has bitdepth of 2.I want to retain or reduce the bitdepth too. Here is my code.

           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();

Upvotes: 7

Views: 12812

Answers (1)

dlemstra
dlemstra

Reputation: 8163

You are getting more than two colors because you are resizing the image. This will add an alpha channel and that will result in a lot of semi-transparent pixels. If you want to go back to 2 colors you should change the ColorType of the image to BiLevel. And setting the format to MagickFormat.Png8 will make sure that your image will be written as a 2-bit png. Below is an example of how you could do that:

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);
}

Upvotes: 7

Related Questions