Display an image in Notepad (C#)

・Like ASCII art?

・It becomes metallic

・Can be created with Visual Studio


Source code

Created as a "C# console application (.NET Framework)"

Replace the namespace part with your project name.

Specify the image file in the "Image file path here" section


using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

namespace (Change here)
{
    public partial class Form1 : Form
    {
        //↓Use your favorite characters(Multibyte characters, in order of density)).
        string[] dchs = { "歌","姫","電", "音", "初", "子", "39", "YY", "01", "ね", "ギ", "♪", "み", "は", "ネ", "ミ", "ク", "く", "つ","3","9","0", "Y", "1" ," " };
        
        //For blank.
        string bg = "・";

        //Scale(1/SCALEx).
        const int SCALE = 5;

        //Gamma Correction(Maybe 3.5 would be just right?).
        const double GAMMA = 3.5;

        //For black and white inversion (select true if the text in Notepad is white, false if it is black).
        bool rev = true;

        public Form1()
        {
            InitializeComponent();
            Bitmap bitmap = new Bitmap(@"Image file path");
            Bitmap bmp = new Bitmap(bitmap,new Size(bitmap.Width / SCALE,bitmap.Height / SCALE));
            BitmapData data = bmp.LockBits(
            new Rectangle(0, 0, bmp.Width, bmp.Height),
            ImageLockMode.ReadWrite,
            PixelFormat.Format32bppArgb);
            byte[] buf = new byte[bmp.Width * bmp.Height * 4];
            Marshal.Copy(data.Scan0, buf, 0, buf.Length);
            Encoding enc = Encoding.GetEncoding("UTF-8");
            StreamWriter writer = new StreamWriter(@"return.txt", false, enc);
            // ファイルを閉じる
            int avr = 0;
            for (int i = 0; i < buf.Length; i += 4)
            {
                avr = 0;
                for (int j = 0; j < 4; j++) avr += buf[i + j];
                if (avr == 0)
                {
                    writer.Write(bg);
                }
                else
                {
                    avr = (int)(256 * 4 * Math.Pow(avr / 256.0 / 4, GAMMA));
                    writer.Write(dchs[rev ? dchs.Length - 1 - avr * dchs.Length / 256 / 4 : avr * dchs.Length / 256 / 4]);
                }
                if(i > 0 && i % (bmp.Width * 4) == 0) writer.Write("\n");
            }
            writer.Close();
            bmp.UnlockBits(data);
        }
    }
}
			

Completed!

Display image in notepad. It becomes metallic<

On Windows, you can change the zoom level by holding down Ctrl and rotating the mouse wheel.

Download execution example (1.97MB)