Add Images to Excel Files

The example below shows how you can add images and position them in various ways, in C# and VB.NET using GemBox.Spreadsheet.

using GemBox.Spreadsheet;

class Program
{
    static void Main()
    {
        // If using the Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

        var workbook = new ExcelFile();
        var worksheet = workbook.Worksheets.Add("Images");

        // Add small BMP image with specified rectangle position.
        worksheet.Pictures.Add("%#SmallImage.bmp%", 50, 50, 48, 48, LengthUnit.Pixel);

        // Add large JPG image with specified top-left cell.
        worksheet.Pictures.Add("%#FragonardReader.jpg%", "B9");

        // Add PNG image with specified top-left and bottom-right cells.
        worksheet.Pictures.Add("%#Dices.png%", "J16", "K20");

        // Add GIF image using anchors.
        var picture = worksheet.Pictures.Add("%#Zahnrad.gif%",
            new AnchorCell(worksheet.Columns[9], worksheet.Rows[21], 100000, 100000),
            new AnchorCell(worksheet.Columns[10], worksheet.Rows[23], 50000, 50000));

        // Set picture's position mode.
        picture.Position.Mode = PositioningMode.Move;

        // Add SVG image with specified top-left cell and size.
        picture = worksheet.Pictures.Add("%#Graphics1.svg%", "J9", 250, 100, LengthUnit.Pixel);

        // Set picture's metadata.
        picture.Metadata.Name = "SVG Image";

        workbook.Save("Images.%OutputFileType%");
    }
}
Imports GemBox.Spreadsheet

Module Program

    Sub Main()

        ' If using the Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")

        Dim workbook = New ExcelFile
        Dim worksheet = workbook.Worksheets.Add("Images")

        ' Add small BMP image with specified rectangle position.
        worksheet.Pictures.Add("%#SmallImage.bmp%", 50, 50, 48, 48, LengthUnit.Pixel)

        ' Add large JPG image with specified top-left cell.
        worksheet.Pictures.Add("%#FragonardReader.jpg%", "B9")

        ' Add PNG image with specified top-left and bottom-right cells.
        worksheet.Pictures.Add("%#Dices.png%", "J16", "K20")

        ' Add GIF image using anchors.
        Dim picture = worksheet.Pictures.Add("%#Zahnrad.gif%",
            New AnchorCell(worksheet.Columns(9), worksheet.Rows(21), 100000, 100000),
            New AnchorCell(worksheet.Columns(10), worksheet.Rows(23), 50000, 50000))

        ' Set picture's position mode.
        picture.Position.Mode = PositioningMode.Move

        ' Add SVG image with specified top-left cell and size.
        picture = worksheet.Pictures.Add("%#Graphics1.svg%", "J9", 250, 100, LengthUnit.Pixel)

        ' Set picture's metadata.
        picture.Metadata.Name = "SVG Image"

        workbook.Save("Images.%OutputFileType%")

    End Sub
End Module
Adding images of various formats and positions to an Excel worksheet from C# and VB.NET
Screenshot of Excel sheet with images

The following example shows how you can add images that will fit into a single cell.

using GemBox.Spreadsheet;
using System;

class Program
{
    static void Main()
    {
        // If using the Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

        var workbook = new ExcelFile();
        var worksheet = workbook.Worksheets.Add("Smileys");

        // Create a sheet with specified columns width and rows height.
        for (int i = 0; i < 6; i++)
        {
            worksheet.Columns[i].SetWidth(10 * (i + 1), LengthUnit.Point);
            worksheet.Rows[i].SetHeight(10 * (i + 1), LengthUnit.Point);
        }

        // Add images that fit inside a single cell.
        foreach (var cell in worksheet.Cells.GetSubrange("A1:F6"))
        {
            var picture = worksheet.Pictures.Add("%#SmilingFace.png%", cell.Name);
            var position = picture.Position;

            double maxWidth = cell.Column.GetWidth(LengthUnit.Point);
            double maxHeight = cell.Row.GetHeight(LengthUnit.Point);

            var ratioX = maxWidth / position.Width;
            var ratioY = maxHeight / position.Height;
            var ratio = Math.Min(ratioX, ratioY);

            if (ratio < 1)
            {
                position.Width *= ratio;
                position.Height *= ratio;
            }
        }

        workbook.Save("CellsImages.%OutputFileType%");
    }
}
Imports GemBox.Spreadsheet
Imports System

Module Program

    Sub Main()

        ' If using the Professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")

        Dim workbook As New ExcelFile()
        Dim worksheet = workbook.Worksheets.Add("Smileys")

        ' Create a sheet with specified columns width and rows height.
        For i As Integer = 0 To 5
            worksheet.Columns(i).SetWidth(10 * (i + 1), LengthUnit.Point)
            worksheet.Rows(i).SetHeight(10 * (i + 1), LengthUnit.Point)
        Next

        ' Add images that fit inside a single cell.
        For Each cell In worksheet.Cells.GetSubrange("A1:F6")
            Dim picture = worksheet.Pictures.Add("%#SmilingFace.png%", cell.Name)
            Dim position = picture.Position

            Dim maxWidth As Double = cell.Column.GetWidth(LengthUnit.Point)
            Dim maxHeight As Double = cell.Row.GetHeight(LengthUnit.Point)

            Dim ratioX = maxWidth / position.Width
            Dim ratioY = maxHeight / position.Height
            Dim ratio = Math.Min(ratioX, ratioY)

            If ratio < 1 Then
                position.Width *= ratio
                position.Height *= ratio
            End If
        Next

        workbook.Save("CellsImages.%OutputFileType%")

    End Sub
End Module
Adding images into individual Excel cells with C# and VB.NET
Screenshot of images inside Excel cells

GemBox.Spreadsheet supports all popular image formats like PNG, JPEG, EXIF, GIF, TIFF, ISO, SVG, EMF, and WMF. However, note that only PNG, JPEG, and EMF images are supported in XLS files (old binary format).

In PDF files, the SVG images are rendered as vector graphics, resulting in smaller file sizes and better quality than bitmap images (PNG, JPEG, BMP, etc.).

See also


Next steps

GemBox.Spreadsheet is a .NET component that enables you to read, write, edit, convert, and print spreadsheet files from your .NET applications using one simple API.

Download Buy