Exemplo n.º 1
0
        public static void Main(string[] args)
        {
            // Maximize console
            NativeMethods.ShowWindow(Process.GetCurrentProcess().MainWindowHandle, 3); //SW_MAXIMIZE = 3
            // Set flag for colorful output
            var handle = NativeMethods.GetStdHandle(-11);                              // STD_OUTPUT_HANDLE = -11

            NativeMethods.GetConsoleMode(handle, out int mode);
            NativeMethods.SetConsoleMode(handle, mode | 0x4); // ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x4
            // Save original foreground color
            var consoleColor = Console.ForegroundColor;

            // Hide cursor
            Console.CursorVisible = false;

            string filename;
            int    hScale = 0;

            if (args.Length != 2)
            {
                // Load image path
                OpenFileDialog ofd = new OpenFileDialog
                {
                    CheckFileExists = true,
                    CheckPathExists = true,
                    Filter          = "Image(*.jpg;*.jpeg;*.png;*.bmp;*.ico;*.tiff;*.gif)|*.jpg;*.jpeg;*.png;*.bmp;*.ico;*.tiff;*.gif|All files(*.*)|*.*",
                    Multiselect     = false,
                    Title           = "Select an image to process..."
                };
                ofd.ShowDialog();
                if (string.IsNullOrEmpty(ofd.FileName))
                {
                    return;
                }
                filename = ofd.FileName;

                // Provide suggested scale
                int suggestedHScale = 1;
                using (Bitmap bmp = new Bitmap(filename))
                {
                    int suggestedHScaleByWidth  = (int)Math.Ceiling((double)bmp.Width / Console.WindowWidth);
                    int suggestedHScaleByHeight = (int)Math.Ceiling((double)bmp.Height / Console.WindowHeight / 2);
                    suggestedHScale = Math.Max(suggestedHScaleByWidth, suggestedHScaleByHeight);
                }

                // Set scale
                Application.EnableVisualStyles();
                ScaleBox sb = new ScaleBox(suggestedHScale);
                Application.Run(sb);
                if (sb.DialogResult != DialogResult.OK)
                {
                    return;
                }
                hScale = sb.HScale;
            }
            else // Input arguments
            {
                // Load image path
                filename = args[0];
                if (!File.Exists(filename))
                {
                    Console.WriteLine("Failed: File not found.");
                    Environment.Exit(-1);
                }

                // Set scale
                bool validScale = int.TryParse(args[1], out hScale);
                if (!validScale)
                {
                    Console.WriteLine("Failed: Scale should be positive integer.");
                    Environment.Exit(-2);
                }
            }
            int vScale = hScale * 2; // Good for console output

            // Draw image
            try
            {
                using (Bitmap bmp = new Bitmap(filename))
                {
                    FrameDimension fd         = new FrameDimension(bmp.FrameDimensionsList[0]);
                    int            frameCount = bmp.GetFrameCount(fd);
                    // For GIF
                    if (frameCount > 1)
                    {
                        int i = 0;
                        while (i <= frameCount) // Loop playback
                        {
                            for (i = 0; i < frameCount; i++)
                            {
                                bmp.SelectActiveFrame(fd, i);
                                GetChars(bmp, hScale, vScale, true);
                            }
                        }
                    }
                    // For other image formats
                    else
                    {
                        GetChars(bmp, hScale, vScale, true);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }

            // Recover color
            Console.ForegroundColor = consoleColor;

            Console.WriteLine("\nPress any key to exit...");
            Console.ReadKey();
        }
Exemplo n.º 2
0
        public static void Main(string[] args)
        {
            #region Set console

            // Maximize console
            NativeMethods.ShowWindow(Process.GetCurrentProcess().MainWindowHandle, 3); //SW_MAXIMIZE = 3
            // Set flag for colorful output
            var handle = NativeMethods.GetStdHandle(-11);                              // STD_OUTPUT_HANDLE = -11
            NativeMethods.GetConsoleMode(handle, out int mode);
            NativeMethods.SetConsoleMode(handle, mode | 0x4);                          // ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x4

            #endregion Set console

            // Load image or video path
            OpenFileDialog ofd = new OpenFileDialog
            {
                CheckFileExists = true,
                CheckPathExists = true,
                Filter          = string.Format("Media({0};{1})|{0};{1}", ImageConverter.FilterString, VideoConverter.FilterString),
                Multiselect     = false,
                Title           = "Select an image or video to convert..."
            };
            ofd.ShowDialog();
            if (string.IsNullOrEmpty(ofd.FileName))
            {
                return;
            }
            string filename = ofd.FileName;

            // Set converter mode
            ConverterMode converterMode = ConverterMode.Unsupported;
            string        extension     = new FileInfo(filename).Extension;
            if (VideoConverter.SupportedFormats.Contains(extension))
            {
                converterMode = ConverterMode.Video;
            }
            else if (ImageConverter.SupportedFormats.Contains(extension))
            {
                converterMode = ConverterMode.Image;
            }

            // Draw
            Application.EnableVisualStyles();
            ScaleBox scaleBox;
            switch (converterMode)
            {
            case ConverterMode.Image:
                ImageConverter ic = new ImageConverter(filename);
                if (!ic.IsValid)
                {
                    break;
                }
                scaleBox = new ScaleBox(ic.GetSuggestedHScale());
                Application.Run(scaleBox);
                if (scaleBox.DialogResult != DialogResult.OK)
                {
                    return;
                }
                ic.SetScale(scaleBox.HScale);
                ic.Draw();
                break;

            case ConverterMode.Video:
                VideoConverter vc = new VideoConverter(filename);
                scaleBox = new ScaleBox(vc.GetSuggestedHScale());
                Application.Run(scaleBox);
                if (scaleBox.DialogResult != DialogResult.OK)
                {
                    return;
                }
                vc.SetScale(scaleBox.HScale);
                vc.CreatePipe();
                vc.Decode();
                break;

            default:
                Console.WriteLine($"\x1b[38;2;200;200;200mUnsupported format.");
                break;
            }

            // Exit
            Console.WriteLine($"\x1b[38;2;200;200;200mPress any key to exit...");
            Console.ReadKey();
        }