0

我有以下代码:

  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);

问题是bitmap.Palette属性为空。为什么?我该如何解决这个问题?

编辑:

我将像素格式设置为PixelFormats.Indexed8,但我无法更改像素值以便在我的调色板中设置特定索引。例如:

        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();

color_data为了将像素颜色设置为第二种调色板颜色(在这种情况下为黄色),我需要设置什么值?

4

0 回答 0