|
| 1 | + |
| 2 | +using System; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Linq; |
| 5 | +using System.Text; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using System.Windows; |
| 8 | +using System.Windows.Controls; |
| 9 | +using System.Windows.Data; |
| 10 | +using System.Windows.Documents; |
| 11 | +using System.Windows.Input; |
| 12 | +using System.Windows.Media; |
| 13 | +using System.Windows.Media.Imaging; |
| 14 | +using System.Windows.Navigation; |
| 15 | +using System.Windows.Shapes; |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +namespace GameOfLife.Demos { |
| 22 | + class WriteableBitmapDemo { |
| 23 | + |
| 24 | + public static void Draw(Grid grdMain){ |
| 25 | + const int width = 240; |
| 26 | + const int height = 240; |
| 27 | + |
| 28 | + |
| 29 | + |
| 30 | + WriteableBitmap wbitmap = new WriteableBitmap( |
| 31 | + width, height, 96, 96, PixelFormats.Bgra32, null); |
| 32 | + byte[, ,] pixels = new byte[height, width, 4]; |
| 33 | + |
| 34 | + // Clear to black. |
| 35 | + for (int row = 0; row < height; row++) |
| 36 | + { |
| 37 | + for (int col = 0; col < width; col++) |
| 38 | + { |
| 39 | + for (int i = 0; i < 3; i++) |
| 40 | + pixels[row, col, i] = 0; |
| 41 | + pixels[row, col, 3] = 255; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + // Blue. |
| 46 | + for (int row = 0; row < 80; row++) |
| 47 | + { |
| 48 | + for (int col = 0; col <= row; col++) |
| 49 | + { |
| 50 | + pixels[row, col, 0] = 255; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + // Green. |
| 55 | + for (int row = 80; row < 160; row++) |
| 56 | + { |
| 57 | + for (int col = 0; col < 80; col++) |
| 58 | + { |
| 59 | + pixels[row, col, 1] = 255; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // Red. |
| 64 | + for (int row = 160; row < 240; row++) |
| 65 | + { |
| 66 | + for (int col = 0; col < 80; col++) |
| 67 | + { |
| 68 | + pixels[row, col, 2] = 255; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // Copy the data into a one-dimensional array. |
| 73 | + byte[] pixels1d = new byte[height * width * 4]; |
| 74 | + int index = 0; |
| 75 | + for (int row = 0; row < height; row++) |
| 76 | + { |
| 77 | + for (int col = 0; col < width; col++) |
| 78 | + { |
| 79 | + for (int i = 0; i < 4; i++) |
| 80 | + pixels1d[index++]= pixels[row, col, i]; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // Update writeable bitmap with the colorArray to the image. |
| 85 | + Int32Rect rect = new Int32Rect(0, 0, width, height); |
| 86 | + int stride = 4 * width; |
| 87 | + wbitmap.WritePixels(rect, pixels1d, stride, 0); |
| 88 | + |
| 89 | + // Create an Image to display the bitmap. |
| 90 | + Image image = new Image(); |
| 91 | + image.Stretch = Stretch.None; |
| 92 | + image.Margin = new Thickness(0); |
| 93 | + |
| 94 | + grdMain.Children.Add(image); |
| 95 | + |
| 96 | + //Set the Image source. |
| 97 | + image.Source = wbitmap; |
| 98 | + |
| 99 | + } |
| 100 | + |
| 101 | + } |
| 102 | +} |
0 commit comments