Search code examples
c#imagemagickmagicknet

How to Change the color of each pixel in a image by using Magick.Net


I'm new to the Magick.Net. I tried to change the color of each pixel in the image but there's no change in the new image. Here's my code. Could anyone tell me what's going on? Thanks a lot.

  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using ImageMagick;
  namespace MagickTutor
  {
   class Program
     {
       static void Main(string[] args)
        {
          MagickImage image = new MagickImage();
          image.Read("C:\\.....\\test1.png");
          foreach (Pixel p in image.GetWritablePixels()) {
              p.SetChannel(0, 65535);
          }
          image.Write("C:\\.....\\test2.png");
        }
      }
  }

Solution

  • You should call the Write method of the WritablePixelCollection class that is returned by image.GetWritablePixels() to make sure the pixels are written to the image.

    You could also do what you are doing in a different way:

    using (MagickImage image = new MagickImage())
    {
      image.Read(@"C:\.....\test1.png");
      image.Evaluate(Channels.Red, EvaluateOperator.Set, Quantum.Max);
      image.Write(@"C:\.....\test2.png");
    }