
Originally Posted by
i00
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:
Dim clrInt As Integer = clr.ToArgb And &HFFFFFF 'zeroise the Alpha byte
Dim TargetImage As New Bitmap(AlphaMask) 'make sure the target image is in 32bpp format
Using fp As New FastPix(TargetImage)
Dim pixels As Integer() = fp.PixelArray
For i As Integer = 0 to pixels.Count-1
Dim alpha As Integer = pixels(i) << 24 'shift the blue byte to the Alpha byte
pixels(i) = alpha Or clrInt 'combine Alpha with the base colour
Next
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.