Skip to content

Instantly share code, notes, and snippets.

@cathode
Created April 19, 2013 05:00
Show Gist options
  • Save cathode/5418255 to your computer and use it in GitHub Desktop.
Save cathode/5418255 to your computer and use it in GitHub Desktop.
public unsafe void WriteToBitmap(System.Drawing.Bitmap bitmap)
{
Contract.Requires(bitmap != null);
var bmpData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
unchecked
{
int* ptr = (int*)bmpData.Scan0.ToPointer();
fixed (Vector4f* cptr = &(this.Pixels[0]))
{
float* fp = (float*)cptr;
int max = this.Pixels.Length - 1;
for (int i = -1, n = -1; i < max; )
{
ptr[++i] = (byte)(fp[++n] * 255) | (byte)(fp[++n] * 255) << 8 | (byte)(fp[++n] * 255) << 16 | (byte)(fp[++n] * 255) << 24;
}
}
}
bitmap.UnlockBits(bmpData);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment