get image in Excel cell and write to disk

Hello,
I found this discussion - https://www.syncfusion.com/forums/142131/get-image-in-excel-cell

How do I get the image stream  to save it to disk?

3 Replies 1 reply marked as answer

SK Shamini Kiruba Sobers Syncfusion Team March 10, 2021 07:03 AM UTC

Hi Andrey, 

Greetings from Syncfusion support. 

Kindly look into the following UG link to get the image stream into Excel cell and save it to disk in Xamarin platform. 

Code snippet: 

using (ExcelEngine excelEngine = new ExcelEngine()) 
{ 
    IApplication application = excelEngine.Excel; 
    application.DefaultVersion = ExcelVersion.Excel2013; 
    IWorkbook workbook = application.Workbooks.Create(1); 
    IWorksheet worksheet = workbook.Worksheets[0]; 
 
    //Adding a picture 
    Assembly assembly = typeof(App).GetTypeInfo().Assembly; 
    Stream imageStream = assembly.GetManifestResourceStream("SampleBrowser.XlsIO.Samples.Template.Image.png"); 
    IPictureShape shape = worksheet.Pictures.AddPicture(1, 1, imageStream); 
 
    //Saving the workbook as stream 
    MemoryStream stream = new MemoryStream(); 
    workbook.SaveAs(stream); 
 
    stream.Position = 0; 
 
    //Save the document as file and view the saved document 
    SaveAndroid androidSave = new SaveAndroid(); 
    await androidSave.SaveAndView("AddingImage.xlsx", "application/msexcel", stream, this); 
} 

The operation in SaveAndView under Xamarin varies between Windows Phone, Android and iOS platforms. Please refer the following link for Xamarin.Android code. 

Kindly let us know if this helps. 

Regards, 
Shamini 



AN Andrey March 11, 2021 12:12 AM UTC

Hello, 

That's not what I meant.

There is a ready-made excel file with an image. I need to take an image from excel and save the image to disk.
I am getting images like this:
 foreach (IPictureShape picture in worksheet.Pictures)
                {
                    ShapeImpl shape = picture as ShapeImpl;
                }
How do I get a stream from an IPictureShape?


SK Shamini Kiruba Sobers Syncfusion Team March 11, 2021 11:14 AM UTC

Hi Andrey, 

Thanks for the update. 

You can get the image as a stream from an IPictureShape with the help of the following code snippet. 

Code snippet: 

using (ExcelEngine excelEngine = new ExcelEngine()) 
{ 
    IApplication application = excelEngine.Excel; 
    Assembly assembly = typeof(MainActivity).GetTypeInfo().Assembly; 
    Stream fileStream = assembly.GetManifestResourceStream("CreateExcelSample.Sample.xlsx"); 
    IWorkbook workbook = application.Workbooks.Open(fileStream, ExcelOpenType.Automatic); 
    IWorksheet sheet = workbook.Worksheets[0]; 
 
    IPictureShape picture = sheet.Pictures[0]; 
    Image image = picture.Picture; 
    byte[] byteArray = image.ImageData; 
    MemoryStream stream = new MemoryStream(byteArray); 
    stream.Position = 0; 
 
    //Save the document as file and view the saved document  
    SaveAndroid androidSave = new SaveAndroid(); 
    await androidSave.SaveAndView("OutputImage.png", "application/image", stream, this); 
} 

Kindly let us know if it helps. 

Regards, 
Shamini 


Marked as answer
Loader.
Up arrow icon