using System; using System.Collections.Generic; using System.Diagnostics; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.Reflection; using System.Windows.Forms; using UTIL; namespace Test_img { public partial class Form1 : Form { Bitmap bm = null; Bitmap newbm = null; //int ter1 = 0; //int ter2 = 0; //int wMax = 0; //int hMax = 0; //int wMaxF = 0; //int hMaxF = 0; //int panelR = 117; //int panelD = 84; int xPos; int yPos; float zoomFactor = 1; Size resolution = new Size(0, 0); Size minform = new Size(666, 606); private Point startingPoint = Point.Empty; private Point movingPoint = Point.Empty; private bool panning = false; private bool select = false; private Point startSelPoint = Point.Empty; private Point stopSelPoint = Point.Empty; private Point SelPoint1 = Point.Empty; private Point SelPoint2 = Point.Empty; private Point MassCenter = Point.Empty; search srch = new search(); int imgsearch = 200; UInt32 fon = 200; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { pictureBox1.MouseWheel += new MouseEventHandler(pictureBox1_MouseWheel); resolution = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Size; img_aspect.Text = $"Аспект: {zoomFactor:F3}"; StaticData.ValueChanged += (sender1, e1) => { imgsearch = StaticData.DataBuffer; //img_srch.Text = StaticData.DataBuffer.ToString(); }; StaticData.SHandlerSearch = new StaticData.SHandler(SearchPixel); pictureBox1.Image = bm = new Bitmap(pictureBox1.Width, pictureBox1.Height); StaticData.TrgEventHandler += SearchPixel2; } private void Form1_SizeChanged(object sender, EventArgs e) { if (this.WindowState != FormWindowState.Maximized) { this.WindowState = FormWindowState.Normal; this.Size = minform; this.Location = new Point(resolution.Width / 2 - minform.Width / 2, resolution.Height / 2 - minform.Height / 2); Console.WriteLine($"{pictureBox1.Size.Width} {pictureBox1.Size.Height} {pictureBox1.Location.X} {pictureBox1.Location.Y}"); this.CenterToScreen(); } else { this.WindowState = FormWindowState.Maximized; this.CenterToScreen(); Console.WriteLine($"{pictureBox1.Size.Width} {pictureBox1.Size.Height} {pictureBox1.Location.X} {pictureBox1.Location.Y}"); } movingPoint = new Point((pictureBox1.Width - bm.Width) / 2, (pictureBox1.Height - bm.Height) / 2); pictureBox1.Invalidate(); } private void pictureBox1_MouseEnter(object sender, EventArgs e) { pictureBox1.MouseWheel += pictureBox1_MouseWheel; pictureBox1.Cursor = Cursors.Cross; } private void pictureBox1_MouseLeave(object sender, EventArgs e) { pictureBox1.MouseWheel -= pictureBox1_MouseWheel; pictureBox1.Cursor = Cursors.Default; } private void pictureBox1_MouseWheel(object sender, MouseEventArgs e) { pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Normal; float zf = zoomFactor; if(bm != null) { if (e.Delta > 0 && zoomFactor * bm.Width < 8100) { zoomFactor *= 1.1F; } if (e.Delta < 0 && zoomFactor * bm.Width > 100) { zoomFactor /= 1.1F; } img_aspect.Text = $"Аспект: {zoomFactor:F3}"; movingPoint.X -= (int)(bm.Width * zoomFactor - bm.Width * zf) / 2; movingPoint.Y -= (int)(bm.Height * zoomFactor - bm.Height * zf) / 2; pictureBox1.Invalidate(); } } private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { //Console.WriteLine($"X={e.X} Y={e.Y} " + // $"w={bm.Width * zoomFactor} h={bm.Height * zoomFactor} " + // размер картинки // $"mpx={movingPoint.X} mpy={movingPoint.Y} " + // картинка в боксе // $"xpos={xPos} ypos={yPos} " + // мышь в боксе // $"mx={xPos - movingPoint.X} my={yPos - movingPoint.Y} " + // мышь в картинке в боксе // $"mx={(xPos - movingPoint.X) / zoomFactor} my={(yPos - movingPoint.Y) / zoomFactor} " + // мышь в картинке в боксе // $"z={zoomFactor}"); select = true; startSelPoint = new Point(e.Location.X, e.Location.Y); stopSelPoint = new Point(e.Location.X, e.Location.Y); int xx = minmax(0, bm.Width - 1, (int)((xPos - movingPoint.X) / zoomFactor)); int yy = minmax(0, bm.Height - 1, (int)((yPos - movingPoint.Y) / zoomFactor)); SelPoint1 = new Point(xx, yy); SelPoint2 = new Point(xx, yy); } if (e.Button == MouseButtons.Middle) { panning = true; startingPoint = new Point(e.Location.X - movingPoint.X, e.Location.Y - movingPoint.Y); } } private void pictureBox1_MouseUp(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { select = false; int xx = minmax(0, bm.Width - 1, (int)((xPos - movingPoint.X) / zoomFactor)); int yy = minmax(0, bm.Height - 1, (int)((yPos - movingPoint.Y) / zoomFactor)); SelPoint2 = new Point(xx, yy); //Console.WriteLine($"x1 {SelPoint1.X} y1 {SelPoint1.Y} x3 {SelPoint2.X} y3 {SelPoint2.Y}"); Rectangle r = nrect(SelPoint1, SelPoint2); Console.WriteLine($"x1 {r.X} y1 {r.Y} x3 {r.X + r.Width} y3 {r.Y + r.Height}"); MassCenter = CalcCenterMass(r); Console.WriteLine($"x {MassCenter.X} y {MassCenter.Y}"); } if (e.Button == MouseButtons.Middle) { panning = false; } } private void pictureBox1_MouseMove(object sender, MouseEventArgs e) { xPos = e.X; // мышь в боксе yPos = e.Y; if (panning) { movingPoint = new Point(e.Location.X - startingPoint.X, e.Location.Y - startingPoint.Y); pictureBox1.Invalidate(); } if (select) { stopSelPoint = new Point(e.Location.X, e.Location.Y); int xx = minmax(0, bm.Width - 1, (int)((xPos - movingPoint.X) / zoomFactor)); int yy = minmax(0, bm.Height - 1, (int)((yPos - movingPoint.Y) / zoomFactor)); SelPoint2 = new Point(xx, yy); //using (Graphics g = pictureBox1.CreateGraphics()) //{ // g.DrawRectangle(new Pen(Color.Green, 1), nrect(startSelPoint, stopSelPoint)); //} //pictureBox1.Invalidate(); } if(bm != null) { int xx = minmax(0, bm.Width - 1, (int)((xPos - movingPoint.X) / zoomFactor)); int yy = minmax(0, bm.Height - 1, (int)((yPos - movingPoint.Y) / zoomFactor)); img_color.Text = $"R:{bm.GetPixel(xx, yy).R:D3} G:{bm.GetPixel(xx, yy).G:D3} B:{bm.GetPixel(xx, yy).B:D3}"; img_coord.Text = $"X = {xx:D4} Y = {yy:D4}"; ttt.Text = $"{movingPoint.X:D4} {movingPoint.Y:D4}"; yyy.Text = $"{xPos:D4} {yPos:D4}"; } } private void pictureBox1_Paint(object sender, PaintEventArgs e) { e.Graphics.Clear(Color.White); if (newbm != null) e.Graphics.DrawImage(ResizeImage(newbm, zoomFactor), movingPoint); if (select) e.Graphics.DrawRectangle(new Pen(Color.Green, 1), nrect(startSelPoint, stopSelPoint)); if (img_center.Checked) { e.Graphics.DrawLine(new Pen(Color.LimeGreen, 1), BmToPb(MassCenter).X, BmToPb(MassCenter).Y - 20, BmToPb(MassCenter).X, BmToPb(MassCenter).Y + 20); e.Graphics.DrawLine(new Pen(Color.LimeGreen, 1), BmToPb(MassCenter).X - 20, BmToPb(MassCenter).Y, BmToPb(MassCenter).X + 20, BmToPb(MassCenter).Y); e.Graphics.DrawRectangle(new Pen(Color.LimeGreen, 1), BmToPb(MassCenter).X - 20, BmToPb(MassCenter).Y - 20, 40, 40); } GC.Collect(); } public Bitmap ResizeImage(Image image, float zoom) { var destRect = new Rectangle(0, 0, (int)(image.Width * zoom), (int)(image.Height * zoom)); var destImage = new Bitmap((int)(image.Width * zoom), (int)(image.Height * zoom)); //destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution); using (var graphics = Graphics.FromImage(destImage)) { graphics.CompositingMode = CompositingMode.SourceCopy; graphics.CompositingQuality = CompositingQuality.HighQuality; if(img_smooth.Checked) graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; else graphics.InterpolationMode = InterpolationMode.NearestNeighbor; graphics.SmoothingMode = SmoothingMode.HighQuality; graphics.PixelOffsetMode = PixelOffsetMode.Half; using (var wrapMode = new ImageAttributes()) { wrapMode.SetWrapMode(WrapMode.TileFlipXY); //graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode); graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel); } } return destImage; } private T minmax(T min, T max, T val) where T : IComparable { if (val.CompareTo(min) <= 0) return min; if (val.CompareTo(max) > 0) return max; return val; } private Rectangle nrect(Point p1, Point p3) { Rectangle r = Rectangle.Empty; Point p2 = new Point(p3.X, p1.Y); Point p4 = new Point(p1.X, p3.Y); if (p1.X < p3.X && p1.Y < p3.Y) return new Rectangle(p1, new Size(p3.X - p1.X, p3.Y - p1.Y)); if (p1.X < p3.X && p1.Y > p3.Y) return new Rectangle(p4, new Size(p3.X - p4.X, p1.Y - p4.Y)); if (p1.X > p3.X && p1.Y < p3.Y) return new Rectangle(p2, new Size(p1.X - p2.X, p3.Y - p2.Y)); if (p1.X > p3.X && p1.Y > p3.Y) return new Rectangle(p3, new Size(p4.X - p3.X, p2.Y - p3.Y)); return r; } private void SearchPixel() { List points = new List(); Stopwatch stopwatch = Stopwatch.StartNew(); UnsafeBitmap btm = new UnsafeBitmap(bm); btm.LockBitmap(); for (int y = 0; y < bm.Height; y++) { for (int x = 0; x < bm.Width; x++) { PixelData c = btm.GetPixel(x, y); if (c.blue >= imgsearch) { points.Add(new Point(x, y)); //Console.WriteLine($"{x} {y}"); } } } stopwatch.Stop(); Console.WriteLine(stopwatch.Elapsed.ToString()); btm.Dispose(); using (Graphics g = Graphics.FromImage(newbm)) { //g.Clear(Color.White); for (int i = 0; i < points.Count; i++) g.DrawEllipse(new Pen(Color.Red), points[i].X - 5, points[i].Y - 5, 10, 10); } pictureBox1.Invalidate(); } private Point CalcCenterMass(Rectangle r) { Int32 sX = 0; Int32 sY = 0; Int32 sZ = 0; if (r.Width != 0 && r.Height != 0) { UnsafeBitmap btm = new UnsafeBitmap(bm); btm.LockBitmap(); Int32 num = 1; for (int y = r.Y; y < r.Bottom; y++) { for (int x = r.X; x < r.Right; x++) { PixelData c = btm.GetPixel(x, y); if (minus_fon.Checked) { sX += (Int32)((c.blue - fon) * x); sY += (Int32)((c.blue - fon) * y); sZ += (Int32)(c.blue - fon); } else { sX += (Int32)(c.blue * x); sY += (Int32)(c.blue * y); sZ += (Int32)(c.blue); } num++; } } //sX /= 255; //sY /= 255; //sX /= num - 1; //sY /= num - 1; sX /= sZ; sY /= sZ; btm.Dispose(); Console.WriteLine($"num: {num} num2: {r.Width * r.Height} x: {sX} y: {sY}"); return new Point(sX, sY); } else return Point.Empty; } private Point BmToPb(Point p) { return new Point((int)(p.X * zoomFactor + movingPoint.X), (int)(p.Y * zoomFactor + movingPoint.Y)); } #region Меню private void img_load_Click(object sender, EventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "bitmap files (*.bmp)|*.bmp|All files (*.*)|*.*"; if (ofd.ShowDialog() != DialogResult.OK) return; bm = (Bitmap)Image.FromFile(ofd.FileNames[0]); newbm = (Bitmap)bm.Clone(); img_resolution.Text = $"{bm.Width} X {bm.Height}"; this.CenterToScreen(); zoomFactor = (float)pictureBox1.Width / (float)bm.Width; zoomFactor = 1; img_aspect.Text = $"Аспект: {zoomFactor:F3}"; movingPoint = new Point((pictureBox1.Width - bm.Width) / 2, (pictureBox1.Height - bm.Height) / 2); pictureBox1.Invalidate(); //pictureBox1.Image = ZoomControl(bm, zoomFactor); } private void exit_Click(object sender, EventArgs e) { Application.Exit(); } private void развернутьToolStripMenuItem_Click(object sender, EventArgs e) { this.WindowState = FormWindowState.Maximized; this.CenterToScreen(); movingPoint = new Point((pictureBox1.Width - bm.Width) / 2, (pictureBox1.Height - bm.Height) / 2); pictureBox1.Invalidate(); img_aspect.Text = $"Аспект: {zoomFactor:F3}"; } private void свернутьToolStripMenuItem_Click(object sender, EventArgs e) { this.WindowState = FormWindowState.Normal; this.Size = new Size(666, 606); this.CenterToScreen(); movingPoint = new Point((pictureBox1.Width - bm.Width) / 2, (pictureBox1.Height - bm.Height) / 2); pictureBox1.Invalidate(); img_aspect.Text = $"Аспект: {zoomFactor:F3}"; } private void img_search_Click(object sender, EventArgs e) { if (img_search.Checked) { img_search.Image = Properties.Resources.CheckBoxChecked; srch.Show(); StaticData.Fon?.Invoke(fon); } else { img_search.Image = Properties.Resources.CheckBoxUnchecked; srch.Hide(); } } private void img_save_Click(object sender, EventArgs e) { SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "bitmap files (*.bmp)|*.bmp|All files (*.*)|*.*"; if (sfd.ShowDialog() != DialogResult.OK) return; lock (this) { newbm.Save(sfd.FileName, ImageFormat.Bmp); } } private void screenshot_Click(object sender, EventArgs e) { using (Bitmap bitmap = new Bitmap(pictureBox1.Width, pictureBox1.Height)) { using (Graphics g = Graphics.FromImage(bitmap)) { g.CopyFromScreen(new Point(this.Bounds.Left + pictureBox1.Left + 9, this.Bounds.Top + pictureBox1.Top + 32), Point.Empty, pictureBox1.Size); } SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "bitmap files (*.bmp)|*.bmp|All files (*.*)|*.*"; if (sfd.ShowDialog() != DialogResult.OK) return; bitmap.Save(sfd.FileName, ImageFormat.Bmp); } } private void img_smooth_Click(object sender, EventArgs e) { if (img_smooth.Checked) img_smooth.Image = Properties.Resources.CheckBoxChecked; else img_smooth.Image = Properties.Resources.CheckBoxUnchecked; pictureBox1.Invalidate(); } #endregion #region Загрузка тестовых картинок private void img1_Click(object sender, EventArgs e) { bm = Properties.Resources.olo_02; newbm = (Bitmap)bm.Clone(); img_resolution.Text = $"{bm.Width} X {bm.Height}"; this.CenterToScreen(); zoomFactor = 1; img_aspect.Text = $"Аспект: {zoomFactor:F3}"; movingPoint = new Point((pictureBox1.Width - bm.Width) / 2, (pictureBox1.Height - bm.Height) / 2); pictureBox1.Invalidate(); imgfon(); } private void img2_Click(object sender, EventArgs e) { bm = Properties.Resources.olo_03; newbm = (Bitmap)bm.Clone(); img_resolution.Text = $"{bm.Width} X {bm.Height}"; this.CenterToScreen(); zoomFactor = 1; img_aspect.Text = $"Аспект: {zoomFactor:F3}"; movingPoint = new Point((pictureBox1.Width - bm.Width) / 2, (pictureBox1.Height - bm.Height) / 2); pictureBox1.Invalidate(); imgfon(); } private void img3_Click(object sender, EventArgs e) { bm = Properties.Resources.t1_320x256; newbm = (Bitmap)bm.Clone(); img_resolution.Text = $"{bm.Width} X {bm.Height}"; this.CenterToScreen(); zoomFactor = 1; img_aspect.Text = $"Аспект: {zoomFactor:F3}"; movingPoint = new Point((pictureBox1.Width - bm.Width) / 2, (pictureBox1.Height - bm.Height) / 2); pictureBox1.Invalidate(); imgfon(); } #endregion private void SearchPixel2() { } private void img_fon_Click(object sender, EventArgs e) { UnsafeBitmap btm = new UnsafeBitmap(bm); btm.LockBitmap(); UInt32 sc = 0; for (int y = 0; y < bm.Height; y++) { PixelData c = btm.GetPixel(y, y); sc += (UInt32)(c.blue * c.blue); c = btm.GetPixel(bm.Width - 1 - y, y); sc += (UInt32)(c.blue * c.blue); } Console.WriteLine(sc.ToString()); sc = (UInt32)Math.Sqrt(sc / bm.Height / 2); Console.WriteLine(sc.ToString()); img_srch.Text = $"Фон: {sc}"; btm.UnlockBitmap(); btm.Dispose(); fon = sc; StaticData.Fon?.Invoke(sc); } private void imgfon() { UnsafeBitmap btm = new UnsafeBitmap(bm); btm.LockBitmap(); UInt32 sc = 0; for (int y = 0; y < bm.Height; y++) { PixelData c = btm.GetPixel(y, y); sc += (UInt32)(c.blue * c.blue); c = btm.GetPixel(bm.Width - 1 - y, y); sc += (UInt32)(c.blue * c.blue); } Console.WriteLine(sc.ToString()); sc = (UInt32)Math.Sqrt(sc / bm.Height / 2); Console.WriteLine(sc.ToString()); img_srch.Text = $"Фон: {sc}"; btm.UnlockBitmap(); btm.Dispose(); fon = sc; StaticData.Fon?.Invoke(sc); } private void img_timer_Click(object sender, EventArgs e) { if (img_timer.Checked) { img_timer.Image = Properties.Resources.CheckBoxChecked; timer1.Enabled = true; } else { img_timer.Image = Properties.Resources.CheckBoxUnchecked; timer1.Enabled = false; } } private void timer1_Tick(object sender, EventArgs e) { newbm = (Bitmap)bm.Clone(); if (img_search.Checked) { SearchPixel(); } pictureBox1.Invalidate(); } private void img_center_Click(object sender, EventArgs e) { if (img_center.Checked) { img_center.Image = Properties.Resources.CheckBoxChecked; } else { img_center.Image = Properties.Resources.CheckBoxUnchecked; } } private void minus_fon_Click(object sender, EventArgs e) { if (minus_fon.Checked) { minus_fon.Image = Properties.Resources.CheckBoxChecked; } else { minus_fon.Image = Properties.Resources.CheckBoxUnchecked; } } } }