//for byte image //========================================== /// <summary> /// Get the height of the image /// </summary> /// <param name="ImageMatrix">2D ImageMatrixay that contains the image</param> /// <returns>Image Height</returns> public static int GetHeight(MyColor[,] ImageMatrix) { return ImageMatrix.GetLength(0); }
//======================================================= /// <summary> /// Display the given image on the given PictureBox object /// </summary> /// <param name="ImageMatrix">2D ImageMatrixay that contains the image</param> /// <param name="PicBox">PictureBox object to display the image on it</param> public static void DisplayImage(MyColor[,] ImageMatrix, PictureBox PicBox) { // Create Image: //============== int Height = ImageMatrix.GetLength(0); int Width = ImageMatrix.GetLength(1); Bitmap ImageBMP = new Bitmap(Width, Height, PixelFormat.Format24bppRgb); unsafe { BitmapData bmd = ImageBMP.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadWrite, ImageBMP.PixelFormat); int nWidth = 0; nWidth = Width * 3; int nOffset = bmd.Stride - nWidth; byte* p = (byte*)bmd.Scan0; for (int i = 0; i < Height; i++) { for (int j = 0; j < Width; j++) { p[0] = ImageMatrix[i, j].red; p[1] = ImageMatrix[i, j].green; p[2] = ImageMatrix[i, j].blue; p += 3; } p += nOffset; } ImageBMP.UnlockBits(bmd); } PicBox.Image = ImageBMP; }
/// <summary> /// Normal resize of an image /// </summary> /// <param name="ImageMatrix">2D array of image values</param> /// <param name="NewWidth">desired width</param> /// <param name="NewHeight">desired height</param> /// <returns>Resized image</returns> public static MyColor[,] NormalResize(MyColor[,] ImageMatrix, int NewWidth, int NewHeight) { int i = 0, j = 0; int Height = ImageMatrix.GetLength(0); int Width = ImageMatrix.GetLength(1); double WidthRatio = (double)(Width) / (double)(NewWidth); double HeightRatio = (double)(Height) / (double)(NewHeight); int OldWidth = Width; int OldHeight = Height; MyColor P1, P2, P3, P4; MyColor Y1, Y2, X = new MyColor(); MyColor[,] Data = new MyColor[NewHeight, NewWidth]; Width = NewWidth; Height = NewHeight; int floor_x, ceil_x; int floor_y, ceil_y; double x, y; double fraction_x, one_minus_x; double fraction_y, one_minus_y; for (j = 0; j < NewHeight; j++) for (i = 0; i < NewWidth; i++) { x = (double)(i) * WidthRatio; y = (double)(j) * HeightRatio; floor_x = (int)(x); ceil_x = floor_x + 1; if (ceil_x >= OldWidth) ceil_x = floor_x; floor_y = (int)(y); ceil_y = floor_y + 1; if (ceil_y >= OldHeight) ceil_y = floor_y; fraction_x = x - floor_x; one_minus_x = 1.0 - fraction_x; fraction_y = y - floor_y; one_minus_y = 1.0 - fraction_y; P1 = ImageMatrix[floor_y, floor_x]; P2 = ImageMatrix[ceil_y, floor_x]; P3 = ImageMatrix[floor_y, ceil_x]; P4 = ImageMatrix[ceil_y, ceil_x]; Y1.red = (byte)(one_minus_y * P1.red + fraction_y * P2.red); Y1.green = (byte)(one_minus_y * P1.green + fraction_y * P2.green); Y1.blue = (byte)(one_minus_y * P1.blue + fraction_y * P2.blue); Y2.red = (byte)(one_minus_y * P3.red + fraction_y * P4.red); Y2.green = (byte)(one_minus_y * P3.green + fraction_y * P4.green); Y2.blue = (byte)(one_minus_y * P3.blue + fraction_y * P4.blue); X.red = (byte)(one_minus_x * Y1.red + fraction_x * Y2.red); X.green = (byte)(one_minus_x * Y1.green + fraction_x * Y2.green); X.blue = (byte)(one_minus_x * Y1.blue + fraction_x * Y2.blue); Data[j, i] = X; } return Data; }
/// <summary> /// Save the blurred image on the given PictureBox object /// </summary> /// <param name="ImageMatrix">2D ImageMatrix that contains the image</param> public static void SaveImage(MyColor[,] ImageMatrix) { // Create New Image: //====================== int Height = ImageMatrix.GetLength(0); int Width = ImageMatrix.GetLength(1); Bitmap NewImageBMP = new Bitmap(Width, Height, PixelFormat.Format24bppRgb); unsafe { BitmapData bmd = NewImageBMP.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadWrite, NewImageBMP.PixelFormat); int nWidth = 0; nWidth = Width * 3; int nOffset = bmd.Stride - nWidth; byte* p = (byte*)bmd.Scan0; for (int i = 0; i < Height; i++) { for (int j = 0; j < Width; j++) { p[0] = ImageMatrix[i, j].red; p[1] = ImageMatrix[i, j].green; p[2] = ImageMatrix[i, j].blue; p += 3; } p += nOffset; } NewImageBMP.UnlockBits(bmd); } if (NewImageBMP != null) { SaveFileDialog saveFileDialog1 = new SaveFileDialog(); if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { string fileExtension = Path.GetExtension(saveFileDialog1.FileName).ToUpper(); ImageFormat imgFormat = ImageFormat.Bmp; if (fileExtension == "JPG") { imgFormat = ImageFormat.Jpeg; } string saveFile = saveFileDialog1.FileName + "." + imgFormat; StreamWriter streamWriter = new StreamWriter(saveFile, false); NewImageBMP.Save(streamWriter.BaseStream, imgFormat); streamWriter.Flush(); streamWriter.Close(); NewImageBMP = null; } } }
/// <summary> /// Get the width of the image /// </summary> /// <param name="ImageMatrix">2D ImageMatrixay that contains the image</param> /// <returns>Image Width</returns> public static int GetWidth(MyColor[,] ImageMatrix) { return ImageMatrix.GetLength(1); }