//constructor public ImageMatrix(Bitmap image) { this.width = image.Width; this.height = image.Height; function = new byte[width, height];//define bounds of function //unsafe bitmap to iterate through the image unSafeBitmap usb = new unSafeBitmap(image); usb.LockImage(); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { /* Reading average (gray scale) value for each of the pixel * and storing it in function*/ this.function[i, j] = usb.GetAverage(i, j); } } }
//to convert imagematrix to public Bitmap ToImage() { Bitmap image = new Bitmap(width, height); unSafeBitmap usb = new unSafeBitmap(image);//making object for unsafe bitmapping usb.LockImage(); //making a grayscale image according to function array for(int i=0;i<width;i++) { for(int j=0;j<height;j++) { //writign each pixel on image bitmap. byte Value = function[i, j]; Color c = Color.FromArgb(Value, Value, Value);//gray scale image usb.SetPixel(i, j, c); } } //unlock image before returning usb.UnlockImage(); return image; }