private void Settingdetectsectioncopybtn_Click(object sender, EventArgs e) { if (pictureBox1.Image != null) { if (chartstack.Count == 0) { bitmapSource = (Bitmap)pictureBox1.Image; Bitmap chartstacktemp = new Bitmap(pictureBox1.Width, pictureBox1.Height); pictureBox1.DrawToBitmap(chartstacktemp, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height)); chartstack.Add(chartstacktemp); } } if (chartstack.Count > 1) { for (int k = 0; k < list1.Count; k++) { Settingimageentity tempsettingimageeneity = (Settingimageentity)list1[k]; if (tempsettingimageeneity.getparamatername() == paramatername) { list1.RemoveAt(k); } } chartstack.RemoveAt(chartstack.Count - 1); pictureBox1.Image = (Bitmap)chartstack[chartstack.Count - 1]; pointStartX = copyStartX; pointEndX = copyEndX; pointEndY = copyEndY; pointStartY = copyStartY; int iWidth = copyEndX - copyStartX; int iHeight = copyEndY - copyStartY; if (chartstack.Count == 0) { bitmapSource = (Bitmap)pictureBox1.Image; } else { bitmapSource = (Bitmap)chartstack[chartstack.Count - 1]; } if (bitmapSource != null) { bitmap = new Bitmap(bitmapSource); Pen pen = new Pen(Color.White); Graphics gh = Graphics.FromImage(bitmap); rectNew = new Rectangle(pointStartX, pointStartY, iWidth, iHeight); gh.DrawRectangle(pen, rectNew); //this.CreateGraphics().DrawImage(bitmap, pictureBox1.Left, pictureBox1.Top, 500, 500); pictureBox1.Image = bitmap; bitmapSource = bitmap; // Graphics.FromImage(bitmap); // g.DrawImage(bitmap,rectNew); Bitmap newbitmap = new Bitmap(rectNew.Width - 1, rectNew.Height - 1); Bitmap chartstacktemp = new Bitmap(pictureBox1.Width, pictureBox1.Height); pictureBox1.DrawToBitmap(chartstacktemp, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height)); chartstack.Add(chartstacktemp); g = Graphics.FromImage(newbitmap); //g.DrawImage(bitmap, rectNew.Top, rectNew.Left); g.DrawImage(bitmap, pictureBox1.Left - rectNew.Location.X - pictureBox1.Left - 1, pictureBox1.Top - rectNew.Location.Y - pictureBox1.Top - 1); g.Dispose(); //newbitmap.Save("C:\\Users\\zsd\\Desktop\\" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg"); Settingimageentity tempentity = new Settingimageentity(); tempentity.setparamatername(paramatername); tempentity.setimage(newbitmap); tempentity.setparamatertype(paramatertype); list1.Add(tempentity); ((Settingimagedetection)tabControl1.TabPages[tapindexi].Controls[0]).pointStartX = pointStartX; ((Settingimagedetection)tabControl1.TabPages[tapindexi].Controls[0]).pointStartY = pointStartY; ((Settingimagedetection)tabControl1.TabPages[tapindexi].Controls[0]).pointEndX = pointEndX; ((Settingimagedetection)tabControl1.TabPages[tapindexi].Controls[0]).pointEndY = pointEndY; for (int k = 0; k < methodtemp.getsettinglist().Count; k++) { Settingentity settingentity = (Settingentity)methodtemp.getsettinglist()[k]; if (settingentity.getparamatername() == paramatername) { settingentity.getImageSample().setimagesample(rectNew.X * framewidth, rectNew.Y * frameheight, rectNew.Width * framewidth, rectNew.Height * frameheight); break; } } blnDraw = false; neededboxes--; settingdetectsectionneededvaluelabel.Text = neededboxes.ToString(); } } else { pictureBox1.Image = (Bitmap)chartstack[chartstack.Count - 1]; pointStartX = copyStartX; pointEndX = copyEndX; pointEndY = copyEndY; pointStartY = copyStartY; int iWidth = copyEndX - copyStartX; int iHeight = copyEndY - copyStartY; if (chartstack.Count == 0) { bitmapSource = (Bitmap)pictureBox1.Image; } else { bitmapSource = (Bitmap)chartstack[chartstack.Count - 1]; } if (bitmapSource != null) { bitmap = new Bitmap(bitmapSource); Pen pen = new Pen(Color.White); Graphics gh = Graphics.FromImage(bitmap); rectNew = new Rectangle(pointStartX, pointStartY, iWidth, iHeight); gh.DrawRectangle(pen, rectNew); //this.CreateGraphics().DrawImage(bitmap, pictureBox1.Left, pictureBox1.Top, 500, 500); pictureBox1.Image = bitmap; bitmapSource = bitmap; // Graphics.FromImage(bitmap); // g.DrawImage(bitmap,rectNew); Bitmap newbitmap = new Bitmap(rectNew.Width - 1, rectNew.Height - 1); Bitmap chartstacktemp = new Bitmap(pictureBox1.Width, pictureBox1.Height); pictureBox1.DrawToBitmap(chartstacktemp, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height)); chartstack.Add(chartstacktemp); g = Graphics.FromImage(newbitmap); //g.DrawImage(bitmap, rectNew.Top, rectNew.Left); g.DrawImage(bitmap, pictureBox1.Left - rectNew.Location.X - pictureBox1.Left - 1, pictureBox1.Top - rectNew.Location.Y - pictureBox1.Top - 1); g.Dispose(); //newbitmap.Save("C:\\Users\\zsd\\Desktop\\" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg"); Settingimageentity tempentity = new Settingimageentity(); tempentity.setparamatername(tabControl1.TabPages[tapindexi].Name.ToString()); tempentity.setimage(newbitmap); tempentity.setparamatertype(paramatertype); list1.Add(tempentity); ((Settingimagedetection)tabControl1.TabPages[tapindexi].Controls[0]).pointStartX = pointStartX; ((Settingimagedetection)tabControl1.TabPages[tapindexi].Controls[0]).pointStartY = pointStartY; ((Settingimagedetection)tabControl1.TabPages[tapindexi].Controls[0]).pointEndX = pointEndX; ((Settingimagedetection)tabControl1.TabPages[tapindexi].Controls[0]).pointEndY = pointEndY; for (int k = 0; k < methodtemp.getsettinglist().Count; k++) { Settingentity settingentity = (Settingentity)methodtemp.getsettinglist()[k]; if (settingentity.getparamatername() == paramatername) { settingentity.getImageSample().setimagesample(rectNew.X * framewidth, rectNew.Y * frameheight, rectNew.Width * framewidth, rectNew.Height * frameheight); break; } } blnDraw = false; neededboxes--; settingdetectsectionneededvaluelabel.Text = neededboxes.ToString(); } } }
private void upbtn_Click(object sender, EventArgs e) { if (chartstack.Count > 1) { pointStartY = pointStartY - 1; if (pointStartY >= 0) { pointEndY = pointEndY - 1; for (int k = 0; k < list1.Count; k++) { Settingimageentity tempsettingimageeneity = (Settingimageentity)list1[k]; if (tempsettingimageeneity.getparamatername() == paramatername) { list1.RemoveAt(k); } } chartstack.RemoveAt(chartstack.Count - 1); pictureBox1.Image = (Bitmap)chartstack[chartstack.Count - 1]; int iWidth = pointEndX - pointStartX; int iHeight = pointEndY - pointStartY; if (chartstack.Count == 0) { bitmapSource = (Bitmap)pictureBox1.Image; } else { bitmapSource = (Bitmap)chartstack[chartstack.Count - 1]; } if (bitmapSource != null) { //copy the original picture which is stored in bitmapsource and eliminate the old rectangle bitmap = new Bitmap(bitmapSource); Pen pen = new Pen(Color.White); Graphics gh = Graphics.FromImage(bitmap); rectNew = new Rectangle(pointStartX, pointStartY, iWidth, iHeight); //draw rectangle gh.DrawRectangle(pen, rectNew); // show it on the screen //this.CreateGraphics().DrawImage(bitmap, pictureBox1.Left, pictureBox1.Top, 500, 500); pictureBox1.Image = bitmap; bitmapSource = bitmap; // Graphics.FromImage(bitmap); // g.DrawImage(bitmap,rectNew); Bitmap newbitmap = new Bitmap(rectNew.Width - 1, rectNew.Height - 1); Bitmap chartstacktemp = new Bitmap(pictureBox1.Width, pictureBox1.Height); pictureBox1.DrawToBitmap(chartstacktemp, new Rectangle(0, 0, pictureBox1.Width, pictureBox1.Height)); chartstack.Add(chartstacktemp); g = Graphics.FromImage(newbitmap); //g.DrawImage(bitmap, rectNew.Top, rectNew.Left); g.DrawImage(bitmap, pictureBox1.Left - rectNew.Location.X - pictureBox1.Left - 1, pictureBox1.Top - rectNew.Location.Y - pictureBox1.Top - 1); g.Dispose(); //newbitmap.Save("D:\\" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg"); Settingimageentity tempentity = new Settingimageentity(); tempentity.setparamatername(paramatername); tempentity.setimage(newbitmap); tempentity.setparamatertype(paramatertype); list1.Add(tempentity); ((Settingimagedetection)tabControl1.TabPages[tapindexi].Controls[0]).pointStartX = pointStartX; ((Settingimagedetection)tabControl1.TabPages[tapindexi].Controls[0]).pointStartY = pointStartY; ((Settingimagedetection)tabControl1.TabPages[tapindexi].Controls[0]).pointEndX = pointEndX; ((Settingimagedetection)tabControl1.TabPages[tapindexi].Controls[0]).pointEndY = pointEndY; for (int k = 0; k < methodtemp.getsettinglist().Count; k++) { Settingentity settingentity = (Settingentity)methodtemp.getsettinglist()[k]; if (settingentity.getparamatername() == paramatername) { settingentity.getImageSample().setimagesample(rectNew.X * framewidth, rectNew.Y * frameheight, rectNew.Width * framewidth, rectNew.Height * frameheight); break; } } blnDraw = false; } } else { pointStartY = pointStartY + 1; } } }