Results 1 to 2 of 2

Thread: Alpha mask help

  1. #1

    Thread Starter
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,390

    Alpha mask help

    I have a alpha mask (grayscale)... and want to specify a color (eg color.blue) and then be able to paint the blue version of the new image(using the solid color with the alpha mask) to the screen using graphics...

    How is it best to do this?

    Thanks
    Kris

  2. #2
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: Alpha mask help

    Quote Originally Posted by i00 View Post
    I have a alpha mask (grayscale)... and want to specify a color (eg color.blue) and then be able to paint the blue version of the new image(using the solid color with the alpha mask) to the screen using graphics...

    How is it best to do this?

    Thanks
    Kris
    Hi Kris,

    For each pixel in the image, you copy a grayscale color byte to the alpha byte of your target image, and set the target image blue byte to 255. For example:
    Code:
    grayscale image: alpha = &H255 red = &H35 green = &H35 blue = &H35
    target image: alpha = &H35 red = &H00 green = &H00 blue = &H255
    You could do this easily by looping through the whole image in the X and Y directions, using Bitmap.GetPixel and Bitmap.SetPixel. But for anything but tiny images that is usually too slow to be practical. Instead you should use a LockBits-based method. Here's an example of how I would do it using the FastPix class (see the LockBits Wrapper in my signature). I assume AlphaMask is the name of your grayscale bitmap, and clr is the base color you want to use for the target image:

    vb.net Code:
    1. Dim clrInt As Integer = clr.ToArgb And &HFFFFFF 'zeroise the Alpha byte
    2. Dim TargetImage As New Bitmap(AlphaMask) 'make sure the target image is in 32bpp format
    3. Using fp As New FastPix(TargetImage)
    4.    Dim pixels As Integer() = fp.PixelArray
    5.    For i As Integer = 0 to pixels.Count-1
    6.       Dim alpha As Integer = pixels(i) << 24 'shift the blue byte to the Alpha byte
    7.       pixels(i) = alpha Or clrInt 'combine Alpha with the base colour
    8.    Next
    9. End Using
    Now you can use TargetImage as a BackgroundImage or paint in the Paint event of a Form or other control with DrawImage.

    BB

    EDIT: The above code works fine. I was able to simplify the loop code a bit.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width