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());
}
}
}