This project has moved and is read-only. For the latest updates, please go here.

How to Change the color of each pixel in a image

Apr 25, 2015 at 11:56 PM
Hi everyone, 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");
        }
    }
}
Apr 26, 2015 at 10:39 AM
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");
}
May 15, 2015 at 6:24 PM
Hi,
I also need to change pixel by pixel value, but applying the Write method to the WritablePixelCollection does not work. By that, i mean the final picture is the same as the original.

Picking up the KevinlsCoding example:
class Program
{
    static void Main(string[] args)
    {
        MagickImage image = new MagickImage();
        image.Read("C:\\.....\\test1.png");
        WritablePixelCollection writablePixels = image.GetWritablePixels();
        foreach (Pixel p in writablePixels) {
            p.SetChannel(0, <some random number between 0 and max of ushort>);
        }
        writablePixels.Write();
        image.Write("C:\\.....\\test2.png"); //this gives the same image as the original with no transformation on pixel color.
    }
}
May 15, 2015 at 7:44 PM
Edited May 15, 2015 at 7:45 PM
I did not notice it in the previous post but you are only modifying the pixel and not the pixel of the image. The pixel has no reference to the WritablePixelCollection. This choice was made because you are allowed to modify the X and Y position of the pixel. I should probably change that so you can modify the pixel without having to call writeablePixel.Set. You should do this for now:
using (WritablePixelCollection writablePixels = image.GetWritablePixels())
{
  foreach (Pixel p in writablePixels)
  {
    p.SetChannel(0, <some random number between 0 and max of ushort>);
    writablePixels.Set(p);
  }
  writablePixels.Write();
}
May 15, 2015 at 8:40 PM
Awesome! Clear as water. Many Thanks for your quick reply!
May 15, 2015 at 9:41 PM
I just committed a patch so that you won't need to call 'writablePixels.Set' in the next release of Magick.NET.