Page 1 of 1

Create 8-bit depth BMP

Posted: 2019-09-23T03:26:25-07:00
by dialecticus
I asked a question on StackOverflow, but will probably get a faster response here. I want to create a BMP file with 8 bits per pixel. I use file Properties > Details to verify the result. If Bit depth is 8 there then I got my picture. This is code that I have:

Code: Select all

byte[] input = <existing TIFF image>;
using (var image = new MagickImage(input))
{
    image.Grayscale();
    image.ColorType = ColorType.Palette;
    image.Depth = 8;
    image.Quantize(new QuantizeSettings() { Colors = 256,  DitherMethod = DitherMethod.No });

    byte[] result = image.ToByteArray(MagickFormat.Bmp);

    return result;
}
Here I'm converting from existing image, but I also tried to create a new image, and still can't make it 8 bpp bitmap. Is this even possible?

Re: Create 8-bit depth BMP

Posted: 2019-09-23T03:57:01-07:00
by snibgo
dialecticus wrote:I want to create a BMP file ...
Which part of your code writes a file?

Re: Create 8-bit depth BMP

Posted: 2019-09-23T04:29:45-07:00
by dialecticus
I am creating the file, but I do not write it on hard disk. Imagine there is the following line before return:

Code: Select all

System.IO.File.WriteAllBytes(@"...", result)
EDIT: Actually, I do write it, but only to verify that it's 8 bits per pixel. I use WriteAllBytes for this. I'll remove this when I get a proper BMP file in memory.

Re: Create 8-bit depth BMP

Posted: 2019-09-24T01:38:07-07:00
by dialecticus
Got the answer in the other place. Quote from there: "Windows Explorer displays all compressed BMP files as 32-bit contrary to their actual bit depths." Let's call it a bug. If one really wants to see 8 bits depth in file properties they should add the following line before the call to ToByteArray:

Code: Select all

image.Settings.Compression = CompressionMethod.NoCompression;

Re: Create 8-bit depth BMP

Posted: 2019-09-24T03:43:20-07:00
by snibgo
Beware that "depth" might mean bits/channel/pixel, eg 8, or total bits/pixel, which might be 32 when there are four channels.

Software (including IM) should be explicit which definition they use, but rarely is.