// Copy line by line. This can happen if the image width is not divisible by four for (int i = 0; i < bitmap.Height; ++i) { Marshal.Copy(buffer, i * imageStride, new IntPtr(ptrBmp.ToInt64() + i * bmpData.Stride), width); }