Home > .NET Code Samples > C# Code Sample: Rendering images in Microsoft Excel

C# Code Sample: Rendering images in Microsoft Excel

July 25th, 2010

You may have seen examples of pixel art in Excel like these examples here and here. The way this is done is by setting the background color of each cell of the worksheet just as you would set pixel colors in a graphics editor.

I always wondered how much work it would be to import photographic images into Excel this way. Our code would have to perform these basic steps:

  • Iterate over all lines of the image
  • Iterate over all pixels in each line of the image
  • Get the color of the currently iterated pixel
  • Set the background color of the worksheet cell with the currently iterated x/y coordinates to the color of the image’s pixel
  • Set the column width and row height of the worksheet cell so that it is approximately 1 pixel big
    As it turns out, you can do this with less than 30 lines of C# code:
            private const String _bitmapFile = @"C:\Users\Public\Pictures\Wallpapers\Photo.jpg";
            private const int xlAutomatic = -4105;
            private const int xlSolid = 1;
    
            private void ThisAddIn_Startup(object sender, System.EventArgs e) {
    
                try {
                    Application.ScreenUpdating = false;
    
                    using (Bitmap bitmap = (Bitmap)Bitmap.FromFile(_bitmapFile)) {
                        // Set row heights to 1
                        for (int y = 1; y <= bitmap.Height; y++) {
                            Application.ActiveSheet.Rows[y].RowHeight = 1;
                        }
                        // Set column widths to 0.1
                        for (int x = 1; x <= bitmap.Width; x++) {
                            Application.ActiveSheet.Columns[x].ColumnWidth = 0.1;
                        }
    
                        for (int y = 1; y <= bitmap.Height; y++) {
                            for (int x = 1; x <= bitmap.Width; x++) {
                                var color = bitmap.GetPixel(x - 1, y - 1);
                                var range = Application.ActiveSheet.Cells[y, x];
                                range.Select();
                                dynamic interior = Application.Selection.Interior;
                                interior.Pattern = xlSolid;
                                interior.PatternColorIndex = xlAutomatic;
                                interior.Color = ((int)color.B * 65536) + ((int)color.G * 256) + ((int)color.R);
                                interior.TintAndShade = 0;
                                interior.PatternTintAndShade = 0;
                            }
                        }
                    }
                }
                finally {
                    Application.ScreenUpdating = true;
                }
            }

    Simply create a new Excel 2010 Add-In project in Visual Studio 2010 and paste the above code over the ThisAddIn_Startup method stub. After you have compiled the project Excel will import the image file _bitmapFile on the next startup (give it some time).

Here’s a sample of what it looks like:

Image in Excel

I know it’s not very sophisticated but it was fun to do and I learned something about VSTO 2010. If you’re looking for some really cool stuff to do with Excel I recommend you check out this 3D rendering engine made with Excel.

Categories: .NET Code Samples
Comments are closed.