Let's say I have a VB.net or C# console program that outputs its data to console. Is there a way to change color of a pixel? For example, my resolution is 1600x900. Can I paint pixel (800,600) red? Can I do the same for the active window (paint pixel (300,300) if the console is 400x400 - using coordinates relative to console)? What i want to do is to make a plot of some expression. I have multiple points and their coordinates and i want to draw them in the console by painting pixels. I'm really puzzled how to do this. Any suggestion and help is very much appreciated.
4 Answers
I know this answer is a little late, but in case you still want it, need it, or if someone else wants it, I found some tutorials and other things on stack overflow that led me to an answer. Here it is:
[DllImport("kernel32.dll", EntryPoint = "GetConsoleWindow", SetLastError = true)]
private static extern IntPtr GetConsoleHandle();
IntPtr handler;
Bitmap mainMap;
void FinalDraw()
{
using (var graphics = Graphics.FromHwnd(handler))
graphics.DrawImage(mainMap, new PointF(0, 0));
}
Now just change the pixels inside the Bitmap mainMap, using mainMap.SetPixel(Color), and your all set, just call FinalDraw, and there you go. You will need to set mainMap to a new Bitmap and set the resolution. Finally make sure to install the System.Drawing.Common from the Manage NuGet window in Visual Studios. As well as add using System.Drawing; You may need to add a project reference as well. Good luck and have fun!
Edit:
Youll need to include System.Drawing, and System.Runtime.InteropServices. THe System.Drawing is for the graphics object, and the InteropServices is for the dll imports.
You can also make a reference to a graphics object, so you don't need to constantly use using.
So instead probably create a class that stores all your functions and the graphics object.
Example:
public class Canvas
{
[DllImport("kernel32.dll", EntryPoint = "GetConsoleWindow",
SetLastError = true)]
private static extern IntPtr GetConsoleHandle();
IntPtr handler;
Graphics g;
public Canvas()
{
using(var graphics = Graphics.FromHwnd(handler))
g = graphics;
}
}

- 21
- 4
Here a full example how to draw lines into console window. Build with .Net Framework 4.5.2 and VS-Studio 2015
using System;
using System.Drawing;
using System.Runtime.InteropServices;
namespace ConsoleDraw1
{
class Program
{
[DllImport("kernel32.dll", EntryPoint = "GetConsoleWindow", SetLastError = true)]
private static extern IntPtr GetConsoleHandle();
[DllImport("user32.dll")]
private static extern int GetWindowRect(IntPtr hwnd, out Rectangle rect);
static void Main(string[] args)
{
Console.WindowHeight = 40;
Console.WindowWidth = 132;
Console.CursorVisible = false;
Console.Clear();
IntPtr handle = GetConsoleHandle();
Rectangle rect;
GetWindowRect(handle, out rect);
int width = rect.Width;
int height = rect.Height;
// using (var bmp = new Bitmap(600, 400))
// using (var gfx = Graphics.FromImage(bmp))
using (var gfx = Graphics.FromHwnd(handle))
{
gfx.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
gfx.Clear(Color.Navy);
Random rand = new Random(0);
Pen pen = new Pen(Color.White);
for (int i = 0; i < 10000; i++)
{
pen.Color = Color.FromArgb(rand.Next());
Point pt1 = new Point(rand.Next(width), rand.Next(height));
Point pt2 = new Point(rand.Next(width), rand.Next(height));
gfx.DrawLine(pen, pt1, pt2);
}
// bmp.Save(@"C:\temp\demo.png");
}
Console.ReadKey(true);
}
}
}

- 11
- 1
You can change the Foreground and Background color as you write to the Console, but that's it. You need to use Winforms to accomplish what you want.

- 908
- 2
- 9
- 23
You cant draw a pixels, but you can use colored pseudographics. You can use Win32 functions like WriteConsoleOutput
by p/invoking them. See Advanced Console IO in .NET for example.

- 1
- 1

- 79
- 8