Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Here's an example how to fill a `WriteableBitmap` with random colors:
- // Hardcoded just for example
- int width = 500;
- int height = 500;
- // Calculate stride
- var stride = (width * (PixelFormats.Bgra32.BitsPerPixel) + 7) / 8;
- // Create a WriteableBitmap
- WriteableBitmap wb = new WriteableBitmap(width, height, 300.0, 300.0, PixelFormats.Bgra32, null);
- // And a buffer for the bytes
- byte [] b = new byte[stride * height];
- // Randomly fill array
- for (int i = 0; i < height; i++)
- {
- for (int j = 0; j < width; j++)
- {
- b[(i * width) + (j * 4)] = 255; // Alpha
- b[(i * width) + (j * 4) + 1] = (byte)r.Next(255);
- b[(i * width) + (j * 4) + 2] = (byte)r.Next(255);
- b[(i * width) + (j * 4) + 3] = (byte)r.Next(255);
- }
- }
- // Copy
- Int32Rect rect = new Int32Rect(0, 0, width, height);
- wb.WritePixels(rect, b, stride, 0, 0);
- // img is a WPF Image control
- img.Source = wb;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement