C#/VB.NET: Extract or Delete Images in Excel

2023-08-18 07:22:00 Written by  support iceblue
Rate this item
(0 votes)

Although Excel files typically store numbers and data, they can also contain images associated with the file. After inserting images into Excel, it may sometimes be necessary to extract the images for use elsewhere, or to remove them to improve simplicity. In this article, you will learn how to programmatically extract or remove images from an Excel document using Spire.XLS for .NET.

Install Spire.XLS for .NET

To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.XLS

Extract Images from Excel in C# and VB.NET

Spire.XLS for .NET allows you to extract all images in an Excel worksheet at once. The following are the detailed steps.

  • Create a Workbook instance.
  • Load a sample Excel file using Workbook.LoadFromFile() method.
  • Get a specific worksheet using Workbook.Worksheets[] property.
  • Loop through to get all pictures in the worksheet using Worksheet.Pictures property.
  • Extract pictures and save them to a specified file path using ExcelPicture.Picture.Save() method.
  • C#
  • VB.NET
using Spire.Xls;

namespace ExtractImages
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook workbook = new Workbook();

            //Load a sample Excel document
            workbook.LoadFromFile("input.xlsx");

            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            //Get all images in the worksheet
            for (int i = sheet.Pictures.Count - 1; i >= 0; i--)
            {
                ExcelPicture picture = sheet.Pictures[i];

                //Extract the images
                picture.Picture.Save(string.Format(@"Image\image_{0}.png", i));
            }
        }
    }
}

C#/VB.NET: Extract or Delete Images in Excel

Delete Images from Excel in C# and VB.NET

To delete pictures from an Excel worksheet, you can use the Worksheet.Pictures.Remove() method. The following are the detailed steps.

  • Create a Workbook instance.
  • Load a sample Excel file using Workbook.LoadFromFile() method.
  • Get a specific worksheet using Workbook.Worksheets[] property.
  • Loop through all pictures in the worksheet and remove them using Worksheet.Pictures.Remove() method.
  • Save the result document using Workbook.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Xls;

namespace DeleteImages
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook workbook = new Workbook();

            //Load a sample Excel document
            workbook.LoadFromFile("input0.xlsx");

            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            //Delete all pictures from the worksheet
            for (int i = sheet.Pictures.Count - 1; i >= 0; i--)
            {
                sheet.Pictures[i].Remove();
            }

            //Save the result document
            workbook.SaveToFile("DeleteImages.xlsx", ExcelVersion.Version2016);
        }
    }
}

C#/VB.NET: Extract or Delete Images in Excel

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Friday, 18 August 2023 01:29