public Bitmap PgmToBitmap(PgmImage pgmImage)
        {
            if (pgmImage != null && pgmImage.MagicNumber == "P2")
            {
                int    width  = pgmImage.Width;
                int    height = pgmImage.Height;
                Bitmap result = new Bitmap(width, height);

                Graphics gr = Graphics.FromImage(result);

                int pixel = 0;
                for (int i = 0; i < pgmImage.Height; ++i)
                {
                    for (int j = 0; j < pgmImage.Width; ++j)
                    {
                        int        pixelColor = pgmImage.Data[pixel++];
                        Color      c          = Color.FromArgb(pixelColor, pixelColor, pixelColor);
                        SolidBrush sb         = new SolidBrush(c);
                        gr.FillRectangle(sb, j, i, 1, 1);
                    }
                }

                return(result);
            }
            return(null);
        }
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dlgResult = openFileDialog1.ShowDialog();

            if (dlgResult == DialogResult.OK)
            {
                try {
                    pgmOriginalImage = new PgmImage(openFileDialog1.FileName);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                pbImage.Image = PgmToBitmap(pgmOriginalImage);

                if (pbImage.Image != null)
                {
                    inputFileName = openFileDialog1.FileName;

                    outputFileName = null;

                    labelMagicNumber.Text = "Magic Number: " + pgmOriginalImage.MagicNumber;
                    labelWidth.Text       = "Width: " + pgmOriginalImage.Width + " px";
                    labelHieght.Text      = "Height: " + pgmOriginalImage.Height + " px";
                    labelMaxVal.Text      = "Maximum Gray Value: " + pgmOriginalImage.MaxVal;

                    this.btnOpenInput.Enabled = true;
                    pgmResultImage            = new PgmImage(pgmOriginalImage.Width, pgmOriginalImage.Height, pgmOriginalImage.MagicNumber, "", 0);
                }
                else
                {
                    inputFileName  = null;
                    outputFileName = null;

                    pbImage.Image = pbImage.ErrorImage;

                    labelMagicNumber.Text = "Magic Number:";
                    labelWidth.Text       = "Width:";
                    labelHieght.Text      = "Height:";
                    labelMaxVal.Text      = "Maximum Gray Value:";

                    this.btnOpenInput.Enabled = false;

                    pgmResultImage = null;
                }

                fileToolStripMenuItem.PerformClick();
            }
        }