External/Aga.Controls/BitmapHelper.cs
author sl
Sun, 03 Feb 2013 19:06:01 +0100
changeset 392 8af90fa0940f
permissions -rw-r--r--
Now fetching some of our Sound Graph DLL function pointers.
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Text;
     4 using System.Drawing;
     5 using System.Runtime.InteropServices;
     6 using System.Drawing.Imaging;
     7 
     8 namespace Aga.Controls
     9 {
    10 	public static class BitmapHelper
    11 	{
    12 		[StructLayout(LayoutKind.Sequential)]
    13 		private struct PixelData
    14 		{
    15 			public byte B;
    16 			public byte G;
    17 			public byte R;
    18 			public byte A;
    19 		}
    20 
    21 		public static void SetAlphaChanelValue(Bitmap image, byte value)
    22 		{
    23 			if (image == null)
    24 				throw new ArgumentNullException("image");
    25 			if (image.PixelFormat != PixelFormat.Format32bppArgb)
    26 				throw new ArgumentException("Wrong PixelFormat");
    27 
    28 			BitmapData bitmapData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height),
    29 									 ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
    30 			unsafe
    31 			{
    32 				PixelData* pPixel = (PixelData*)bitmapData.Scan0;
    33 				for (int i = 0; i < bitmapData.Height; i++)
    34 				{
    35 					for (int j = 0; j < bitmapData.Width; j++)
    36 					{
    37 						pPixel->A = value;
    38 						pPixel++;
    39 					}
    40 					pPixel += bitmapData.Stride - (bitmapData.Width * 4);
    41 				}
    42 			}
    43 			image.UnlockBits(bitmapData);
    44 		}
    45 	}
    46 }