Create 8-bit depth BMP

Magick.NET is an object-oriented C# interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick.NET
Post Reply
dialecticus
Posts: 3
Joined: 2019-09-23T03:17:03-07:00
Authentication code: 1152

Create 8-bit depth BMP

Post by dialecticus » 2019-09-23T03:26:25-07:00

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?

snibgo
Posts: 12157
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Create 8-bit depth BMP

Post by snibgo » 2019-09-23T03:57:01-07:00

dialecticus wrote:I want to create a BMP file ...
Which part of your code writes a file?
snibgo's IM pages: im.snibgo.com

dialecticus
Posts: 3
Joined: 2019-09-23T03:17:03-07:00
Authentication code: 1152

Re: Create 8-bit depth BMP

Post by dialecticus » 2019-09-23T04:29:45-07:00

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.

dialecticus
Posts: 3
Joined: 2019-09-23T03:17:03-07:00
Authentication code: 1152

Re: Create 8-bit depth BMP

Post by dialecticus » 2019-09-24T01:38:07-07:00

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;

snibgo
Posts: 12157
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Create 8-bit depth BMP

Post by snibgo » 2019-09-24T03:43:20-07:00

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.
snibgo's IM pages: im.snibgo.com

Post Reply