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:
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:
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