Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Change Pixel Color of an Image in C#.NET

Tags:

I am working with Images in Java, I have designed more over 100+ images(.png) format, They were all Trasparent and Black Color Drawing.

The problem is, Now I have been asked to change the color of the Drawing (Black -to ).

I have searched many code snipped at google,that changes the Bitmap (pixels) of the Image, but i am not guessing what i have to do to match the exact pixel and replace specially when the images if in Transparent mode. Below is the code in .Net (C#)

        Bitmap newBitmap = new Bitmap(scrBitmap.Width, scrBitmap.Height);         for (int i = 0; i < scrBitmap.Width; i++)         {             for (int j = 0; j < scrBitmap.Height; j++)             {                                     originalColor = scrBitmap.GetPixel(i, j);                 if(originalColor = Color.Black)                   newBitmap.SetPixel(i, j, Color.Red);             }         }                     return newBitmap; 

but it was not matching at all, I debugged it, throughout the file, there was no value of Red,Green,Blue parameters of Color (originalColor) variable.

Anybody can help?

like image 679
Bibi Tahira Avatar asked Jun 20 '13 07:06

Bibi Tahira


People also ask

How do I change pixels from one color to another?

Start by going to Image > Adjustments > Replace Color. Tap in the image to select the color to replace — I always begin with the purest part of the color. Fuzziness sets the tolerance of the Replace Color mask. Set the hue you're changing to with the Hue, Saturation, and Lightness sliders.

What are the 3 colors that make up a pixel?

Each pixel on a computer screen is composed of three small dots of compounds called phosphors surrounded by a black mask. The phosphors emit light when struck by the electron beams produced by the electron guns at the rear of the tube. The three separate phosphors produce red, green, and blue light, respectively.

How is color data stored in pixels?

Every one of the squares is a pixel. To store the picture, the computer simply records a number to represent the colour of each square. The more squares in the grid, the better the images will look. It works a bit like a digital colour by numbers.


1 Answers

Here is the Solution I have done with Pixels.

Attaching the source code so one can try the exact and get the result.

I have sample images of 128x128 (Width x Height).

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; using System.IO; //using System.Globalization;  namespace colorchange {    class Program    {       static void Main(string[] args)       {           try           {               Bitmap bmp = null;               //The Source Directory in debug\bin\Big\               string[] files = Directory.GetFiles("Big\\");               foreach (string filename in files)               {                  bmp = (Bitmap)Image.FromFile(filename);                                      bmp = ChangeColor(bmp);                  string[] spliter = filename.Split('\\');                  //Destination Directory debug\bin\BigGreen\                  bmp.Save("BigGreen\\" + spliter[1]);               }                                                             }            catch (System.Exception ex)            {               Console.WriteLine(ex.ToString());            }                    }                public static Bitmap ChangeColor(Bitmap scrBitmap)        {           //You can change your new color here. Red,Green,LawnGreen any..           Color newColor = Color.Red;           Color actualColor;                       //make an empty bitmap the same size as scrBitmap           Bitmap newBitmap = new Bitmap(scrBitmap.Width, scrBitmap.Height);           for (int i = 0; i < scrBitmap.Width; i++)           {              for (int j = 0; j < scrBitmap.Height; j++)              {                 //get the pixel from the scrBitmap image                 actualColor = scrBitmap.GetPixel(i, j);                 // > 150 because.. Images edges can be of low pixel colr. if we set all pixel color to new then there will be no smoothness left.                 if (actualColor.A > 150)                     newBitmap.SetPixel(i, j, newColor);                 else                     newBitmap.SetPixel(i, j, actualColor);              }           }                       return newBitmap;        }    } } 

//Below is the sample image and different results by applying different color enter image description here

Code modifications will be highly appreciated.

like image 96
DareDevil Avatar answered Sep 18 '22 12:09

DareDevil