0

I'm looking to remove all color from WMF image file by only 1 color.

Metafile img = new Metafile(path + strFilename + ".wmf");
float planScale = 0.06615f;
float scale = 1200f / (float)img.Width;
planScale = planScale / scale; ;
float widht = img.Width * scale;
float height = img.Height * scale;
using (var target = new Bitmap((int)widht, (int)height))
{
    using (var g = Graphics.FromImage(target))
    {
        g.DrawImage(img, 0, 0, (int)widht, (int)height);
        target.Save("image.png", ImageFormat.Png);
    }
}

For the moment, I load a WMF file, set the scale and save it as PNG file.

Example of PNG result: enter image description here

But now I need to remove all the colors (green, purple....) and set only 1 color like Gray for example.

  • 1
    try this - since you mentioned gray - https://stackoverflow.com/questions/2265910/convert-an-image-to-grayscale – Ctznkane525 Sep 10 '18 at 15:33
  • 2
    Is that you want a binary image with only white or one specific grey value or shall different colors be mapped to different greyscale values? – Fildor Sep 10 '18 at 16:03
  • If you want a grayscale, think that, in RGB, gray tones are those colors where R = G = B. If you want a binary image (black and white), you need to choose a threshold and set the channels to 0 or 255 (below or above the threshold). – heringer Sep 10 '18 at 16:31

1 Answers1

0

If the background is always white you can do something like that. You can change the 200 to something you want, to adjust the Color that shouldn't be changed. In this example the white color is not changed. If you don't want to draw black, you can adjust the Color at target.SetPixel(x,y,Color.Black);

Metafile img = new Metafile("D:\\Chrysanthemum.wmf");
float planScale = 0.06615f;
float scale = 1200f / (float)img.Width;
planScale = planScale / scale; ;
float widht = img.Width * scale;
float height = img.Height * scale;
using (var target = new Bitmap((int)widht, (int)height))
{
    using (var g = Graphics.FromImage(target))
    {
        g.DrawImage(img, 0, 0, (int)widht, (int)height);
    }

    for (int x = 0; x < target.Width; x++)
    {
        for (int y = 0; y < target.Height; y++)
        {
            Color white = target.GetPixel(x, y);
            if ((int)white.R > 200 || (int)white.G > 200 || (int)white.B > 200)
            {
                target.SetPixel(x, y, Color.Black);
            }
        }
    }

target.Save("D:\\image.png", ImageFormat.Png);
}

WMF Image: enter image description here

PNG Image: enter image description here

I hope that is what you are searching for.

NelDav
  • 785
  • 2
  • 11
  • 30
  • This is painfully slow. Using a ColorMatrix is blindingly fast. And using [LockBits](https://stackoverflow.com/questions/28792723/adding-or-subtracting-color-from-an-image-in-a-picturebox-using-c-sharp/28799612#28799612) gives you both speed and full control. – TaW Sep 10 '18 at 17:29
  • Yes you are right it is very slow. I hadn't worked with a ColorMatix ever before. Please show us how it works with a ColorMatrix. – NelDav Sep 10 '18 at 18:47
  • [Here](https://stackoverflow.com/search?q=user%3A3152130+colormatrix) are a few example posts.. – TaW Sep 10 '18 at 18:50
  • Thank you. Now I have seen that the link above also shows an example. – NelDav Sep 10 '18 at 18:51
  • My background is transparent but it's working correctly, anyway if someone got a better solution with ColoMatrix, I'll be glad to see some examples – Guillaume Brulet Sep 11 '18 at 08:29