Introduction

By adding images, users can enrich their data with relevant graphics or illustrations. Removing or deleting images simplifies content editing and organization. Additionally, the ability to retrieve images allows for repurposing them in other documents or applications, as well as updating existing images. Collectively, these features enhance user control over images, improve the overall user experience, and enable seamless image manipulation within Excel workbooks.


Get started with IronXL

Start using IronXL in your project today with a free trial.

First Step:
green arrow pointer


Add Images Example

To insert an image into a spreadsheet, utilize the InsertImage method, which supports various image types, such as JPG/JPEG, BMP, PNG, GIF, and TIFF. You must specify the top-left and bottom-right corners of the image to determine its dimensions, calculated by subtracting the column and row values. For example, you can try the following approaches:

  • For a 1x1 image size:
    • worksheet.InsertImage("image.gif", 5, 1, 6, 2);
  • For a 2x2 image size:
    • worksheet.InsertImage("image.gif", 5, 1, 7, 3);

Please note
The generated image IDs follow a pattern of 1, 3, 5, 7, and so on.

:path=/static-assets/excel/content-code-examples/how-to/add-remove-extract-worksheet-images-insert.cs
// Import the IronXL library which is used for Excel operations
using IronXL;

// Create a new Workbook instance
WorkBook workBook = WorkBook.Create();

// Get the default worksheet from the workbook
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Insert an image "ironpdf.jpg" into the worksheet at the specified cell range.
// The parameters in InsertImage specify the file name, starting column index, starting row index, ending column index, and ending row index.
workSheet.InsertImage("ironpdf.jpg", startColumnIndex: 2, startRowIndex: 2, endColumnIndex: 4, endRowIndex: 4);

// Insert another image "ironpdfIcon.png" into the worksheet at a different cell range
workSheet.InsertImage("ironpdfIcon.png", startColumnIndex: 2, startRowIndex: 6, endColumnIndex: 4, endRowIndex: 8);

// Save the workbook to a file named "insertImages.xlsx"
workBook.SaveAs("insertImages.xlsx");
' Import the IronXL library which is used for Excel operations

Imports IronXL



' Create a new Workbook instance

Private workBook As WorkBook = WorkBook.Create()



' Get the default worksheet from the workbook

Private workSheet As WorkSheet = workBook.DefaultWorkSheet



' Insert an image "ironpdf.jpg" into the worksheet at the specified cell range.

' The parameters in InsertImage specify the file name, starting column index, starting row index, ending column index, and ending row index.

workSheet.InsertImage("ironpdf.jpg", startColumnIndex:= 2, startRowIndex:= 2, endColumnIndex:= 4, endRowIndex:= 4)



' Insert another image "ironpdfIcon.png" into the worksheet at a different cell range

workSheet.InsertImage("ironpdfIcon.png", startColumnIndex:= 2, startRowIndex:= 6, endColumnIndex:= 4, endRowIndex:= 8)



' Save the workbook to a file named "insertImages.xlsx"

workBook.SaveAs("insertImages.xlsx")
$vbLabelText   $csharpLabel

Output Spreadsheet

Insert Image

Extract Images Example

To extract images from the selected worksheet, simply 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.

:path=/static-assets/excel/content-code-examples/how-to/add-remove-extract-worksheet-images-extract.cs
using IronSoftware.Drawing;
using IronXL;
using IronXL.Drawing;
using System;
using System.Collections.Generic;

// Load the Excel workbook from a file
var workBook = WorkBook.Load("insertImages.xlsx");

// Get the default worksheet from the workbook
var workSheet = workBook.DefaultWorkSheet;

// Retrieve the list of images from the worksheet
var images = workSheet.Images;

// Iterate over each image in the list
foreach (var image in images)
{
    // Convert the image to AnyBitmap and save it as a PNG file
    AnyBitmap anyBitmap = image.ToAnyBitmap();
    anyBitmap.SaveAs($"{image.Id}.png");

    // Resize the image with width multiplier as 1 and height multiplier as 3
    image.Resize(1, 3);

    // Get the image's position in terms of row and column indexes
    Position position = image.Position;
    Console.WriteLine("Top row index: " + position.TopRowIndex);
    Console.WriteLine("Bottom row index: " + position.BottomRowIndex);

    // Retrieve byte array data of the image
    byte[] imageByte = image.Data;
    // The `imageByte` variable can be used for further processing if needed
}

// Save the modified workbook to a new file
workBook.SaveAs("resizeImage.xlsx");
Imports IronSoftware.Drawing

Imports IronXL

Imports IronXL.Drawing

Imports System

Imports System.Collections.Generic



' Load the Excel workbook from a file

Private workBook = WorkBook.Load("insertImages.xlsx")



' Get the default worksheet from the workbook

Private workSheet = workBook.DefaultWorkSheet



' Retrieve the list of images from the worksheet

Private images = workSheet.Images



' Iterate over each image in the list

For Each image In images

	' Convert the image to AnyBitmap and save it as a PNG file

	Dim anyBitmap As AnyBitmap = image.ToAnyBitmap()

	anyBitmap.SaveAs($"{image.Id}.png")



	' Resize the image with width multiplier as 1 and height multiplier as 3

	image.Resize(1, 3)



	' Get the image's position in terms of row and column indexes

	Dim position As Position = image.Position

	Console.WriteLine("Top row index: " & position.TopRowIndex)

	Console.WriteLine("Bottom row index: " & position.BottomRowIndex)



	' Retrieve byte array data of the image

	Dim imageByte() As Byte = image.Data

	' The `imageByte` variable can be used for further processing if needed

Next image



' Save the modified workbook to a new file

workBook.SaveAs("resizeImage.xlsx")
$vbLabelText   $csharpLabel
Extracted Images
Image Size

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.

:path=/static-assets/excel/content-code-examples/how-to/add-remove-extract-worksheet-images-remove.cs
// This code demonstrates how to remove an image from an Excel worksheet using IronXL.

using IronXL;

// Load the workbook from an existing Excel file.
WorkBook workBook = WorkBook.Load("insertImages.xlsx");

// Get the default worksheet from the workbook.
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Check if the worksheet has enough images before attempting to remove.
// Remove an image from the worksheet by its index.
// Indexing is zero-based, so index 3 refers to the fourth image.
if (workSheet.Images.Count > 3)
{
    workSheet.RemoveImage(3);
    // Informative comment: This will remove the fourth image due to zero-based indexing.
}
else
{
    // Informative comment: If there are fewer than four images, an error would occur, so handle accordingly.
    Console.WriteLine("There are fewer than four images, so the fourth image cannot be removed.");
}

// Save the workbook after the image has been removed.
workBook.SaveAs("removeImage.xlsx");
// The workbook is saved with the name 'removeImage.xlsx' having the fourth image removed if it existed.
' This code demonstrates how to remove an image from an Excel worksheet using IronXL.



Imports IronXL



' Load the workbook from an existing Excel file.

Private workBook As WorkBook = WorkBook.Load("insertImages.xlsx")



' Get the default worksheet from the workbook.

Private workSheet As WorkSheet = workBook.DefaultWorkSheet



' Check if the worksheet has enough images before attempting to remove.

' Remove an image from the worksheet by its index.

' Indexing is zero-based, so index 3 refers to the fourth image.

If workSheet.Images.Count > 3 Then

	workSheet.RemoveImage(3)

	' Informative comment: This will remove the fourth image due to zero-based indexing.

Else

	' Informative comment: If there are fewer than four images, an error would occur, so handle accordingly.

	Console.WriteLine("There are fewer than four images, so the fourth image cannot be removed.")

End If



' Save the workbook after the image has been removed.

workBook.SaveAs("removeImage.xlsx")

' The workbook is saved with the name 'removeImage.xlsx' having the fourth image removed if it existed.
$vbLabelText   $csharpLabel

Frequently Asked Questions

How can I add images to an Excel worksheet using IronXL?

To insert an image into a spreadsheet, use the `InsertImage` method. This method supports various image formats like JPG, BMP, PNG, GIF, and TIFF. You need to specify the top-left and bottom-right corners of the image to set its dimensions.

What image formats are supported by the InsertImage method in IronXL?

The InsertImage method supports multiple image formats including JPG/JPEG, BMP, PNG, GIF, and TIFF.

How do I extract images from an Excel worksheet using IronXL?

To extract images, access the `Images` property of the worksheet. This provides a list of all images, allowing you to export, resize, retrieve positions, and obtain the byte data of each image.

Can I remove images from an Excel worksheet using IronXL?

Yes, you can remove images by passing the image's ID number to the `RemoveImage` method. This will remove the specified image from the worksheet.

What is required to start using IronXL for image manipulation in Excel?

To begin using IronXL for manipulating images in Excel, you need to download the IronXL C# library from NuGet and either load an existing Excel file or create a new one.

How are image IDs generated in IronXL?

Image IDs in IronXL follow an odd-numbered pattern, incrementing in the sequence of 1, 3, 5, 7, and so on.

Is it possible to export images extracted from an Excel worksheet?

Yes, once images are extracted using the `Images` property, they can be exported to formats such as PNG using the `Export` method.

Chaknith related to Remove Image Example
Software Engineer
Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking.