0

I need to change the color of all objects available in a metafile into an specific color. However, what is currently provided is just for translating/scaling/rotating colors. For example I need to set the color of all available objects to green.

What is currently discussed below is to just change the colors relatively, not absolutely:

https://learn.microsoft.com/en-us/dotnet/framework/winforms/advanced/recoloring-images

How to change colors of EMF+ image in C#

What this code can do is just translate all the colors to white color. But what I need is to set all the colors to e.g. green, dark red, and so on. Is there any way to do this?

ImageAttributes GetAttr()
{
    /* Get image attributes to translate to white */
    float[][] colorMatrixElements = {
            new float[] {1,  0,  0,  0, 0}, // no red scaling
            new float[] {0,  1,  0,  0, 0}, // no green scaling
            new float[] {0,  0,  1,  0, 0}, // no blue scaling
            new float[] {0,  0,  0,  1, 0}, // no alpha scaling
            new float[] {1, 1, 1, 1, 1}}; // four translations of 1.0
    ImageAttributes imageAttributes = new ImageAttributes();
    imageAttributes.SetColorMatrix(new ColorMatrix(colorMatrixElements));
    return imageAttributes;
}

void test()
{
    var fname = Path.GetTempFileName();
    var frame = new Rectangle(0, 0, 640, 480);
    using (var img = new Metafile(fname, CreateGraphics().GetHdc(), frame, MetafileFrameUnit.Point))
    {
        using (var g = Graphics.FromImage(img))
        {
            var arrow = new Metafile("arrow.emf");
            g.DrawImage(arrow, new Rectangle(150, 400, 75, 50),
                0, 0, arrow.Width, arrow.Height, GraphicsUnit.Pixel, GetAttr());
        }
    }
}
Kamran Kia
  • 347
  • 3
  • 7
  • I suspect one has to scan the file for all objects and treat each of them.. – TaW Sep 16 '19 at 18:09
  • It would be a nice idea, but GDI+ does not support any access to the internal objects of a metafile. – Kamran Kia Sep 16 '19 at 18:52
  • Yes, I guess this will go beyond GDI+ or rather 'before'. There should be some library out there but you can't ask here.. - Did you see [this](https://learn.microsoft.com/en-us/windows/win32/gdi/editing-an-enhanced-metafile) ? – TaW Sep 16 '19 at 20:18
  • The solution seems very complicated, to find, remove and redraw every single object; moreover, the given record must correspond to a GDI drawing function. Since color matrices exists in GDI+, it seems a bit odd that setting color to a constant value is not supported. – Kamran Kia Sep 17 '19 at 18:11

0 Answers0