Click here to Skip to main content
15,881,424 members
Articles / Programming Languages / C#
Article

Quick n' Dirty Alpha Mask Generator

Rate me:
Please Sign up or sign in to vote.
4.00/5 (3 votes)
15 Mar 2007CPOL1 min read 39.6K   484   22   8
A quick-to-implement method for generating an alpha mask from a flat image file with no alpha channel
Screenshot - screenshot.jpg

Introduction

During a recent project at work, I needed a quick solution to apply transparency on-the-fly to a flat raster image provided by the user. After Googling like a mad man for ten minutes, I opted for a hand-rolled solution. This app demonstrates the use of the exceptionally inefficient, but effective result.

Using the Code

The sample app's single method goes through the source image pixel-by-pixel, takes a sampling of the surrounding pixels, and uses the mathematical mean of this sampling to calculate the base alpha value for that pixel. The lighter the sampling, the lower the calculated alpha value... e.g. a sampling that equals pure white would render the given pixel as fully transparent (alpha 0).

C#
for(int i=0; i<9; i++)
{
  Color sample;
  //SOOOOOOO inefficient!
  switch(i)
  {
    //TopLeft
    case 0:    
      sample = (x>0 && y>0) ? 
        oldBitmap.GetPixel(x-1,y-1) :
        oldBitmap.GetPixel(x,y);
      sampleGrid[i] = (sample.R + sample.G + sample.B)/3;
      break;

    //TopCenter
    case 1:
      sample = (y>0) ? 
        oldBitmap.GetPixel(x,y-1) :
        oldBitmap.GetPixel(x,y);
      sampleGrid[i] = (sample.R + sample.G + sample.B)/3;
      break;
                                
    /* etc., etc. */    

It then weighs that pixel's computed alpha value against a user-set threshold... if the value is above the threshold, the pixel is given full opacity (alpha 255). This could be easily modified to treat the darker parts of an image as transparent.

C#
int A = 0;
for(int i=0; i<9; i++)
  A += sampleGrid[i];

A = 255 - (A/9);
if (A > (int)this.numThreshold.Value)
  A = 255;

newBitmap.SetPixel(x,y, Color.FromArgb(A, color));

Points of Interest

I am quite certain that there are better ways to have implemented this, but I was on an insanely tight schedule, and this sufficed for the task at hand. If anyone would care to demonstrate a better implementation, I would greatly appreciate your feedback.

History

  • 15th March, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer the patchwerks
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralConvolution Pin
Adam Honeybell14-Jul-08 2:42
Adam Honeybell14-Jul-08 2:42 
GeneralNice work Pin
stixoffire22-Mar-07 6:55
stixoffire22-Mar-07 6:55 
I like this example. I know that others have documented different ways of doing things. For example the maketransaparent() method is nice, but what about that circle - if you had say a white area inside of it, (like a bullseye) but you did not want that area to be Xparent use maketransparent would not do the trick.. as it would set the Bullseye Xparent as well.. This method allows the possibility to see how to edit and manipulate the pixels.

I would definitely like to learn how to do edge detection.
Generalmaketransparent Pin
PuReBRaiNZ19-Mar-07 12:54
PuReBRaiNZ19-Mar-07 12:54 
GeneralRe: maketransparent Pin
patchwerk1-Apr-07 5:55
patchwerk1-Apr-07 5:55 
GeneralFastBitmap Pin
Patrick Etc.16-Mar-07 6:22
Patrick Etc.16-Mar-07 6:22 
GeneralRe: FastBitmap Pin
CarlosMMartins19-Mar-07 23:44
CarlosMMartins19-Mar-07 23:44 
AnswerRe: FastBitmap Pin
stixoffire22-Mar-07 6:50
stixoffire22-Mar-07 6:50 
GeneralRe: FastBitmap Pin
Patrick Etc.22-Mar-07 10:19
Patrick Etc.22-Mar-07 10:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.