Advertisement
Guest User

Untitled

a guest
Dec 30th, 2016
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.00 KB | None | 0 0
  1. Here's an example how to fill a `WriteableBitmap` with random colors:
  2.  
  3.    // Hardcoded just for example
  4.    int width = 500;
  5.    int height = 500;
  6.    // Calculate stride
  7.    var stride = (width * (PixelFormats.Bgra32.BitsPerPixel) + 7) / 8;
  8.    // Create a WriteableBitmap
  9.    WriteableBitmap wb = new WriteableBitmap(width, height, 300.0, 300.0, PixelFormats.Bgra32, null);
  10.    // And a buffer for the bytes
  11.    byte [] b = new byte[stride * height];
  12.    // Randomly fill array
  13.    for (int i = 0; i < height; i++)
  14.    {
  15.        for (int j = 0; j < width; j++)
  16.        {
  17.            b[(i * width) + (j * 4)] = 255;   // Alpha
  18.            b[(i * width) + (j * 4) + 1] = (byte)r.Next(255);
  19.            b[(i * width) + (j * 4) + 2] = (byte)r.Next(255);
  20.            b[(i * width) + (j * 4) + 3] = (byte)r.Next(255);
  21.        }
  22.    }
  23.  
  24.    // Copy
  25.    Int32Rect rect = new Int32Rect(0, 0, width, height);
  26.    wb.WritePixels(rect, b, stride, 0, 0);
  27.    // img is a WPF Image control
  28.    img.Source = wb;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement