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); } pictureBoxOriginalImage.Image = PgmToBitmap(pgmOriginalImage); if (pictureBoxOriginalImage.Image != null) { inputFileName = openFileDialog1.FileName; trackBarThreshold.Maximum = pgmOriginalImage.MaxVal; trackBarThreshold.Value = trackBarThreshold.Minimum; trackBarThreshold.Enabled = true; outputFileName = null; pictureBoxResultImage.Visible = true; pictureBoxResultImage.Image = 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; } else { inputFileName = null; outputFileName = null; pictureBoxResultImage.Visible = false; pictureBoxOriginalImage.Image = pictureBoxOriginalImage.ErrorImage; trackBarThreshold.Value = trackBarThreshold.Minimum; trackBarThreshold.Enabled = false; labelMagicNumber.Text = "Magic Number:"; labelWidth.Text = "Width:"; labelHieght.Text = "Height:"; labelMaxVal.Text = "Maximum Gray Value:"; labelThreshold.Text = "Threshold:"; } } }
public PgmImage BitmapToPgm(Bitmap monoBmpImage, string magicNumber) { if (monoBmpImage != null) { PgmImage result = new PgmImage(monoBmpImage.Width, monoBmpImage.Height, magicNumber, null, 255); int pixel = 0; for (int i = 0; i < monoBmpImage.Height; ++i) { for (int j = 0; j < monoBmpImage.Width; ++j) { // the image is monochromatic result.Data[pixel++] = monoBmpImage.GetPixel(j, i).R; } } return(result); } return(null); }