ChaseTheSun
ChaseTheSun

Reputation: 3840

GDI+: How to replace black pixels with red pixels?

I have a black and white image, a would like to replace the black pixels with red pixels. I've tried

Gdiplus::Graphics* g = Gdiplus::Graphics::FromImage(filename);

Gdiplus::ImageAttributes ia;

Gdiplus::ColorMatrix m = { 
    0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
    0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
    0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
    0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
    1.0f, 0.0f, 0.0f, 0.0f, 1.0f
};

ia.SetColorMatrix(&m);

g->DrawImage(org, r, 0, 0, org->GetWidth(), org->GetHeight(), Gdiplus::UnitPixel, &ia);

But it's making the entire bitmap red.

Upvotes: 0

Views: 616

Answers (1)

user4624062
user4624062

Reputation:

Do not use a matrix to perform this transformation. Your matrix will always output the following vector:
[1.0 0.0 0.0 currentAlpha 1.0]
That's why you have a red image.
Visit https://msdn.microsoft.com/en-us/library/ms533875%28v=vs.85%29.aspx
Use this instead

ImageAttributes ia;

ColorMap blackToRed;

blackToRed.oldColor = Color(255, 0, 0, 0);  // black

blackToRed.newColor = Color(255, 255, 0, 0);// red

ia.SetRemapTable(1, &blackToRed);

Upvotes: 2

Related Questions