Victor Image Processing Library How-to Tips

Create a Transparent GIF Image from a Blue Screen Photograph

The sequence of steps used in the sample:

C# Source Code

Transparent Background - the C# Source Code

Requires Victor Image Processing Library for 32-bit Windows v 6 or higher, using the viclib namespace.
         private int savetransparent()
         //For ease of understanding, in this sample we do not check the return code values -- 
         // but you should always check the return code values to make sure everything is operating properly.
            int rcode;
            int length, width, vbitcount;
            int avg_red = 0, avg_grn = 0, avg_blu = 0;
            int extent = 60;
            int gifsavemode = 2;
            int transcolor;
            int j;
            string oldfilename = "bluescreenphoto.bmp";
            string newfilename = "transparent.gif";
            vicwin.imgdes simage; // source
            vicwin.imgdes oimage; // operator
            vicwin.imgdes mimage; // mask
            vicwin.imgdes rimage; // result
            vicwin.imgdes timage; // temp
            vicwin.RGBTRIPLE background;
            background = new vicwin.RGBTRIPLE();
            vicwin.BITMAPINFOHEADER bdata;
            bdata = new vicwin.BITMAPINFOHEADER();
            rcode = vicwin.bmpinfo(ref oldfilename, ref bdata);
            if (rcode == vicwin.NO_ERROR)
                width = bdata.biWidth;
                length = bdata.biHeight;
                vbitcount = bdata.biBitCount;
                simage = new vicwin.imgdes();   // source
                oimage = new vicwin.imgdes();   // operator
                mimage = new vicwin.imgdes();   // mask
                rimage = new vicwin.imgdes();   // result
                timage = new vicwin.imgdes();   // temp
                rcode = vicwin.allocimage(ref simage, width, length, vbitcount);
                if (rcode == vicwin.NO_ERROR)
                    rcode = vicwin.loadbmp(ref oldfilename, ref simage);
                if (rcode == vicwin.NO_ERROR)
                    rcode = vicwin.allocimage(ref oimage, width, length, vbitcount);
                    rcode = vicwin.allocimage(ref mimage, width, length, 8);
                    rcode = vicwin.allocimage(ref rimage, width, length, 8);
                    rcode = vicwin.allocimage(ref timage, width, length, vbitcount);
        // Identify the background color
                vicwin.setimagearea(ref simage, 190, 15, 217, 177);  // select an area of background color
                vicwin.calcavglevel(ref simage, ref avg_red, ref avg_grn, ref avg_blu); // determine the avgerage RGB values
                vicwin.setimagearea(ref simage, 0, 0, width - 1, length - 1);  // restore area to entire image
                background.rgbtRed = (byte)avg_red;
                background.rgbtGreen = (byte)avg_grn;
                background.rgbtBlue = (byte)avg_blu;
        // Create the mask to distinguish the foreground/background
                vicwin.extractcolorrange(ref background, extent, ref simage, ref timage);
                vicwin.colortogray(ref timage, ref mimage);  // mask is ready, transparent area is white, rest is black
        // Set entire operator to single background color
                byte[] redtab = new byte[256];
                byte[] grntab = new byte[256];
                byte[] blutab = new byte[256];
                for (j = 0; j < 256; j++)
                    redtab[j] = (byte)avg_red; grntab[j] = (byte)avg_grn; blutab[j] = (byte)avg_blu;
                rcode = vicwin.usetable(ref redtab[0], ref grntab[0], ref blutab[0], ref oimage, ref oimage);
        // Combine source, operator, and mask to set background of original source image to the new single color
                vicwin.wtaveragemask(ref simage, ref oimage, ref simage, ref mimage);
        // Convert to 8-bit
                vicwin.convertrgbtopalex(256, ref simage, ref rimage, vicwin.CR_TSDNODIFF);
        // Determine the palette index of the background color
                transcolor = vicwin.getpixelcolor(ref rimage, 190, 15);
        // Save the image as transparent GIF 
                vicwin.savegifex(ref newfilename, ref rimage, gifsavemode, transcolor);
        // Free all the image buffers
                vicwin.freeimage(ref timage);
                vicwin.freeimage(ref rimage);
                vicwin.freeimage(ref mimage);
                vicwin.freeimage(ref oimage);
                vicwin.freeimage(ref simage); 
            return (rcode);

Victor Image Processing Library

Victor Image Processing Library homepage | Victor Product Summary | more source code

Copyright  2009 Catenary Systems Inc. All rights reserved. Victor Image Processing Library is a trademark of Catenary Systems.