title: Why does the number of colors change after saving a bitmap?
body: <p>I'm creating a single-purpose app that would take an image and save it with some alpha channel adjustments. The adjustments are made with a color matrix and are meant to alter only the alpha channel. I want the otput to be indentical to the original in terms of RGB values.<br>
  I'm working with 32-bit PNG images I create using either Photoshop or After Effects. I save/render the files without any compression (at least that's what I'm told). </p>
  
  <p>These are two color matrices that will help me illustrate the issue. </p>
  
  <pre class="lang-cs prettyprint-override"><code>            ColorMatrix harmlessCm = new ColorMatrix(new float[][]{
                  new float[]{1, 0, 0, 0, 0},
                  new float[]{0, 1, 0, 0, 0},
                  new float[]{0, 0, 1, 0, 0},
                  new float[]{0, 0, 0, 1, 0},
                  new float[]{0, 0, 0, 0, 0}});
  
              ColorMatrix luminanceToAlphaCm = new ColorMatrix(new float[][]{
                  new float[]{1, 0, 0, .2125f, 0},
                  new float[]{0, 1, 0, .7154f, 0},
                  new float[]{0, 0, 1, .0721f, 0},
                  new float[]{0, 0, 0, 0, 0},
                  new float[]{0, 0, 0, 0, 0}});
  </code></pre>
  
  <p>Scenario #1. File: 32-bit PNG with no transparent areas, number of colors <strong>5707</strong>.<br>
  Applying the <code>harmlessCm</code> doesn't have any effect on number of colors.<br>
  Applying the <code>luminanceToAlphaCm</code> led to decrease in number of colors: 1052.</p>
  
  <p>Scenario #2. File: 32-bit PNG with multiple semi-tranparent areas (same image but without a background), number of colors <strong>6244</strong>.<br>
  Applying the <code>harmlessCm</code> led to decrease in number of colors: 5990.<br>
  Applying the <code>luminanceToAlphaCm</code> led to decrease in number of colors: 1470.</p>
  
  <p>Full class:</p>
  
  <pre class="lang-cs prettyprint-override"><code>using Microsoft.WindowsAPICodePack.Dialogs;
  using Prism.Commands;
  using System.Drawing;
  using System.Drawing.Imaging;
  using System.IO;
  
  namespace RGBA_Playground.ViewModels
  {
      class MainViewModel : BaseViewModel
      {
          public string Before { get; private set; }
          public Image After { get; private set; } 
  
          public DelegateCommand UploadCommand { get; }
          public DelegateCommand SaveCommand { get; }
  
          public MainViewModel()
          {
              UploadCommand = new DelegateCommand(() =&gt;
              {
                  using (var dialog = new CommonOpenFileDialog())
                  {
                      dialog.Multiselect = false;
  
                      if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
                      {
                          var image = dialog.FileName;
                          Before = image;
                          After = GetChangedImage(image);
                          RaisePropertyChanged("Before");
                          RaisePropertyChanged("After");
                      }
                  }
              });
  
              SaveCommand = new DelegateCommand(() =&gt;
              {
                  var changedImage = GetChangedImage(Before);
  
                  using (Bitmap bm = new Bitmap(changedImage))
                  {
                      bm.Save(Path.GetDirectoryName(Before) + "\\" + Path.GetRandomFileName() + ".png", ImageFormat.Png);
                  }
              });
  
          private static Bitmap GetChangedImage(string path)
          {
              Bitmap original = new Bitmap(path, false);
              Bitmap result = new Bitmap(original.Width, original.Height, PixelFormat.Format32bppArgb);
              result.SetResolution(original.HorizontalResolution, original.VerticalResolution);
  
              ColorMatrix cm = new ColorMatrix(new float[][]{
                  new float[]{1, 0, 0, .2125f, 0},
                  new float[]{0, 1, 0, .7154f, 0},
                  new float[]{0, 0, 1, .0721f, 0},
                  new float[]{0, 0, 0, 0, 0},
                  new float[]{0, 0, 0, 0, 0}});
  
              using (ImageAttributes ia = new ImageAttributes())
              {
                  ia.SetColorMatrix(cm);
  
                  using (Graphics g = Graphics.FromImage(result))
                  {
                      g.DrawImage(original, 
                          new Rectangle(0, 0, original.Width, original.Height),
                          0, 0, 
                          original.Width, 
                          original.Height,
                          GraphicsUnit.Pixel, 
                          ia);
                  }
              }
  
              return result;
          }
      }
  }
  
  </code></pre>
  
  <p>I need the output to have the same number of colors in both scenarios. 
  Any help will be very much appreciated.
  Thanks!</p>
  
  <p>I'm sorry if my initial post was confusing. 
  Another edit: I forgot to roll back some edits before posting here. I'm using another Bitmap constructor now.<br>
  <code>Bitmap result = new Bitmap(original.Width, original.Height, PixelFormat.Format32bppArgb);</code><br>
  The scenarios and results are edited too. </p>