0

I have the following code:

  List<Color> colors = new List<Color>();
  colors.Add(Colors.Red);
  colors.Add(Colors.Yellow);

  BitmapPalette palette = new BitmapPalette(colors);


  var bitmap = new WriteableBitmap( (int)width,
                                    (int)height,
                                    96,
                                    96,
                                    PixelFormats.Bgr32,
                                    palette);

The proble is that bitmap.Palette property is null. Why? How can I fix this problem?

Edit:

I set the pixel fortam as PixelFormats.Indexed8, but I am unable to change the pixel value in order to set a specific index form my palette. For example:

        int column = ...;
        int row = ...;

        // Reserve the back buffer for updates.
        bitmap.Lock();

        unsafe 
        {
            // Get a pointer to the back buffer.
            int pBackBuffer = (int)bitmap.BackBuffer;

            // Find the address of the pixel to draw.
            pBackBuffer += row * bitmap.BackBufferStride;
            pBackBuffer += column * 4;

            // Compute the pixel's color.
            int color_data = ???;

            // Assign the color data to the pixel.
            *((int*)pBackBuffer) = color_data;
        }



        // Specify the area of the bitmap that changed.
        bitmap.AddDirtyRect(new Int32Rect(column, row, 1, 1));

        // Release the back buffer and make it available for display.
        bitmap.Unlock();

What value have I to set to color_data in order to set the pixel color to the second palet color (that is, yellow in this case)?

Noam M
  • 3,156
  • 5
  • 26
  • 41
Nick
  • 10,309
  • 21
  • 97
  • 201
  • The `Bgr32` format does not use a palette, because each pixel's color value is directly encoded as a tuple of four bytes, one for blue, one for green, one for red, and one unused. See [PixelFormats](https://msdn.microsoft.com/en-us/library/system.windows.media.pixelformats.aspx) for the details of all possible formats. Only the `Indexed*` formats use a palette. – Clemens Jun 01 '15 at 09:38
  • @Clemens and how shoul I change the pixel value of my bitmap in order to select the proper palet index? Please write your answer. – Nick Jun 01 '15 at 10:44
  • Note sure what you mean. There is no palette index in a Bgr32 bitmap. To set pixel values, use one of the WritePixels overloads of the WriteableBitmap. – Clemens Jun 01 '15 at 11:22
  • @Clemens please see my edit. – Nick Jun 01 '15 at 11:34
  • 1
    The code in the question doesn't set it as Indexed8, it sets it as Bgr32, and thus there is no palette. "Indexed8" implies that each pixel is 1 byte, looking up the color values from the palette, but even your code seems to indicate you're using 32-bit pixels, `pBackBuffer += column * 4;`. Please double-check whether you're actually setting `Indexed8` – Lasse V. Karlsen Jun 01 '15 at 11:51
  • The constructor, you call it with `PixelFormat.Bgr32`, call it with `PixelFormat.Indexed8` instead. – Lasse V. Karlsen Jun 01 '15 at 11:53
  • And if you actually have set `Indexed8`, note that there is one byte per pixel, whose value is an index into the palette. You should hence treat the BackBuffer as `byte*`, and set the color values `0` or `1`, as you seem to only have two colors in your palette. – Clemens Jun 01 '15 at 11:57
  • Thanks, all your hints work properly. You can write your answers. – Nick Jun 01 '15 at 12:06

0 Answers0