DEV Community

Alex Antsiferov
Alex Antsiferov

Posted on • Edited on

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

Top comments (0)