Mark13426
Mark13426

Reputation: 2639

Create an image in WPF whose pixels are randomly chosen

I want to create an 800x600 image whose each pixel is randomly chosen to be either green or red. How can I do this in WPF?

Upvotes: 1

Views: 551

Answers (1)

Kris
Kris

Reputation: 7170

See WriteableBitmap

const uint red = 0xFFFF0000,green = 0xFF00FF00;
var rnd = new Random();
var bmp = new WriteableBitmap(800, 600, 96, 96, PixelFormats.Pbgra32, null);
var data = Enumerable.Range(0, 800 * 600).Select(x => rnd.NextDouble() > 0.5 ? red : green).ToArray();
bmp.WritePixels(new Int32Rect(0, 0, 800, 600), data, bmp.BackBufferStride, 0);

That is a simple example and does not cover dealing with the bitmap stride, different pixel formats or alpha.

Upvotes: 2

Related Questions