Introduction
This little project is to use Excel worksheet's cells as pixels to render real image. It needs .NET 4.5 and local Excel to run. It's coded in C#/Winform/VS 2013.
Background
Basically, the Winform app picks an image from your hard drive, and launches Excel to render the picked image with worksheet cells.
The brief running process is as below:
- It picks the image and resamples it into the memory according to your UI inputs (units, columns, etc.).
- It launches Excel, makes the spreadsheet cell square-like to simulate a big pixel.
- It scans the resampled image in memory either in a linear way or random way (I prefer the random way, the effect is so fun to watch).
- It changes cell's background color, one by one (in either linear order or random order)/
(This is what the app looks like.)
(It's rendering in Excel in a random way.)
(Rendering is done.)
Using the Code
The code is very straightforward. It uses VS 2013 (or above), .NET 4.5, C# and Winform.
Make sure you have Office Interop Excel referred properly.
Points of Interest
- Running Progress Popup
Originally, I thought I need to do async/await to make a decent progress popup. But you know what, after so many years, this so called "long-run" process handling framework is still hard to use unless you do it on a daily basis. Plus, you have to deal with COM+/Office Interop with this new async/await thing. I have done some experiments, I decide to go with another approach.
It turns out that, just a regular call can handle it decently. Maybe it's because Office Interop, the system already takes care of this kind of async, worker thread, UI thread switching myth. Frankly, I don't know. But it works.
You can see the yellow marked code, no fancy async/await stuff, but still does the trick smoothly.
Obviously the above method still missing a feature: "Cancel
".
To cancel a long run process in the middle, folks, it's not an easy thing to do. It's an advanced topic and case by case (sometimes, you want to rollback, sometimes you just want to kill it...). I'm busy living my own life and this "feature" actually disturbs the main purpose of this little project, so I...
- Performance
If you mess this little app with big size image or super small cells (by change the cell unit size), it will be super slow. Keep in mind that Excel worksheet has limitations (1048576 rows X 16384 cols). But before you even try those numbers, I warned you in my code comments:
- Performance Again
In my experiment, while image rendering, if the "focus" is on Excel, it's slow. But if "focus" is out of Excel, it's faster. Don't ask me why. If you want to figure it out, go nuts, it's all yours. But my humble approach is just to make sure the app is having the focus.
Can you believe it? This is 2017 and .NET 5.0 era (am I right?) and when you want to do some basic things like "switching focus", you still have to rely on almighty WIN32!
- Randomization
I put the default rendering effect is "Randomization". It's fun to watch. It's well documented in my comments. Just a reminder here as well: it's not "real" random method. It's just good enough to cheat on human eyes.
- The Test Image
I put a photo of "this guy"' as test image for your convenience. It's packaged in the source code zip.
That's about it! Just download the source and play around. I think my comments are also good for you to understand the code.
The souce code solution project can be downloaded here.
I also made a YouTube video to show how it runs. Have fun:
History
- 30th August, 2017: Initial version