News Category

How to Extract Image from Excel Worksheet in C#, VB.NET

2017-04-26 07:38:00 Written by  support iceblue
Rate this item
(0 votes)

In the previous article, we've introduced how to insert image into excel worksheet. In this article, we'll demonstrate how to extract image from Excel worksheet. Please check the below screenshot of the source excel worksheet which contains an image:

How to Extract Image from Excel Worksheet in C#, VB.NET

Code snippet:

Step 1: Instantiate an instance of Workbook class and load the excel file.

Workbook workbook = new Workbook();
workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\test.xlsx");

Step 2: Get the first worksheet.

Worksheet sheet = workbook.Worksheets[0];

Step 3: Get the first picture in the worksheet and save it to disk.

ExcelPicture picture = sheet.Pictures[0];
picture.Picture.Save(@"Image\image.png", ImageFormat.Png);

Screenshot:

How to Extract Image from Excel Worksheet in C#, VB.NET

Full code:

[C#]
using System.Drawing.Imaging;
using Spire.Xls;

namespace Extract_Image
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile(@"C:\Users\Administrator\Desktop\test.xlsx");
            Worksheet sheet = workbook.Worksheets[0];
            ExcelPicture picture = sheet.Pictures[0];
            picture.Picture.Save(@"Image\image.png", ImageFormat.Png);
        }
    }
}
[VB.NET]
Imports System.Drawing.Imaging
Imports Spire.Xls

Namespace Extract_Image
	Class Program
		Private Shared Sub Main(args As String())
			Dim workbook As New Workbook()
			workbook.LoadFromFile("C:\Users\Administrator\Desktop\test.xlsx")
			Dim sheet As Worksheet = workbook.Worksheets(0)
			Dim picture As ExcelPicture = sheet.Pictures(0)
			picture.Picture.Save("Image\image.png", ImageFormat.Png)
		End Sub
	End Class
End Namespace

Additional Info

  • tutorial_title: Extract Image from Excel Worksheet in C#, VB.NET
Last modified on Monday, 06 September 2021 02:25