private void button1_Click(object sender, EventArgs e) // Open image { label1.Visible = false; label2.Visible = false; label3.Visible = false; OpenFileDialog openFileDialog1 = new OpenFileDialog(); if (openFileDialog1.ShowDialog() == DialogResult.OK) { try { OrigBmp = new Bitmap(openFileDialog1.FileName); OpenImageFile = openFileDialog1.FileName; Number = 0; } catch (Exception ex) { MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message); } } else { return; } double ScaleX = (double)pictureBox1.Width / OrigBmp.Width; double ScaleY = (double)pictureBox1.Height / OrigBmp.Height; if (ScaleX < ScaleY) { Scale1 = ScaleX; } else { Scale1 = ScaleY; } Drawn = false; progressBar1.Maximum = 100; progressBar1.Value = 0; progressBar1.Step = 1; progressBar1.Visible = true; label1.Text = "Opened image: " + OpenImageFile; label1.Visible = true; OrigIm = new CImage(OrigBmp.Width, OrigBmp.Height, 24); OrigIm.denomProg = progressBar1.Maximum / progressBar1.Step; OrigIm.nLoop = 2; if (OrigBmp.PixelFormat == PixelFormat.Format8bppIndexed) { OrigIm.BitmapToImageOld(OrigBmp, this); Indexed = true; } else if (OrigBmp.PixelFormat == PixelFormat.Format24bppRgb) { OrigIm.BitmapToImage(OrigBmp, this); Indexed = false; } else { MessageBox.Show("Inappropriate pixel format=" + OrigBmp.PixelFormat); return; } BmpPictBox1 = new Bitmap(OrigBmp.Width, OrigBmp.Height, PixelFormat.Format24bppRgb); if (Indexed) { OrigIm.ImageToBitmapOld(BmpPictBox1, this); } else { OrigIm.ImageToBitmap(BmpPictBox1, this); } pictureBox1.Image = BmpPictBox1; g1 = Graphics.FromImage(BmpPictBox1); BmpPictBox2 = new Bitmap(OrigBmp.Width, OrigBmp.Height, PixelFormat.Format24bppRgb); pictureBox2.Image = BmpPictBox2; g2 = Graphics.FromImage(BmpPictBox2); pictureBox1.Image = OrigBmp; progressBar1.Visible = false; progressBar1.Maximum = 100; progressBar1.Step = 1; OrigIm.denomProg = progressBar1.Maximum / progressBar1.Step; ResultIm = new CImage(OrigBmp.Width, OrigBmp.Height, 24); marginX = (pictureBox1.Width - (int)(Scale1 * OrigIm.width)) / 2; // space left of the image marginY = (pictureBox1.Height - (int)(Scale1 * OrigIm.height)) / 2; // space above the image label2.Visible = true; OPEN = true; CLICK = false; } //********************************* end Open image *********************************
} //**************************** end MouseClick ********************************************* private void button2_Click(object sender, EventArgs e) // Straighten { if (!CLICK) { MessageBox.Show("Click the button 'Click corners' and clockwise the corners of the painting starting with the lower left one"); return; } if (Number != 4 || !CORNERS) { MessageBox.Show("Click clockwise the corners of the painting starting with the lower-left one"); return; } OrigIm.nLoop = 2; ResultIm.denomProg = 100; ResultIm.nLoop = 2; if (radioButton1.Checked) { CUT = true; CUTCHOSEN = true; } else if (radioButton2.Checked) { CUT = false; CUTCHOSEN = true; } if (!CUTCHOSEN) { MessageBox.Show("Decide whether removing the background"); return; } if (CUT) { OrigIm.Rect_Optimal(v, CUT, ref ResultIm); } else { if (radioButton3.Checked) { Rel = 0.1; } if (radioButton4.Checked) { Rel = 0.15; } if (radioButton5.Checked) { Rel = 0.20; } OrigIm.Rect_Retain(v, CUT, Rel, ref ResultIm); // 'Rel' is the part of background } ResultBmp = new Bitmap(ResultIm.width, ResultIm.height, PixelFormat.Format24bppRgb); ResultIm.ImageToBitmap(ResultBmp, this); progressBar1.Visible = false; pictureBox2.Image = ResultBmp; BmpPictBox1 = new Bitmap(ResultIm.width, ResultIm.height, PixelFormat.Format24bppRgb); label3.Visible = true; RECTIFIED = true; } //******************************* end Straighten *************************************