Articles in this section
Category / Section

How to replace an existing image in Excel worksheet using C#,VB.NET?

3 mins read

Syncfusion Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. Also, converts Excel documents to PDF files.

This article explains how to replace an existing image in a worksheet with a new image, within the bounds of existing image using C# and VB.NET.

Steps to replace existing image in Excel worksheet:

  1. Information of existing image like image size and position should be collected first.
  2. Then remove the existing image.
  3. Now, add new image with old image size and position.

The following complete code snippet explains this process in C# and VB.NET.

C#

using Syncfusion.XlsIO;
 
namespace ReplaceImage
{
    class Program
    {
        static void Main(string[] args)
        {
            //Initialize ExcelEngine
            using (ExcelEngine = new ExcelEngine())
            {
                //Initialize IApplication
                IApplication application = excelEngine.Excel;
                application.DefaultVersion = ExcelVersion.Xlsx;
 
                //Opening existing workbook
                IWorkbook workbook = application.Workbooks.Open("../../Data/Sample.xlsx");
                IWorksheet worksheet = workbook.Worksheets[0];
                worksheet["A1"].Text = "The replaced image";
 
                //Gathering information of existing image
                IPictureShape oldImage = worksheet.Pictures[0];
                int leftPosition = oldImage.Left;
                int topPosition = oldImage.Top;
                int height = oldImage.Height;
                int width = oldImage.Width;
 
                //Removing the existing image
                worksheet.Pictures[0].Remove();
 
                //Replace the image with new one and assigning its bounds
                IPictureShape newImage = worksheet.Pictures.AddPicture("../../Data/NewImage.png");
                newImage.Left = leftPosition;
                newImage.Top = topPosition;
                newImage.Height = height;
                newImage.Width = width;
 
                //Save the workbook
                workbook.SaveAs("Output.xlsx");
                System.Diagnostics.Process.Start("Output.xlsx");
            }
        }
    }
}

 

VB.NET

Imports Syncfusion.XlsIO
 
Module ReplaceImage
 
    Sub Main()
        'Initialize ExcelEngine
        Using excelEngine As ExcelEngine = New ExcelEngine()
            'Initialize IApplication
            Dim application As IApplication = excelEngine.Excel
            application.DefaultVersion = ExcelVersion.Xlsx
 
            'Opening existing workbook
            Dim workbook As IWorkbook = application.Workbooks.Open("../../Data/Sample.xlsx")
            Dim worksheet As IWorksheet = workbook.Worksheets(0)
            worksheet("A1").Text = "The replaced image"
 
            'Gathering information of existing image
            Dim oldImage As IPictureShape = worksheet.Pictures(0)
            Dim leftPosition As Integer = oldImage.Left
            Dim topPosition As Integer = oldImage.Top
            Dim height As Integer = oldImage.Height
            Dim width As Integer = oldImage.Width
 
            'Removing the existing image
            worksheet.Pictures(0).Remove()
 
            'Replace the image with new one and assigning its bounds
            Dim newImage As IPictureShape = worksheet.Pictures.AddPicture("../../Data/NewImage.png")
            newImage.Left = leftPosition
            newImage.Top = topPosition
            newImage.Height = height
            newImage.Width = width
 
            'Save the workbook
            workbook.SaveAs("Output.xlsx")
            System.Diagnostics.Process.Start("Output.xlsx")
        End Using
    End Sub
End Module

 

A complete working sample can be downloaded from ReplaceImage.zip.

Following screenshot shows the Excel document with existing image.

existing image in WinForms XIsIO

Existing Image

Below screenshot shows the Excel document with replaced image.

replaced image in WinForms XIsIO

Replaced Image

Take a moment to peruse the documentation, where you can find basic worksheet data manipulation options along with features like Conditional Formatting, worksheet calculations through Formulas, adding Charts in worksheet or workbook, organizing and analyzing data through Tables and Pivot Tables, appending multiple records to worksheet using Template Markers, and most importantly PDF and Image conversions etc. with code examples.

Click here to explore the rich set of Syncfusion Excel (XlsIO) library features.

Note:

Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer the link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.

 

Conclusion

I hope you enjoyed learning about how to replace an existing image in Excel worksheet using C#,VB.NET.

You can refer to our WinForms XIsIO’s feature tour page to know about its other groundbreaking feature representations. You can also explore our WinForms XIsIO example to understand how to present and manipulate data.

For current customers, you can check out our WinForms components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our WinForms XIsIO and other WinForms components.

If you have any queries or require clarifications, please let us know in comments below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied