/// <summary> /// Converts back to a bitmap image ready for use again /// </summary> /// <returns>The channels all in bitmap form</returns> public static Bitmap imageBitsToBitMap(imagebits img, int x, int y) { var b = new Bitmap(x, y, PixelFormat.Format32bppArgb); var BoundsRect = new Rectangle(0, 0, x, y); BitmapData bmpData = b.LockBits(BoundsRect, ImageLockMode.WriteOnly, b.PixelFormat); IntPtr ptr = bmpData.Scan0; byte[] orderedbits = new byte[img.r.Length * 4]; for (int i = 0; i < img.r.Length; i++) { orderedbits[i * 4] = img.b[i]; orderedbits[i * 4 + 1] = img.g[i]; orderedbits[i * 4 + 2] = img.r[i]; orderedbits[i * 4 + 3] = img.a[i]; } Marshal.Copy(orderedbits, 0, ptr, img.r.Length * 4); b.UnlockBits(bmpData); return(b); }
public static imagebits fastbits(Bitmap source) { // Lock the bitmap's bits. Rectangle rect = new Rectangle(0, 0, source.Width, source.Height); BitmapData bmpData = source.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb); // Get the address of the first line. IntPtr ptr = bmpData.Scan0; // Declare an array to hold the bytes of the bitmap. int bytes = bmpData.Stride * source.Height; byte[] rgbaValues = new byte[bytes]; byte[] r = new byte[bytes / 4]; byte[] g = new byte[bytes / 4]; byte[] b = new byte[bytes / 4]; byte[] a = new byte[bytes / 4]; // Copy the RGB values into the array. Marshal.Copy(ptr, rgbaValues, 0, bytes); int count = 0; int stride = bmpData.Stride; for (int column = 0; column < bmpData.Height; column++) { for (int row = 0; row < bmpData.Width; row++) { b[count] = (byte)(rgbaValues[(column * stride) + (row * 4)]); g[count] = (byte)(rgbaValues[(column * stride) + (row * 4) + 1]); r[count] = (byte)(rgbaValues[(column * stride) + (row * 4) + 2]); a[count++] = (byte)(rgbaValues[(column * stride) + (row * 4) + 3]); } } imagebits allbits = new imagebits(); allbits.r = r; allbits.g = g; allbits.b = b; allbits.a = a; allbits.width = bmpData.Width; allbits.height = bmpData.Height; source.UnlockBits(bmpData); return(allbits); }
//New extension methods public static Bitmap toBitMap(this imagebits img) { return(imageBitsToBitMap(img)); }