Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the color of a pixel use Magick.NET?

Tags:

c#

imagemagick

How to change pixel color on the image use the lib Magick.NET. I use the following code:

MagickImage img = new MagickImage(@"d:\TEST\110706M01000509.jpg");
WritablePixelCollection pc = img.GetWritablePixels(100, 100, 50, 50);

foreach (Pixel p in pc)
{
    p.SetChannel(0, 255);
    p.SetChannel(1, 0);
    p.SetChannel(2, 0);
}

pc.Write();

img.Write(@"d:\TEST\110706M01000509_.jpg");

But on the output image the color of the pixels no red.

Example:

image

Why the color of figures is not red?

like image 718
Ivan Avatar asked Jun 08 '15 11:06

Ivan


2 Answers

In 7.4, the above answers are largely correct, but, the method for getting the pixels is now

magick.GetPixels()

not

magick.GetWritablePixels()

They return IPixelCollection

like image 159
Chris Moschini Avatar answered Oct 02 '22 22:10

Chris Moschini


With the current version of Magick.NET (7.0.0.0014) you will need to do this:

using (WritablePixelCollection pc = img.GetWritablePixels(100, 100, 50, 50))
{
  foreach (Pixel p in pc)
  {
    p.SetChannel(0, 255);
    p.SetChannel(1, 0);
    p.SetChannel(2, 0);
    pc.Set(p) // This will update the PixelCollection
  }
}

With Magick.NET 7.0.0.0015 the call to pc.Set will be done automatically and your example above will work.

like image 21
dlemstra Avatar answered Oct 02 '22 22:10

dlemstra