Posted: 2006-11-20T15:25:05-07:00
You can actually do that with the command line "convert" program but if you must use visual studio here is a C function which will do what you want using one of two methods. Change the filenames, coordinate and colours as required.
Code: Select all
/*
Use either MagickMatteFloodfillImage or MagickPaintTransparentImage
to make all the selected pixels transparent and write as a png.
*/
#include <windows.h>
#include <wand/magick_wand.h>
void test_wand(void)
{
// If flood is non-zero use FloodFill, otherwise use PaintTransparent
int flood = 0;
MagickWand *magick_wand = NULL;
// Set default fuzz to zero (see below)
double fuzz = 0.;
MagickWandGenesis();
magick_wand = NewMagickWand();
MagickReadImage(magick_wand,"eqn9_rings_040.jpg");
MagickStripImage(magick_wand);
// Both Floodfill and PaintTransparent force the matte on so if you start with
// a JPG (for example) you don't need to force the matte on.
// In order to change the selected pixels to be fully transparent I had to set
// the "opacity" argument to 255 which means fully opaque not fully transparent.
// This was true for both MagickMatteFloodfillImage and MagickPaintTransparentImage
// I've set the fuzz to 20 for my image but you can change or remove
// this as needed
fuzz = 20.;
// A Floodfill only affects pixels in one contiguous area
// Flood the area that is contiguous with coordinate (50,0) and has the
// same colour to within the fuzz factor
// In this case I know that the coordinate is black and I want that one
// contiguous area filled with transparency
// MagickBooleanType MagickMatteFloodfillImage(MagickWand *wand,
// const Quantum opacity,const double fuzz,const PixelWand *bordercolor,
// const long x,const long y)
if(flood)MagickMatteFloodfillImage(magick_wand,255,fuzz,NULL,50,0);
else {
PixelWand *target;
target = NewPixelWand();
PixelSetColor(target,"black");
// A PaintTransparent affects all pixels of a given colour
// This specifically targets all pixels having the colour specified
// by the "target" wand and changes their opacity to the specified
// value - EXCEPT that I had to specify an opacity of 255 to get
// full transparency
// MagickBooleanType MagickPaintTransparentImage(MagickWand *wand,
// const PixelWand *target,const Quantum opacity,
// const double fuzz)
MagickPaintTransparentImage(magick_wand,target,255,fuzz);
DestroyPixelWand(target);
}
MagickWriteImage(magick_wand,"eqn9_trans_040.png");
/* Clean up */
if(magick_wand)magick_wand = DestroyMagickWand(magick_wand);
MagickWandTerminus();
}
Pete