0

I am trying to create a 3x1 bitmap with a specified palette. For testing I used 3 colors: red, green and blue. When I run my code all I get is all red (which is the first color).

This is my code

public void CreateTestImg()
{
    // List to store colors for palette
    List<Color> Colors = new List<Color>();
    // Adds Red to the list
    Colors.Add(Media.Color.FromArgb(255, 255, 0, 0));
    // Adds Green to the list
    Colors.Add(Media.Color.FromArgb(255, 0, 255, 0));
    // Adds Blue to the list
    Colors.Add(Media.Color.FromArgb(255, 0, 0, 255));
    // Create a new palette with the list of colors as input
    BitmapPalette PLT = new BitmapPalette(Colors);
    // Make a Bitmap with the specified width, height, dpix, dpiy, pixelformat, palette, PixelData, stride.
    BitmapSource wb = BitmapSource.Create(3, 1, 96, 96, PixelFormats.Indexed8, PLT, {0, 1, 2}, 3);
    // Freezes the object
    wb.Freeze();
    // Creates a new brush from the Bitmap so I can draw with it later
    ImageBrush newBMB = new ImageBrush(wb);
    // Freezes the brush
    newBMB.Freeze();
    // Adds brush to dictionary for later referencing
    ImageBatch.Add("WHATEVER", newBMB);
}
TizzyT455
  • 3
  • 3
  • PixelFormats.Indexed8 refers to a 8bit or 256 colour palette. Your colours are 32bit? – Kris Jan 05 '16 at 04:10
  • @Kris isn't it suppose to store 256 colors in the palette and the pixel data references the color in the palette by index? That is what I understood from the doc: https://msdn.microsoft.com/en-us/library/system.windows.media.pixelformats.indexed8%28v=vs.110%29.aspx – TizzyT455 Jan 05 '16 at 04:16

1 Answers1

0

you provide int as the pixelsource, and ints are 32bit, correct would be providing bytes:

BitmapSource wb = BitmapSource.Create(3,1, 96, 96, PixelFormats.Indexed8, PLT, new byte[] { 0, 1, 2 }, 3);

greetings from #c#, you should stay longer to receive help ;)

benneh
  • 16
  • 1
  • Thanks a lot this was indeed the solution. Good looking, I was scratching my head all day with this, such a stupid mistake :'( – TizzyT455 Jan 05 '16 at 04:37