(c) 2003 Visual Studio Magazine 
Fawcette Technical Publications

C#	Listing 1
Modify All of an Image's Alpha Values. This code loops through an entire bitmap, getting each pixel and modifying its alpha value.

private void setAlphaValues ()
{
	if (_image != null)
	{
		// Get the value from the trackbar:
		int alpha = 255 - AlphaTrackBar.Value;

		// Modify each pixel in the image:
		Rectangle rc = new Rectangle (0, 0, 
			_image.Width, _image.Height);
		BitmapData d = _image.LockBits (rc, 
			ImageLockMode.ReadWrite, 
			_image.PixelFormat);

			unsafe 
			{
				int* p = (int*)d.Scan0;
				int numPixels = d.Width * d.Height;
				System.Diagnostics.Debug.Assert 
					(d.Stride / d.Width == 4);
				int px = 0;
				while (px < numPixels)
				{
					// Get the color:
					Color c = Color.FromArgb (*p);
					// Apply the alpha value:
					c = Color.FromArgb (alpha, c);
					// Set the color:
					*p = c.ToArgb ();
					p++; px++;
				}
			}
			// Done, unlock:
			_image.UnlockBits (d);
			Invalidate ();
		}
	}
}
