title: Strange colors while displaying bitmap with PixelFormat.Format8bppIndexed
body: <p>I would like to ask for help. I want to create an image from array and show it in PictureBox. But image, that was supposed to be in gray scale, is displayed in strange colors. In the past, I have had such a problem, but the methods used until now have failed. Here is my code:</p>
  
  <pre><code>        public static Bitmap BitmapFromArray(int[] pixelValues,int rows, int columns)
      {
          Bitmap bitmap = new Bitmap(rows, columns);
          BitmapData bmpData = bitmap.LockBits(new Rectangle(0, 0, rows, columns), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
  
          byte[] pixelValuesOriginal = new byte[Math.Abs(bmpData.Stride) * bitmap.Height];
  
          //Pobierz wartosc wszystkich punktow obrazu
          System.Runtime.InteropServices.Marshal.Copy(bmpData.Scan0, pixelValuesOriginal, 0, pixelValues.Length);
  
          for (int i = 0; i &lt; pixelValues.Length; i++)
          {
              pixelValuesOriginal[i] = (byte)pixelValues[i];
          }
  
          //Ustaw wartosc wszystkich punktow obrazu
          System.Runtime.InteropServices.Marshal.Copy(pixelValuesOriginal, 0, bmpData.Scan0, pixelValuesOriginal.Length);
  
          bitmap.UnlockBits(bmpData);
  
          return bitmap;
      }    
  </code></pre>
  
  <p>I tried the method that I found here: <a href="https://stackoverflow.com/questions/2593212/editing-8bpp-indexed-bitmaps">Editing 8bpp indexed Bitmaps</a></p>
  
  <pre><code>        ColorPalette pal = bitmap.Palette;
          for (int i = 0; i &lt;= 255; i++)
          {
              pal.Entries[i] = Color.FromArgb(i, i, i);
          }
          bitmap.Palette = pal;
  </code></pre>
  
  <p>But this time it did not work because bitmap.Palette.Entries is an array with 0 elements. </p>
  
  <p>I also tried to change the PixelFormat for PixelFormat.Format24bppRgb and use such a trick:</p>
  
  <pre><code>        for (int i = 0; i &lt; pixelValues.Length; i++)
          {
              for (int j = 0; j &lt; 3; j++)
              {
                  pixelValuesOriginal[i + j] = (byte)pixelValues[i];
              }
          }
  </code></pre>
  
  <p>But then I have three gray scaled pictures side by side.</p>
  
  <p>I will be grateful for hints on what to do with this problem.</p>