title: C# Normalizing RGB and creating a new image
body: <p>I am trying to create a program that accepts an image, recursively goes through each pixel, normalizes the pixel and re-creates a NEW image that looks the same as the original, but has normalized pixels instead.</p>
  
  <pre><code>    public void parseJpeg(String jpegPath)
      {
          var normalizedRed = 0.0;
          var normalizedGreen = 0.0;
          var normalizedBlue = 0.0;
          Bitmap normalizedImage = null;
  
          var image = new Bitmap(jpegPath);
          normalizedImage = new Bitmap(image.Width, image.Height);
          for (int x = 0; x &lt; image.Width; ++x)
          {
              for (int y = 0; y &lt; image.Height; ++y)
              {
                  Color color = image.GetPixel(x, y);
  
                  double exponent = 2;
                  double redDouble = Convert.ToDouble(color.R);
                  double blueDouble = Convert.ToDouble(color.B);
                  double greenDouble = Convert.ToDouble(color.G);
  
                  double redResult = Math.Pow(redDouble, exponent);
                  double blueResult = Math.Pow(blueDouble, exponent);
                  double greenResult = Math.Pow(greenDouble, exponent);
  
                  double totalResult = redResult + blueResult + greenResult;                    
  
                  normalizedRed = Convert.ToDouble(color.R) / Math.Sqrt(totalResult);
                  normalizedGreen = Convert.ToDouble(color.G) / Math.Sqrt(totalResult);
                  normalizedBlue = Convert.ToDouble(color.B) / Math.Sqrt(totalResult);
  
  
                  Color newCol = Color.FromArgb(Convert.ToInt32(normalizedRed), Convert.ToInt32(normalizedGreen), Convert.ToInt32(normalizedBlue));
  
                  normalizedImage.SetPixel(x, y, newCol);                                                                
              }                
          }
  
          normalizedImage.Save("C:\\Users\\username\\Desktop\\test1.jpeg"); 
          resultsViewBox.AppendText("Process completed.\n");
      }
  </code></pre>
  
  <p>Using the above code produces all black pixels and I do not understand why. When it normalizes it sets RGB = 1. After normalization, how do I set pixels with the NEW normalized value?</p>
  
  <p>When I perform the below code, I get a black and blue image in my preview, but when I open the file it's blank. This is better than what I was getting before, which was ALL black pixels. This only works on one image though. So I am not sure how much of a step forward it is.</p>
  
  <pre><code>public void parseJpeg(String jpegPath)
      {
          Bitmap normalizedImage = null;           
  
          var image = new Bitmap(jpegPath);
          normalizedImage = new Bitmap(image.Width, image.Height);
          for (int x = 0; x &lt; image.Width; ++x)
          {
              for (int y = 0; y &lt; image.Height; ++y)
              {
                  Color color = image.GetPixel(x, y);
  
                  float norm = (float)System.Math.Sqrt(color.R * color.R + color.B * color.B + color.G * color.G);
  
                  Color newCol = Color.FromArgb(Convert.ToInt32(norm));
  
                  normalizedImage.SetPixel(x, y, newCol);
              }
          }
  
          normalizedImage.Save("C:\\Users\\username\\Desktop\\test1.jpeg");
          resultsViewBox.AppendText("Process completed.\n");
      }
  </code></pre>
  
  <p>I found the code for what I was trying to do:
  <a href="http://www.lukehorvat.com/blog/normalizing-image-brightness-in-csharp/" rel="nofollow">http://www.lukehorvat.com/blog/normalizing-image-brightness-in-csharp/</a></p>
  
  <pre><code>    public void parseJpeg(String jpegPath)
      {
          var image = new Bitmap(jpegPath);
          normalizedImage = new Bitmap(image.Width, image.Height);
  
  
          for (int x = 0; x &lt; image.Width; ++x)
          {
              for (int y = 0; y &lt; image.Height; ++y)
              {
                  float pixelBrightness = image.GetPixel(x, y).GetBrightness();
                  minBrightness = Math.Min(minBrightness, pixelBrightness);
                  maxBrightness = Math.Max(maxBrightness, pixelBrightness);
              }
          }
  
          for (int x = 0; x &lt; image.Width; x++)
          {
              for (int y = 0; y &lt; image.Height; y++)
              {
                  Color pixelColor = image.GetPixel(x, y);
                  float normalizedPixelBrightness = (pixelColor.GetBrightness() - minBrightness) / (maxBrightness - minBrightness);
                  Color normalizedPixelColor = ColorConverter.ColorFromAhsb(pixelColor.A, pixelColor.GetHue(), pixelColor.GetSaturation(), normalizedPixelBrightness);
  
                  normalizedImage.SetPixel(x, y, normalizedPixelColor);
              }
          }
  
          normalizedImage.Save("C:\\Users\\username\\Desktop\\test1.jpeg");
          resultsViewBox.AppendText("Process completed.\n");
      }
  </code></pre>