2

I'm trying to develop a special kind of heatmap, where the color of a marker depends on the value of some calculated variables.

What I need to do is change the color of my existing EMF+-Image. The following code works like a charm when using png or wmf files, but DrawImage doesn't draw anything when using an EMF+ file:

//EMF+ image (color = red)
Metafile mf = new Metafile(@"C:\output\redman.emf");

//changes the color of the image from red to green (works with .png, but not with EMF+)
float[][] matrixColTrans = 
{
  new float[] {215.0f / 195.0f, 0, 0, 0, 0}
    , new float[] {0, 240.0f / 45.0f, 0, 0, 0}
    , new float[] {0, 0, 80.0f / 5.0f, 0, 0}
    , new float[] {0, 0, 0, 1, 0}
    , new float[] {0, 0, 0, 0, 1}
};
ColorMatrix colorMatrix = new ColorMatrix(matrixColTrans);

ImageAttributes ia = new ImageAttributes();
ia.SetColorMatrix(colorMatrix);

g.DrawImage(
    mf
    , new Rectangle(80, 0, 20, 50)// destination rectangle 
    , 0, 0 // upper-left corner of source rectangle 
    , mf.Width // width of source rectangle
    , mf.Height // height of source rectangle
    , GraphicsUnit.Pixel
    , ia
);

Same thing happens when using ia.SetRemapTable for example.

Any ideas on how to solve that problem?

Thomas
  • 81
  • 3
  • Your code works fine here. Where did you get the `g` from? And: Where did you get the `emf` file from? I created mine by saving a chart.. Also: your destination rectangle is a bit off and small for my test data. And wrt the source rectangle: In a vector file width and height in pixels are not necessarily reliable values. – TaW May 02 '18 at 18:04
  • hm you seem to be right, my emf-file is in fact the problem. It's a shape I exported from powerpoint. When I do save a chart like you did it works like it should. So strange... – Thomas May 03 '18 at 08:20
  • Curious: What Height/Width values did it have? – TaW May 03 '18 at 08:23
  • 141 x 322px with a filesize of less than 2kb (it's just the black shape of a man which I want to colorize). But the problem seems to occur with every shape I export from Powerpoint, even the standard ones like rectangles. – Thomas May 03 '18 at 09:22
  • And you move the guy 80px to the right on purpose? – TaW May 03 '18 at 09:24
  • No, that's not necessary, I was just trying a few things and forgot to reset that to 0. But it doesn't change anything - it's some kind of problem with how Powerpoint creates emf-files I think... – Thomas May 03 '18 at 09:32
  • Hm, just did a test with pp2010 and it save fine. I chose only current page and save as emf. – TaW May 03 '18 at 09:39
  • amazing - it works that way, thank you so much! What I did was just saving the shape as emf. I right-clicked the shape / save as picture (or something similar, I'm using the german version of PP) / emf. It didn't work this way. Might be some transparency problem... – Thomas May 03 '18 at 10:05
  • Just tried that; it works but the size/position is off to the right __a lot__ even though I set a destination rectangle equal to the target canvas.. – TaW May 03 '18 at 10:20
  • 1
    Hm, that doesn't work for me. But your previous idea perfectly solved my problem - so thank you very much, you helped me a lot! – Thomas May 03 '18 at 13:24
  • Gern geschehen. Und ich hab gelernt wie man emf Dateien verwenden kann. Einfach, wenn man von der MetaFile Klasse weiß.. – TaW May 03 '18 at 13:33
  • Can you actually write an answer to this, then? – Robinson Jun 09 '22 at 08:47

0 Answers0