Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
like image 755
Manij Rai Avatar asked Nov 26 '16 10:11

Manij Rai


People also ask

How to resize image in magick image command?

What would be the equivalent in Magick.net That command would translate to this: using ( MagickImage image = new MagickImage ( sourceImg )) { image. Resize (390, 0); image. Crop ( 390, 590 ); image. Write ( sourceImg ); } Sorry, something went wrong. Ok. thanks.

How do I resize an image to a specific size?

To resize an image to specific dimensions, use the convert command with an input file, the -resize parameter, your preferred dimensions, and an output filename: This won’t actually resize the image to the exact dimensions specified. What it will do is resize the image to fit within those dimensions.

What is ImageMagick net?

Magick.NET is the .NET wrapper for the popular ImageMagick library. ImageMagick is an open-source, cross-platform library that focuses on image quality, and on offering a very wide choice of supported image formats.

What is the difference between Mogrify and convert in ImageMagick?

mogrify can also operate on an entire directory of files at once, unlike convert. In general, both ImageMagick commands use similar syntax, but convert only works with a single specified input and output. To edit an entire directory of images and resize them all in the same way, you can pass mogrify a * wildcard:


1 Answers

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);
}
like image 176
dlemstra Avatar answered Sep 19 '22 09:09

dlemstra