Skip to content

Instantly share code, notes, and snippets.

@wmeints
Created November 28, 2012 17:30
Show Gist options
  • Save wmeints/4162721 to your computer and use it in GitHub Desktop.
Save wmeints/4162721 to your computer and use it in GitHub Desktop.
Component to write random pixels into a WriteableBitmap on Windows 8
/// <summary>
/// Allows for drawing random pixels into a writeable bitmap
/// </summary>
public class PixelBuffer
{
private WriteableBitmap _source;
private byte[] _pixels;
/// <summary>
/// Initializes a new instance of <see cref="PixelBuffer"/>
/// </summary>
/// <param name="source"></param>
public PixelBuffer(WriteableBitmap source)
{
_source = source;
_pixels = new byte[4* source.PixelWidth * source.PixelHeight];
}
public void SetPixel(int x, int y, Color color)
{
// Move to the correct index in the pixel buffer
// Pixels are arranged on a col, row manner.
int index = 4 * (y * _source.PixelWidth + x);
// The pixel layout is always A, R, G, B.
_pixels[index] = color.A;
_pixels[index + 1] = color.R;
_pixels[index + 2] = color.G;
_pixels[index + 3] = color.B;
}
public void Flush()
{
var pixelStream = _source.PixelBuffer.AsStream();
pixelStream.Seek(0, SeekOrigin.Begin);
pixelStream.Write(_pixels,0,_pixels.Length);
_source.Invalidate();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment