How to change the background color of an image...

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
Experienced Solver
Experienced Solver
Posts: 52
Joined: Fri Sep 07, 2007 11:58 am

How to change the background color of an image...

Post by pujari » Tue Dec 22, 2009 9:23 am


I need to change the background color of an image in my application.How can i do it???
I am loading a file (Jpeg) into my image object. In the file the image has a background of Black color. Now i need to change this black background to white color and disply my image on to a canvass (scrolled window).

I am using wxWidgets Version 2.8.4.

Please guide me in this issue.

Earned some good credits
Earned some good credits
Posts: 104
Joined: Thu Apr 09, 2009 4:58 pm
Location: Russia

Post by yuri » Tue Dec 22, 2009 10:42 am

Short answer: you can't, esp, with jpeg.

What you can do is get access to image data() and manually change all pixels below given brightness to something else. However this way you also change all dark pixels in the "foreground".

How to do it: switch to image format that support transparency (alpha), like PNG or GIF, then change image in some proper image editor, like photoshop or gimp to have transparent background. This way you'll be able to show image on whatever background you want.

Knows some wx things
Knows some wx things
Posts: 32
Joined: Wed Nov 12, 2008 7:04 pm

Post by smf » Tue Dec 22, 2009 7:28 pm

Another option would be to create a second jpeg-image containing a mask-image (eg. white=foreground, black=background) and use this as a blit-mask... This has the clear disadvantage that you need to keep track of two images in memory but depending on what you want to do this could be the better way (if for instance filesize does matter)...


Post Reply