External/Aga.Controls/BitmapHelper.cs
changeset 345 0c551e8818e0
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/External/Aga.Controls/BitmapHelper.cs	Sun May 27 15:16:19 2012 +0000
     1.3 @@ -0,0 +1,46 @@
     1.4 +using System;
     1.5 +using System.Collections.Generic;
     1.6 +using System.Text;
     1.7 +using System.Drawing;
     1.8 +using System.Runtime.InteropServices;
     1.9 +using System.Drawing.Imaging;
    1.10 +
    1.11 +namespace Aga.Controls
    1.12 +{
    1.13 +	public static class BitmapHelper
    1.14 +	{
    1.15 +		[StructLayout(LayoutKind.Sequential)]
    1.16 +		private struct PixelData
    1.17 +		{
    1.18 +			public byte B;
    1.19 +			public byte G;
    1.20 +			public byte R;
    1.21 +			public byte A;
    1.22 +		}
    1.23 +
    1.24 +		public static void SetAlphaChanelValue(Bitmap image, byte value)
    1.25 +		{
    1.26 +			if (image == null)
    1.27 +				throw new ArgumentNullException("image");
    1.28 +			if (image.PixelFormat != PixelFormat.Format32bppArgb)
    1.29 +				throw new ArgumentException("Wrong PixelFormat");
    1.30 +
    1.31 +			BitmapData bitmapData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height),
    1.32 +									 ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
    1.33 +			unsafe
    1.34 +			{
    1.35 +				PixelData* pPixel = (PixelData*)bitmapData.Scan0;
    1.36 +				for (int i = 0; i < bitmapData.Height; i++)
    1.37 +				{
    1.38 +					for (int j = 0; j < bitmapData.Width; j++)
    1.39 +					{
    1.40 +						pPixel->A = value;
    1.41 +						pPixel++;
    1.42 +					}
    1.43 +					pPixel += bitmapData.Stride - (bitmapData.Width * 4);
    1.44 +				}
    1.45 +			}
    1.46 +			image.UnlockBits(bitmapData);
    1.47 +		}
    1.48 +	}
    1.49 +}