Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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?

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;
			}

C#

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...