0

Is there any way to know if an EMF file is only black-white or has some color ?. You can check the color palette or otherwise ?. Someone who can help me with some code please? I need to know the percentage of colors of each file generated by a program, but only if it is not monochromatic, and do not want to make a pixel by pixel count of all files. Thank You

sudheeshix
  • 1,541
  • 2
  • 17
  • 28

1 Answers1

0

If you are using WinForms, this is easy to do:

First, load the EMF file into a bitmap:

Image img = new Metafile(@"MyFile.emf");
var bmp = new Bitmap(img);

Then you can test if its color by using this function:

bool isColor = IsColor(bmp);

private bool IsColor(Bitmap bmp)
{
    for (int x = 0; x < bmp.Width - 1; x++)
    {
        for (int y = 0; y < bmp.Height - 1; y++)
        {
            Color c = bmp.GetPixel(x, y);
            if (!(c.R == c.B && c.R == c.G))
                return true;
        }
    }

    return false;
}

Basically its testing each pixel. If a file is grayscale or monochrome, then the RGB values of a given pixel would be set to the same value. If any of the pixels don't have equal RGB values, then it must be color.

Icemanind
  • 47,519
  • 50
  • 171
  • 296