Setting alpha when Bitmap is locked?

aewarnick

Well-known member
Joined
Jan 29, 2003
Messages
1,031
Here is a method that shades the Bitmap a certain color. I notice that alpha is never accessed here when the bits are locked.

How do I access alpha besides SetPixel when the image is Locked?
C#:
public static bool Color(Bitmap b, int red, int green, int blue)
				{
					if (red < -255 || red > 255) return false;
					if (green < -255 || green > 255) return false;
					if (blue < -255 || blue > 255) return false;

					BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);

					int stride = bmData.Stride;
					System.IntPtr Scan0 = bmData.Scan0;

					unsafe
					{
						byte * p = (byte *)(void *)Scan0;

						int nOffset = stride - b.Width*3;
						int nPixel;

						for(int y=0;y<b.Height;++y)
						{
							for(int x=0; x < b.Width; ++x )
							{
								nPixel = p[2] + red;
								nPixel = Math.Max(nPixel, 0);
								p[2] = (byte)Math.Min(255, nPixel);

								nPixel = p[1] + green;
								nPixel = Math.Max(nPixel, 0);
								p[1] = (byte)Math.Min(255, nPixel);

								nPixel = p[0] + blue;
								nPixel = Math.Max(nPixel, 0);
								p[0] = (byte)Math.Min(255, nPixel);

								p += 3;
							}
							p += nOffset;
						}
					}

					b.UnlockBits(bmData);

					return true;
				}
 
Back
Top