using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.IO; namespace CAPSTONE_4 { // A class to represent WriteableBitmap pixels in Bgra32 format. public class BitmapPixelMaker //the class that allows me to get and set the color of pixels, X is math graphical X, and Y is math graphical Y, so some commands are easier to think through //borrowed from the internet, but I have added/modified a couple functions to suit my needs. { // The bitmap's size. private int Width, Height; // The pixel array. public byte[] Pixels; // The number of bytes per row. public int Stride; // Constructor. Width and height required. public BitmapPixelMaker(int width, int height) { InitializeFromDimensions(width, height); } // Constructor that loads from a WriteableBitmap. public BitmapPixelMaker(WriteableBitmap wbitmap) { InitializeFromWbitmap(wbitmap); } // Initialize from a WriteableBitmap. private void InitializeFromWbitmap(WriteableBitmap wbitmap) { // Initialize the basics. // Use Convert.ToInt32 to round the dimensions to integers. int wid = Convert.ToInt32(wbitmap.Width); int hgt = Convert.ToInt32(wbitmap.Height); InitializeFromDimensions(wid, hgt); // Get the pixels. Int32Rect rect = new Int32Rect(0, 0, Width, Height); wbitmap.CopyPixels(rect, Pixels, Stride, 0); } // Constructor that loads from a URI. public BitmapPixelMaker(Uri uri) { // Load the file into a WriteableBitmap. BitmapImage bitmap = new BitmapImage(uri); WriteableBitmap wbitmap = new WriteableBitmap(bitmap); // Initialize the object. InitializeFromWbitmap(wbitmap); } // Initialize the object from its width and height. private void InitializeFromDimensions(int width, int height) { // Save the width and height. Width = width; Height = height; // Create the pixel array. Pixels = new byte[width * height * 4]; // Calculate the stride. Stride = width * 4; } // Get a pixel's value. public void GetPixel(int x, int y, out byte red, out byte green, out byte blue, out byte alpha) { int index = x * Stride + y * 4; blue = Pixels[index++]; green = Pixels[index++]; red = Pixels[index++]; alpha = Pixels[index]; } public byte GetBlue(int x, int y) { return Pixels[y * Stride + x * 4]; } public byte GetGreen(int x, int y) { return Pixels[y * Stride + x * 4 + 1]; } public byte GetRed(int x, int y) { return Pixels[y * Stride + x * 4 + 2]; } public byte GetAlpha(int x, int y) { return Pixels[y * Stride + x * 4 + 3]; } public void SetBlue(int x, int y, byte blue) { Pixels[y * Stride + x * 4] = blue; } public void SetGreen(int x, int y, byte green) { Pixels[y * Stride + x * 4 + 1] = green; } public void SetRed(int x, int y, byte red) { Pixels[y * Stride + x * 4 + 2] = red; } public void SetAlpha(int x, int y, byte alpha) { Pixels[y * Stride + x * 4 + 3] = alpha; } //I ADDED SETPIXEL WHICH SETS THE COLOR OF THE PIXEL X,Y TO THE SPECIFIED VALUES public void SetPixel(int x, int y, byte red, byte green, byte blue, byte alpha) { int index = x * Stride + y * 4; Pixels[index++] = blue; Pixels[index++] = green; Pixels[index++] = red; Pixels[index++] = alpha; } //this function sets the color of all pixels to a set color public void SetColor(byte red, byte green, byte blue, byte alpha) { int num_bytes = Width * Height * 4; int index = 0; while (index < num_bytes) { Pixels[index++] = blue; Pixels[index++] = green; Pixels[index++] = red; Pixels[index++] = alpha; } } // Set all pixels to a specific opaque color. public void SetColor(byte red, byte green, byte blue) { SetColor(red, green, blue, 255); } // Use the pixel data to create a WriteableBitmap. WriteableBitmaps allow me to further manipulate the screen as the program runs. public WriteableBitmap MakeBitmap(double dpiX, double dpiY) { // Create the WriteableBitmap. WriteableBitmap wbitmap = new WriteableBitmap( Width, Height, dpiX, dpiY, PixelFormats.Bgra32, null); // Load the pixel data. Int32Rect rect = new Int32Rect(0, 0, Width, Height); wbitmap.WritePixels(rect, Pixels, Stride, 0); // Return the bitmap. return wbitmap; } public void EditBitmap(WriteableBitmap wbimp) { for (int i = 0; i < wbimp.Width; i++) { for (int j = 0; j < wbimp.Height; j++) { Int32Rect rect = new Int32Rect(i, j, 1, 1); wbimp.WritePixels(rect, Pixels, Stride, 0); } } // Return the bitmap. return; } } }