using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Drawing.Imaging; using System.Runtime.InteropServices; using Projects; namespace ChildForms { public partial class AnglesControl : UserControl { public Color[,] Pixels; public Color[] Colors = new Color[1000]; public Color CurrCol; //private TrekoControl trekoControl1; string AnglePath; int cycle = 0; Treko.Treko _trekoObj; Project _projectObj; Graphics gPreview; public Treko.Treko TrekoObj { get { return _trekoObj; } set { _trekoObj = value; } } public Project ProjectObj { get { return _projectObj; } set { _projectObj = value; } } public AnglesControl() { InitializeComponent(); } private void btnOk_Click(object sender, EventArgs e) { this.FindForm().Visible = false; } private void btnCancel_Click(object sender, EventArgs e) { this.FindForm().Visible = false; } private bool FColor(Color col) { for (int i = 0; i < Colors.Length; i++) { if (Colors[i] == col) return true; } return false; } private void sortColorBar(int n) { Color temp; for (int i = 0; i <= n - 1; i++) { for (int j = i + 1; j <= n; j++) { if (Colors[i].R < Colors[j].R) { temp = Colors[i]; Colors[i] = Colors[j]; Colors[j] = temp; } } } for (int i = 0; i <= n - 1; i++) { for (int j = i + 1; j <= n; j++) { if (Colors[i].G < Colors[j].G) { temp = Colors[i]; Colors[i] = Colors[j]; Colors[j] = temp; } } } for (int i = 0; i <= n - 1; i++) { for (int j = i + 1; j <= n; j++) { if (Colors[i].B < Colors[j].B) { temp = Colors[i]; Colors[i] = Colors[j]; Colors[j] = temp; } } } } private void DrawCurColor() { Color col; ddPictureBox.Image = new Bitmap(AnglePath); Bitmap bmp = new Bitmap(AnglePath); //Bitmap bmp = new Bitmap(new Bitmap(AnglePath), 1000,600); //ddPictureBox.Image = new Bitmap(bmp); PixelFormat fmt = bmp.PixelFormat; byte bpp = (byte)(fmt == PixelFormat.Format32bppArgb ? 4 : 3); Graphics g = Graphics.FromImage(bmp); //g.Clear(CurrCol); // Lock the bitmap's bits. Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat); // Get the address of the first line. IntPtr ptr = bmpData.Scan0; // Declare an array to hold the bytes of the bitmap. int bytes = Math.Abs(bmpData.Stride) * bmp.Height; byte[] rgbValues = new byte[bytes]; int size1 = bmpData.Stride * bmpData.Height; byte[] data = new byte[size1]; // Copy the RGB values into the array. System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes); for (int counter = 2; counter < rgbValues.Length - 1; counter += 3) { //rgbValues[counter] = 255; col = Color.FromArgb(bpp == 4 ? data[counter + 3] : 255, rgbValues[counter - 0], rgbValues[counter - 1], rgbValues[counter - 2]); if (col == CurrCol) { if (cycle == 0) col = Color.White; if (cycle == 1) col = Color.Yellow; if (cycle == 2) col = Color.Magenta; if (cycle == 3) col = Color.MediumSeaGreen; if (cycle == 4) col = Color.Crimson; if (cycle == 5) col = Color.Salmon; rgbValues[counter + 2] = col.B; rgbValues[counter + 1] = col.G; rgbValues[counter + 0] = col.R; //rgbValues[counter + 2] = (byte)(cycle * 50); //rgbValues[counter + 1] = (byte)(cycle * 50); //rgbValues[counter + 0] = (byte)(cycle * 50); } } // Copy the RGB values back to the bitmap System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes); // Unlock the bits. bmp.UnlockBits(bmpData); // Draw the modified image. g.DrawImage(bmp, 0, 0); ddPictureBox.Image = new Bitmap(bmp); ddPictureBox.Refresh(); } private void BlueColBox1_MouseDown(object sender, MouseEventArgs e) { int X, Y; Bitmap bmp = (Bitmap)BlueColBox1.Image; if (e.Button == MouseButtons.Left) { X = e.X; Y = e.Y; if (e.X < 0) X = 0; if (e.X >= BlueColBox1.Width) X = BlueColBox1.Width - 1; if (e.Y < 20) Y = 20; if (e.Y >= BlueColBox1.Height) Y = BlueColBox1.Height - 1; CurrCol = bmp.GetPixel(X, Y); DrawCurColor(); } } private void BlueColBox1_MouseMove(object sender, MouseEventArgs e) { int X, Y; Bitmap bmp = (Bitmap)BlueColBox1.Image; if (e.Button == MouseButtons.Left) { X = e.X; Y = e.Y; if (e.X < 0) X = 0; if (e.X >= BlueColBox1.Width) X = BlueColBox1.Width - 1; if (e.Y < 20) Y = 20; if (e.Y >= BlueColBox1.Height) Y = BlueColBox1.Height - 1; CurrCol = bmp.GetPixel(X, Y); DrawCurColor(); } } private void BildBars(int n) { int width = 30; int height = 50; if (n < 1) n = 1; BlueColBox1.Width = width * (n + 1); BlueColBox1.Height = height; BlueColBox1.Image = new Bitmap(width * (n + 1), height); Bitmap bmp = (Bitmap)BlueColBox1.Image; Graphics g = Graphics.FromImage(bmp); //ddPanBox.Width = bmp.Width; //ddPanBox.Height = bmp.Height; Color col = Color.FromArgb(255, 10, 120, 230); col = Colors[0]; Rectangle rect = new Rectangle(); Pen Pn = new Pen(col, 10); //SolidBrush Br = new SolidBrush(Color.FromArgb(255,0,0,0); Font drawFont1 = new Font("Arial", 9); SolidBrush Br1 = new SolidBrush(Color.Black); Font drawFont2 = new Font("Arial", 8); SolidBrush Br2 = new SolidBrush(Color.White); //Array.Sort(Colors,0,n); for (int i = 0; i <= n; i++) { col = Colors[i]; Pn = new Pen(col, 15); rect.X = i * width; rect.Y = 0; rect.Width = width - 16; rect.Height = height; g.DrawRectangle(Pn, rect); g.DrawString(Convert.ToString(Colors[i].B), drawFont1, Br1, rect.X, rect.Y); g.DrawString(Convert.ToString(Colors[i].B), drawFont2, Br2, rect.X, rect.Y); //g.DrawEllipse(Pn, rect); } BlueColBox1.Refresh(); g.Dispose(); } private void LoadPicture() { int n = 0, j; Color col; OpenFileDialog open_dialog = new OpenFileDialog(); //создание диалогового окна для выбора файла open_dialog.Filter = "Image Files(*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG|All files (*.*)|*.*"; //форматы загружаемых файлов if (open_dialog.ShowDialog() == DialogResult.OK) //Кнопка "ОК" { AnglePath = open_dialog.FileName; button1.Text = AnglePath; try { for (int i = 0; i < Colors.Length; i++) { Colors[i] = Color.FromArgb(255, 0, 0, 0); } // Create a new bitmap. ddPictureBox.Image = new Bitmap(AnglePath); Bitmap bmp = new Bitmap(open_dialog.FileName); PixelFormat fmt = bmp.PixelFormat; byte bpp = (byte)(fmt == PixelFormat.Format32bppArgb ? 4 : 3); Graphics g = Graphics.FromImage(bmp); // Lock the bitmap's bits. Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat); // Get the address of the first line. IntPtr ptr = bmpData.Scan0; // Declare an array to hold the bytes of the bitmap. int bytes = Math.Abs(bmpData.Stride) * bmp.Height; byte[] rgbValues = new byte[bytes]; int size1 = bmpData.Stride * bmpData.Height; byte[] data = new byte[size1]; // Copy the RGB values into the array. System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes); Colors[0] = Color.FromArgb(255, rgbValues[0], rgbValues[1], rgbValues[2]); for (int counter = 2; counter < rgbValues.Length; counter += 3) { //rgbValues[counter] = 255; col = Color.FromArgb(bpp == 4 ? data[counter + 3] : 255, rgbValues[counter], rgbValues[counter - 1], rgbValues[counter - 2]); if (FColor(col) == false) { //if (col.R != 0 || col.G != 0 || col.B != 0) //{ if (col.B < 255) { //n++; Colors[n] = col; n++; } //} } } // Copy the RGB values back to the bitmap System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes); // Unlock the bits. bmp.UnlockBits(bmpData); // Draw the modified image. g.DrawImage(bmp, 0, 0); } catch (Exception d) { DialogResult rezult = MessageBox.Show(d.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } sortColorBar(n - 1); BildBars(n - 1); timer1.Enabled = true; //if (ddPanBoxAnima.Image == null) //{ // _trekoObj.CreateImageTreko(); // ddPanBoxAnima.Image = _trekoObj.ImgDisplayTreko; //} //else //{ //Graphics g = Graphics.FromImage(ddPanBoxAnima.Image); //ddPanBoxAnima.ClearImage(g); //g.Dispose(); //} //_trekoObj.DrawOnImage(_trekoObj.ImgDisplayTreko, _mmToPixelsCoeffPreview); //_trekoObj.DrawOnImage(_trekoObj.ImgDisplayTreko, 10); //ddPanBoxAnima.Refresh(); //if (ddPanBoxAnima.Image == null) //{ // ddPanBoxAnima.Image = _trekoObj.ImgDisplayTreko; // //ddPanBoxTreko3D.ClearImage(); //} //gPreview = Graphics.FromImage(ddPanBoxAnima.Image); //ddPanBoxAnima.ClearImage(gPreview); //ddPanBoxAnima.Refresh(); //timer1.Interval = 100; //timer1.Enabled = true; } } private void button1_Click(object sender, EventArgs e) { LoadPicture(); } private void timer1_Tick(object sender, EventArgs e) { if (cycle++ > 5) cycle = 0; DrawCurColor(); } } }