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

Setting RGB color values of an image

Aug 25, 2014 at 3:41 PM
I am using .NET. I need to replace the RGB values on an image with different values

for example
R:128, G:118 B:118 to R:100, G:105, B:110

Can you please help me to do this or Guide a way

Any help is greatly appreciated
Aug 25, 2014 at 4:53 PM
Edited Aug 25, 2014 at 4:53 PM
I tried the following code but WritablePixelCollection Write() is not updating the image at all
Also i tried writing the image to physical drive again but not working
using (MagickImage image = new MagickImage(@"E:\Development\DOTNET\PCXEditor\RGBEditor\18975253ee.pcx"))

                WritablePixelCollection p = image.GetWritablePixels();
                foreach (Pixel pp in p)

                    Pixel pc = new Pixel(1, 1, 4);

                    Color c = pp.ToColor();
                    if (c.R == 128)
                        //r = 100;

                        pc.SetChannel(0, 100);

                    if (c.G == 118)
                        //g = 105;
                        pc.SetChannel(1, 105);
                    if (c.B == 118)
                        //b = 110;
                        pc.SetChannel(2, 110);


                    //pc = null;

                MessageBox.Show(p.GetPixel(1, 1).ToColor().R.ToString());   // the value has changed already still here its fine
                p.Write(); // this is not working

                  // image.Write(@"E:\Development\DOTNET\PCXEditor\RGBEditor\1897525311-changed.pcx");

Aug 25, 2014 at 6:01 PM
You should use the Opaque method of MagickImage to do this. You could use the WritablePixelcollection but the Opaque method will be much faster. Your code only changes the pixel at 1x1 so that is probably why you think it is not working.

This will get you the result you are expecting:
using (MagickImage image = new MagickImage(@"E:\Development\DOTNET\PCXEditor\RGBEditor\18975253ee.pcx"))
  MagickColor target = new MagickColor(128, 118, 118);
  MagickColor fill = new MagickColor(100, 105, 110);

  image.Opaque(target, fill);

Note: This example only works with the Q8 version of Magick.NET because that version has pixels in the 0-255 range
Aug 27, 2014 at 1:54 PM
That was great. Thanks a ton

I am done with the coding but when i tried to release the product to a different machine. its not working in any machine ,the application breaks before its start
I have installed .NET 4.0: Visual C++ Redistributable for Visual Studio 2012 (x86 or x64)

Do you have any documentation for deployment. Can you please guide me for a proper release
Aug 27, 2014 at 2:01 PM
Are you using the .NET 2.0 or the .NET 4.0 release of Magick.NET? And do you get an exception when the application crashes?
Aug 27, 2014 at 2:05 PM
I have downloaded
Aug 27, 2014 at 2:08 PM
Apart from

Visual C++ Redistributable for Visual Studio
.NET 4.0: Visual C++ Redistributable for Visual Studio 2012 (x86 or x64)
.NET 2.0: Visual C++ Redistributable for Visual Studio 2008 (x86 or x64)
Do i need to register or install anything else.

I have copied my release folder to the client machine
Aug 27, 2014 at 2:39 PM
I am getting the error

Failed to Load embedded Assembly : Could not load file or assembly Magic.NET -Q8 -x86.dll or one or more of its dependencies The specified module could not be found.

But i have referenced Magick.NET.AnyCPU.dll any idea why its showing the error
Aug 27, 2014 at 3:28 PM
You only need to install '.NET 4.0: Visual C++ Redistributable for Visual Studio 2012 (x86 or x64)'. The specified module could not be found error message is probably happening because your server first needs a reboot. Have you rebooted the machine after the installation?
Aug 28, 2014 at 11:55 AM
Everything is fine now. Thanks a ton for your kind support.