Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to draw an image in the console using C#? [duplicate]

Tags:

c#

I'm looking for a way to draw an image inside a console window. Similar to how I can call Console.Write("this will display in the console window");. I'm not sure if this is possible or not.

I've played around a little bit without any luck (probably because .NET wants you to use winforms for graphical programming).

Here's a snippet of what I'm trying to do:

using System;
using System.Drawing;

namespace DisplayImage
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var bmp = new Bitmap(100, 100))
            using (var gr = Graphics.FromImage(bmp))
            {
                gr.FillRectangle(Brushes.Orange, new Rectangle(0, 0, bmp.Width, bmp.Height));

                Console.WriteLine("draw image");
                gr.DrawImage(bmp, 1, 1);

                Console.WriteLine("drew image");
            }
        }
    }
}

Is there a way to do this?

Edit

Is there a way to manipulate pixels in a console app? perhaps I could manually read pixels out of an image and draw each individual pixel in a console.

This is just for fun, btw. Not a practical project, just an exercise/exploratory project. Let's hack the console :-)

like image 222
quakkels Avatar asked Dec 23 '13 15:12

quakkels


People also ask

Can we display image in console?

No. You could start a separate dialog/window, however. Console applications are used primarily for text only applications. There is no way to display an image.

How do you write to console in C sharp?

In C# you can write or print to console using Console. WriteLine() or Console. Write(), basically both methods are used to print output of console.


Video Answer


2 Answers

Actually, the answer is "Yes it is, sort of". Take a look at Drawing in a Win32 Console on C++? for native code for drawing on top of the console window.

To do this with C#, you'd most likely need to call down into native code to do the actual work.

And you wouldn't really be drawing in the console window itself, only into a window drawn above it (a subtle, but potentially important difference).

like image 63
David Arno Avatar answered Oct 12 '22 23:10

David Arno


No. It is not possible to draw to the console window. It supports text only. The closest you will get is ASCII art! I would recommend WPF app for graphics

like image 21
geedubb Avatar answered Oct 13 '22 01:10

geedubb