goblinfactory / konsole

Home of the simple console library consisting of ProgressBar, Window, Form, Draw & MockConsole (C# console progress bar with support for single or multithreaded progress updates) Window is a 100%-ish console compatible window, supporting all normal console writing to a windowed section of the screen, supporting scrolling and clipping of console output.
719 stars 62 forks source link

Add image support #93

Open 03l54rd1n3 opened 12 months ago

03l54rd1n3 commented 12 months ago

Add support to draw pixel images. On Windows, using a GetConsoleWindow native handle it should be possible to draw direct pixels in the console. Knowing the position of your windows/lines, you could calculate where to position the image instead of drawing characters.

goblinfactory commented 12 months ago

Hi Marius / @03l54rd1n3

I'm not actively spending a lot of time on Konsole, since those who are using don't connect or speak to me often, so I have other projects I have to work on. While I love the project, and still want to finish the high speed writer for OSX and NIX so that I can have a truly cross platform library with scrolling that works on all platforms, there's not a big enough community to justify the effort. (correction, there might be a big community, we've had enough downloads and positive feedback over the years) but not enough interaction to justify much more work.

Regarding adding image support;

I asked chatGPT, "how to render an image inside a C# console application using GetConsoleWindow" It returned with this suggestion below.

I can't add this to the library as a function; but you're welcome to write a community extension and I'll update the documentation to reference it. I've written Konsole so that it can easily be extended using extension methods.

Also, it would cause a world of pain wrt supporting all the hundred ways this will cause problems with different console setups; VSCode terminal, windows, bash, terminal etc etc.

best of luck!

regards

Alan

using System;
using System.Drawing;
using System.Runtime.InteropServices;

class Program
{
    // Import the GetConsoleWindow API
    [DllImport("kernel32.dll", SetLastError = true)]
    static extern IntPtr GetConsoleWindow();

    static void Main()
    {
        // Get the handle of the console window
        IntPtr consoleWindowHandle = GetConsoleWindow();

        // Check if the handle is valid
        if (consoleWindowHandle == IntPtr.Zero)
        {
            Console.WriteLine("Error getting console window handle");
            return;
        }

        // Create graphics object from the window handle
        using (Graphics graphics = Graphics.FromHwnd(consoleWindowHandle))
        {
            // Load the image you want to display
            Image image = Image.FromFile("path_to_your_image.jpg"); // Replace with your image path

            // Draw the image on the console window
            graphics.DrawImage(image, 0, 0, image.Width, image.Height);
        }

        Console.WriteLine("Press any key to exit...");
        Console.ReadKey();
    }
}

An extension method acting on Window is all you need. it should be straight forward to write a `window.DrawImage(path, x, y) where X and Y are column numbers, and you convert the X and Y to appropriate pixels to exactly fit based on some configuration. You'll probably need to ask ChatGPT to help you with a conversion between console X and Y column (char) sizes to graphics pixels depending on DPI and a bunch of stuff. Sounds like fun, let me know how you get on,

Cheers,

Alan

goblinfactory commented 12 months ago

@03l54rd1n3 Some important points to consider;

Check (test) to see if you need to stop all other background threads that are using the console when you use native windows calls. experiment with rendering text at X, Y and then an image at X, Y and again image over the same spot, Do this will some threads and see what happens. Backup your code first you could crash things in windows.

Lastly; I've not tested the code that ChatGPT provided; it could be hallucinating. It may not work at all. Since you mentioned GetconsoleWindow and chatGPT didn't seem to think it was terribly difficult I'm sure you're correct and it should be possible. Im just saying I've not done it, never needed it. Sounds very interesting for text based CMS type solutions. Cheers, Alan

goblinfactory commented 12 months ago

update : btw ...I love this suggestion for graphic images in console apps! ❤️ ( I have put a note on my backlog to create some way of enabling un-supported exeperimental features and I will see if I can add that, and create a dark-arts secret documentation somewhere for anyone that wants to use it.) mmm, nice idea... will be fun!

03l54rd1n3 commented 12 months ago

Hi @goblinfactory, thank you for the very elaborate response. Sorry to hear that there is not enough feedback for you to keep working on this project. I hope your other projects turn out better <3

Haha, love the thought of using ChatGPT. I'll see what I come up with to solve my specific use case. Thank you very much Marius