Comment ajouter, extraire et supprimer des images des feuilles de calcul | IronXL

How to Add, Extract, and Remove Images from Worksheets

This article was translated from English: Does it need improvement?
Translated
View the article in English

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.

Quickstart: Insert, Extract & Remove Images in One Go

Use IronXL’s intuitive API to add, get, and delete images from worksheets in just a few lines. This example shows how easy it is to insert an image, access it via the Images collection, and then remove it—all without ever touching Interop.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronXL with NuGet Package Manager

    PM > Install-Package IronXL.Excel

  2. Copy and run this code snippet.

    workSheet.InsertImage("logo.png", 1, 1, 3, 3);
    workSheet.RemoveImage(1);
    var firstImage = workSheet.Images[0];
  3. Deploy to test on your live environment

    Start using IronXL in your project today with a free trial
    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);

Veuillez noterThe 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
using IronXL;

WorkBook workBook = WorkBook.Create();
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Insert images
workSheet.InsertImage("ironpdf.jpg", 2, 2, 4, 4);
workSheet.InsertImage("ironpdfIcon.png", 2, 6, 4, 8);

workBook.SaveAs("insertImages.xlsx");
Imports IronXL

Private workBook As WorkBook = WorkBook.Create()
Private workSheet As WorkSheet = workBook.DefaultWorkSheet

' Insert images
workSheet.InsertImage("ironpdf.jpg", 2, 2, 4, 4)
workSheet.InsertImage("ironpdfIcon.png", 2, 6, 4, 8)

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;

WorkBook workBook = WorkBook.Load("insertImages.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Retreive images
List<IronXL.Drawing.Images.IImage> images = workSheet.Images;

// Select each image
foreach (IronXL.Drawing.Images.IImage image in images)
{
    // Save the image
    AnyBitmap anyBitmap = image.ToAnyBitmap();
    anyBitmap.SaveAs($"{image.Id}.png");

    // Resize the image
    image.Resize(1,3);

    // Retrieve image position
    Position position = image.Position;
    Console.WriteLine("top row index: " + position.TopRowIndex);
    Console.WriteLine("bottom row index: " + position.BottomRowIndex);

    // Retrieve byte data
    byte[] imageByte = image.Data;
}

workBook.SaveAs("resizeImage.xlsx");
Imports IronSoftware.Drawing
Imports IronXL
Imports IronXL.Drawing
Imports System
Imports System.Collections.Generic

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

' Retreive images
Private images As List(Of IronXL.Drawing.Images.IImage) = workSheet.Images

' Select each image
For Each image As IronXL.Drawing.Images.IImage In images
	' Save the image
	Dim anyBitmap As AnyBitmap = image.ToAnyBitmap()
	anyBitmap.SaveAs($"{image.Id}.png")

	' Resize the image
	image.Resize(1,3)

	' Retrieve image position
	Dim position As Position = image.Position
	Console.WriteLine("top row index: " & position.TopRowIndex)
	Console.WriteLine("bottom row index: " & position.BottomRowIndex)

	' Retrieve byte data
	Dim imageByte() As Byte = image.Data
Next image

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
using IronXL;

WorkBook workBook = WorkBook.Load("insertImages.xlsx");
WorkSheet workSheet = workBook.DefaultWorkSheet;

// Remove image
workSheet.RemoveImage(3);

workBook.SaveAs("removeImage.xlsx");
Imports IronXL

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

' Remove image
workSheet.RemoveImage(3)

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

Questions Fréquemment Posées

Comment puis-je ajouter des images à une feuille de calcul Excel ?

Pour insérer une image dans une feuille de calcul Excel en utilisant IronXL, utilisez la méthode InsertImage. Cette méthode vous permet de spécifier les dimensions de l'image en définissant les coins supérieur gauche et inférieur droit. Les formats pris en charge incluent JPG, BMP, PNG, GIF et TIFF.

Quels formats d'image sont pris en charge pour l'insertion d'images dans Excel ?

IronXL prend en charge plusieurs formats d'image pour l'insertion dans des feuilles de calcul Excel, notamment JPG/JPEG, BMP, PNG, GIF et TIFF.

Comment extraire des images d'une feuille de calcul Excel ?

Pour extraire des images d'une feuille de calcul Excel en utilisant IronXL, accédez à la propriété Images de la feuille de calcul. Cela fournit une liste de toutes les images, vous permettant d'exporter, de redimensionner et de récupérer leurs positions et données en octets.

Puis-je supprimer des images d'une feuille de calcul Excel ?

Oui, avec IronXL, vous pouvez supprimer des images d'une feuille de calcul Excel en utilisant la méthode RemoveImage. Il suffit de passer l'ID de l'image à cette méthode pour la supprimer.

Qu'est-ce qui est nécessaire pour commencer à manipuler des images dans Excel utilisant C# ?

Pour commencer à manipuler des images dans Excel en utilisant IronXL, téléchargez la bibliothèque C# IronXL depuis NuGet et chargez soit un fichier Excel existant, soit créez-en un nouveau.

Comment les IDs d'image sont-ils générés dans IronXL ?

Dans IronXL, les IDs d'image sont générés selon un modèle impair, tel que 1, 3, 5, 7, et ainsi de suite.

Est-il possible d'exporter des images extraites d'une feuille de calcul Excel ?

Oui, une fois les images extraites en utilisant la propriété Images d'IronXL, elles peuvent être exportées aux formats comme PNG grâce à la méthode Export.

Comment puis-je spécifier la taille d'une image lors de son insertion dans une feuille de calcul Excel ?

Lors de l'insertion d'une image dans une feuille de calcul Excel en utilisant IronXL, spécifiez la taille en fournissant les coins supérieur gauche et inférieur droit de l'image via la méthode InsertImage.

Chaknith Bin
Ingénieur logiciel
Chaknith travaille sur IronXL et IronBarcode. Il a une expertise approfondie en C# et .NET, aidant à améliorer le logiciel et à soutenir les clients. Ses idées issues des interactions avec les utilisateurs contribuent à de meilleurs produits, documentation et expérience globale.
Prêt à commencer?
Nuget Téléchargements 1,686,155 | Version : 2025.11 vient de sortir