DEV Community

Alex Antsiferov
Alex Antsiferov

Posted on • Edited on

4 3

Pixel art in Excel

One day, I had this weird idea of displaying an animation in Excel using the cells as pixels.
First of all, I had to check if it was possible at all, and also how fast Excel would render the "pixels".

So I grabbed a 320x200 image of Dangerous Dave here and set off...
To convert a PNG file to CSV I used this PHP tool.

VBA code:

'Canvas reset button
Sub Button2_Click()
    Range("A1:LH200").ColumnWidth = 0.25
    Range("A1:LH200").RowHeight = 2
    Range("A1:LH200").Interior.ColorIndex = 0
End Sub

'Draw button
Sub Button1_Click()
    DrawCSVFile
End Sub

`The draw subroutine itself
Public Sub DrawCSVFile()
    Dim FilePath As String
    FilePath = ActiveWorkbook.Path & "\title1.csv"
    Open FilePath For Input As #1

    Y = 1
    Do Until EOF(1)
        Line Input #1, LineFromFile
        LineItems = Split(LineFromFile, ",")
        For X = 1 To 320
            RGBString = LineItems(X - 1)
            R = Val("&H" & Mid(RGBString, 1, 2))
            G = Val("&H" & Mid(RGBString, 3, 2))
            B = Val("&H" & Mid(RGBString, 5, 2))
            RGBExcel = RGB(R, G, B)
            Cells(Y, X).Interior.Color = RGBExcel
        Next
        Y = Y + 1
    Loop

    Close #1
End Sub
Enter fullscreen mode Exit fullscreen mode

The result: well, it works :)
Dangerous Dave title screen in Excel

But the image above took a full whopping minute to draw.
Reading the CSV line by line and painting individual pixels turned out to be really slow!

I guess some day I should try the range copy method :)

A couple of things I learned along the way:

  • while reading a text file, VBA expects the lines to end with CR or CRLF (in Windows at least); LF is not recognized as the line end
  • RGB in Excel is actually BGR
👋 Kindness is contagious

Please leave your appreciation by commenting on this post!

It takes one minute and is worth it for your career.

Get started

Thank you!

Top comments (0)

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay