Question

I have now been looking for almost an hour and just couldn't find an answer. I am currently working on a program that displays a custom image (using WritableBitmap) either in a gray scale or a colourscale with 256 predetermined colours.

The input for the image will be an array of bytes (which works fine if I set the PixelFormat property to PixelFormats.Gray8), but since I need a custom colour scale too, I would like to create custom scales (one for gray and one or more for colours). I think what I need to use is the PixelFormat.Indexed8 property, but I simply can't find out how to use that and how to create a custom colour palette and the internet was not at all helpful on how to create one.

Was it helpful?

Solution

You can create a custom BitmapPalette and apply it to a new WriteableBitmap:

var myPalette = new BitmapPalette(new List<Color> 
{ 
    Colors.Red,
    Colors.Blue,
    Colors.Green,
    // ...
});

var writeableBitmap = new WriteableBitmap(
    width, 
    height, 
    96, 
    96, 
    PixelFormats.Indexed8, // Paletted bitmap with 256 colours
    myPalette);

writeableBitmap.WritePixels(...);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top