如何在不覆盖已经存在的字符的情况下移动控制台光标的字符?

时间:2018-08-11 11:06:53

标签: c# image console character console-application

我正在尝试向控制台绘制重叠的图像,但是当白色移动光标时,我正在使用的代码会创建空格字符,但这意味着在图像中有空白的地方它会覆盖已经存在的字符。

脚本

using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace ConsoleImage
{
    class Program
    {
        static void Main(string[] args)
        {
            Image Picture = Image.FromFile(@"C:\Users\InfoKriegerX\Pictures\your_image.jpg");
            Console.SetBufferSize((Picture.Width * 0x2), (Picture.Height * 0x2));
            FrameDimension Dimension = new FrameDimension(Picture.FrameDimensionsList[0x0]);
            int FrameCount = Picture.GetFrameCount(Dimension);
            int Left = Console.WindowLeft, Top = Console.WindowTop;
            char[] Chars = { '#', '#', '@', '%', '=', '+', '*', ':', '-', '.', ' ' };
            Picture.SelectActiveFrame(Dimension, 0x0);
            for (int i = 0x0; i < Picture.Height; i++)
            {
                for (int x = 0x0; x < Picture.Width; x++)
                {
                    Color Color = ((Bitmap)Picture).GetPixel(x, i);
                    int Gray = (Color.R + Color.G + Color.B) / 0x3;
                    int Index = (Gray * (Chars.Length - 0x1)) / 0xFF;
                    Console.Write(Chars[Index]);
                }
                Console.Write('\n');
            }
            Console.SetCursorPosition(Left, Top);
            Console.Read();
        }
    }
}

我试图将控制台光标向右移动而不是写一个空白字符,但这会导致控制台输出格式错误。

   if (Chars[Index].Equals(" ")) { Console.SetCursorPosition(Console.WindowLeft + 1, Console.WindowTop); }
                    else
                    {
                        Console.Write(Chars[Index]);
                    }

0 个答案:

没有答案