/// <summary> /// Copies one line at a time, to support diffrent kinds of memory allocation /// </summary> /// <param name="bmd"></param> /// <returns></returns> public static byte[] ToByteArrayScan(this BitmapData bmd) { if (bmd.IsNullOrEmpty()) { return(null); } byte[] result = new byte[bmd.Stride * bmd.Height]; //Marshal.Copy(bmd.Scan0, result, 0, result.Length); int offset = 0, i = 0, w = bmd.Width, h = bmd.Height, bpp = bmd.PixelFormat.GetBitsPerPixel(); long ptr = bmd.Scan0.ToInt64(); int wtBpp = w * (bpp / 8); int htBpp = h * (bpp / 8); for (; i < h; i++) { Marshal.Copy(new IntPtr(ptr), result, offset, wtBpp); offset += wtBpp; ptr += bmd.Stride; } return(result); }
public static byte[] ToByteArray(this BitmapData bmd) { if (bmd.IsNullOrEmpty()) { return(null); } byte[] result = new byte[bmd.Stride * bmd.Height]; Marshal.Copy(bmd.Scan0, result, 0, result.Length); return(result); }
/// <summary> /// Warning ! this array returns [y, x, z] format -> [width, height, pixels] /// </summary> /// <param name="bmd"></param> /// <returns></returns> public static byte[,,] ToByteArray3D_YXZ(this BitmapData bmd) { if (bmd.IsNullOrEmpty()) { return(null); } byte[] data = bmd.ToByteArrayScan(); if (data.IsNullOrEmpty()) { return(null); } int d = bmd.PixelFormat.GetBitsPerPixel() / 8, w = bmd.Width, h = bmd.Height, l = data.Length * sizeof(byte); byte[,,] result = new byte[h, w, d]; Buffer.BlockCopy(data, 0, result, 0, l); return(result); }