Add, Extract, and Remove Images in Excel with C#

Add Images Example

To insert an image into a spreadsheet, use the InsertImage method. It supports various image types, such as JPG/JPEG, BMP, PNG, GIF, and TIFF. You need to specify the top-left and bottom-right corners of the image in the worksheet to determine its dimensions. The dimensions are calculated by subtracting the start and end column and row indices.

Here's an example of how to insert images of different sizes:

  • For a 1x1 image size:
// Insert a 1x1 image into the worksheet starting at row 5, column 1
// and ending at row 6, column 2.
worksheet.InsertImage("image.gif", 5, 1, 6, 2);
// Insert a 1x1 image into the worksheet starting at row 5, column 1
// and ending at row 6, column 2.
worksheet.InsertImage("image.gif", 5, 1, 6, 2);
' Insert a 1x1 image into the worksheet starting at row 5, column 1
' and ending at row 6, column 2.
worksheet.InsertImage("image.gif", 5, 1, 6, 2)
$vbLabelText   $csharpLabel
  • For a 2x2 image size:
// Insert a 2x2 image into the worksheet starting at row 5, column 1
// and ending at row 7, column 3.
worksheet.InsertImage("image.gif", 5, 1, 7, 3);
// Insert a 2x2 image into the worksheet starting at row 5, column 1
// and ending at row 7, column 3.
worksheet.InsertImage("image.gif", 5, 1, 7, 3);
' Insert a 2x2 image into the worksheet starting at row 5, column 1
' and ending at row 7, column 3.
worksheet.InsertImage("image.gif", 5, 1, 7, 3)
$vbLabelText   $csharpLabel

Extract Images Example

To extract images from the selected worksheet, access the Images property, which provides a list of all the images contained within the sheet. From this list, you can perform various operations, such as exporting, resizing, retrieving positions, and obtaining the byte data of each image. Notably, the image IDs follow an odd-numbered pattern, incrementing in the sequence of 1, 3, 5, 7, and so on.

Example code for extracting image details might look like this:

// Access the Images collection from the worksheet
var images = worksheet.Images;

// Iterate over the images to perform operations
foreach (var image in images)
{
    // Example: Get the position and size of the image
    var position = image.Position;
    var size = image.Size;

    // Display or process further...
    Console.WriteLine($"Image at position: {position}, size: {size}");
}
// Access the Images collection from the worksheet
var images = worksheet.Images;

// Iterate over the images to perform operations
foreach (var image in images)
{
    // Example: Get the position and size of the image
    var position = image.Position;
    var size = image.Size;

    // Display or process further...
    Console.WriteLine($"Image at position: {position}, size: {size}");
}
' Access the Images collection from the worksheet
Dim images = worksheet.Images

' Iterate over the images to perform operations
For Each image In images
	' Example: Get the position and size of the image
	Dim position = image.Position
	Dim size = image.Size

	' Display or process further...
	Console.WriteLine($"Image at position: {position}, size: {size}")
Next image
$vbLabelText   $csharpLabel

Remove Image Example

Following the extract images example, you can easily remove any inserted image using its corresponding index number. Simply pass the image's ID number to the RemoveImage method to remove it from the worksheet:

// Assume we know the ID of the image we want to remove
int imageIdToRemove = 3; // Example image ID

// Remove the image from the worksheet
worksheet.RemoveImage(imageIdToRemove);
// Assume we know the ID of the image we want to remove
int imageIdToRemove = 3; // Example image ID

// Remove the image from the worksheet
worksheet.RemoveImage(imageIdToRemove);
' Assume we know the ID of the image we want to remove
Dim imageIdToRemove As Integer = 3 ' Example image ID

' Remove the image from the worksheet
worksheet.RemoveImage(imageIdToRemove)
$vbLabelText   $csharpLabel