Skip to content
/ Michaël Hompus

Last week I received an invitation for a social work meeting about "Pixel art in spreadsheets". I thought: "How hard can it be". Well, it is about 25 lines of C# 9.0 hard! 😁

Last week I received an invitation for a social work meeting about “Pixel art in spreadsheets”.

I thought: “How hard can it be?”.

Well, it is about 25 lines of C# 9.0 hard! 😀

The code

Using my past experience with EmguCV (OpenCV for .NET) and EPPlus (Excel spreadsheets for .NET), it was quickly built.

Program.cs
var source = args[0];
if (!File.Exists(source))
throw new FileNotFoundException($"Input image not found", source);
var sourceImage = CvInvoke.Imread(source);
var mat = new Mat();
CvInvoke
.ResizeForFrame(sourceImage, mat, new Size(64, 64), Inter.Lanczos4, true);
var image = mat.ToImage<Bgr, byte>();
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
using var package = new ExcelPackage(
new FileInfo(source.Replace(Path.GetExtension(source), ".xlsx"))
);
var worksheet = package.Workbook.Worksheets["Pixcel"]
?? package.Workbook.Worksheets.Add("Pixcel");
for (var y = 1; y <= image.Height; y++)
{
worksheet.Row(y).Height = 27.5;
for (var x = 1; x <= image.Width; x++)
{
worksheet.Column(x).Width = 5;
worksheet.Cells[y, x].Style.Fill.PatternType = ExcelFillStyle.Solid;
worksheet.Cells[y, x].Style.Fill.BackgroundColor.SetColor(
0,
image.Data[y-1, x-1, 2],
image.Data[y-1, x-1, 1],
image.Data[y-1, x-1, 0]
);
}
}
package.Save();

Added wrapping for readability.

The result

Animation of using the Pixcel app by dragging a JPG file over the executable in Windows Explorer
Demonstration of the Pixcel app.

The source

Liking the result, I christened the project Pixcel and shared the code on GitHub.

Filed under C#, Office
Last update: