예제 #1
0
        public async Task CloneMatriz()
        {
            string pathImage = @"C:\Users\j053_\Source\Repos\j053pepe\ImageToExcel\ImageToExcel\test.jpg";
            string Adress    = "";
            Bitmap bmp       = new Bitmap(pathImage);
            List <ColorPosicion> lstMatrix = new List <ColorPosicion>();

            for (int x = 0; x < bmp.Size.Width; x++)
            {
                for (int y = 0; y < bmp.Size.Height; y++)
                //for (int y = 0; y < 20; y++)
                {
                    try
                    {
                        //colors.Add(bmp.GetPixel(x, y));
                        Adress = "";
                        Adress = await Matriz.GetAdress(x) + (y + 1);

                        lstMatrix.Add(new ColorPosicion
                        {
                            ColorOriginal = bmp.GetPixel(x, y),
                            Posicion      = Adress
                        });
                    }
                    catch (Exception errorimagen)
                    {
                        Console.WriteLine(errorimagen.Message);
                    }
                }
            }

            Console.WriteLine($"Items en lista: {lstMatrix.Count}");
        }
예제 #2
0
        public async Task FirstListColors()
        {
            string pathImage = @"C:\Users\j053_\Source\Repos\j053pepe\ImageToExcel\ImageToExcel\test.jpg";
            string Adress    = "";
            Bitmap bmp       = new Bitmap(pathImage);
            List <ColorAgrupado> lstMatrix = new List <ColorAgrupado>();

            for (int x = 0; x < bmp.Size.Width; x++)
            {
                for (int y = 0; y < bmp.Size.Height; y++)
                //for (int y = 0; y < 20; y++)
                {
                    try
                    {
                        //colors.Add(bmp.GetPixel(x, y));
                        Adress = "";
                        Adress = await Matriz.GetAdress(x) + (y + 1);

                        int index = lstMatrix.FindIndex(item => item.ColorOriginal == bmp.GetPixel(x, y));

                        if (index != -1)
                        {
                            lstMatrix[index].Posiciones.Add(Adress);
                        }
                        else
                        {
                            lstMatrix.Add(new ColorAgrupado
                            {
                                ColorOriginal = bmp.GetPixel(x, y),
                                Posiciones    = new List <string> {
                                    Adress
                                }
                            });
                        }
                    }
                    catch (Exception errorimagen)
                    {
                        Console.WriteLine(errorimagen.Message);
                    }
                }
            }

            Console.WriteLine($"Items en lista: {lstMatrix.Count}");
        }
예제 #3
0
        static async Task Main(string[] args)
        {
            Console.WriteLine("Inicio de programa");
            Stopwatch stopwatch = new Stopwatch();

            Console.WriteLine("Timer iniciado");

            try
            {
                List <ColorPosicion> lstMatrix = new List <ColorPosicion>();
                string Adress    = "";
                var    file      = new FileInfo(@"C:\Users\j053_\source\repos\j053pepe\ImageToExcel\ImageToExcelNPOI\myWorkbook3.xlsx");
                string pathImage = @"C:\Users\j053_\source\repos\j053pepe\ImageToExcel\ImageToExcelNPOI\HH&Tv2.jpg";
                if (File.Exists(@"C:\Users\j053_\source\repos\j053pepe\ImageToExcel\ImageToExcelNPOI\myWorkbook3.xlsx"))
                {
                    file.Delete();
                    Console.WriteLine("Archivo eliminado");
                }

                Bitmap bmp = new Bitmap(pathImage);
                stopwatch.Start();

                for (int x = 0; x < bmp.Size.Width; x++)
                {
                    for (int y = 0; y < bmp.Size.Height; y++)
                    //for (int y = 0; y < 20; y++)
                    {
                        try
                        {
                            //colors.Add(bmp.GetPixel(x, y));
                            Adress = "";
                            Adress = await Matriz.GetAdress(x) + (y + 1);

                            lstMatrix.Add(new ColorPosicion
                            {
                                ColorOriginal = bmp.GetPixel(x, y),
                                Posicion      = Adress
                            });
                        }
                        catch (Exception errorimagen)
                        {
                            Console.WriteLine(errorimagen.Message);
                        }
                    }
                }


                using (var package = new ExcelPackage(file))
                {
                    var sheet = package.Workbook.Worksheets.Add("Imagen");
                    sheet.DefaultColWidth = 2.5;

                    lstMatrix.ForEach(item =>
                    {
                        sheet.Cells[item.Posicion].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
                        sheet.Cells[item.Posicion].Style.Fill.BackgroundColor.SetColor(item.ColorOriginal);
                        //using(var range = sheet.Cells[item.Posicion])
                        //{
                        //    range.Style.Fill.BackgroundColor.SetColor(item.ColorOriginal);
                        //}
                    });

                    // Save to file
                    package.Save();
                }

                Console.Beep();
                Console.WriteLine("Archivo creado");
                TimeSpan ts = stopwatch.Elapsed;
                Console.WriteLine($"Tiempo transcurrido: {ts.ToString("mm\\:ss\\.ff")}");
                Console.WriteLine("Presiona una tecla para salir.");
                Console.ReadLine();
            }
            catch (Exception errorgeneral)
            {
                Console.WriteLine(errorgeneral.Message);
                Console.WriteLine(errorgeneral.StackTrace);
                Console.Beep();
                Console.WriteLine("Ocurrio un error. Presiona una tecla para salir.");
                Console.ReadLine();
            }
        }