Articles in this section
Category / Section

How to replace an existing image in Winforms Excel worksheet?

5 mins read

Syncfusion® Excel (XlsIO) library is a .NET Excel library used to create, read, and edit Excel documents. It 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 an existing image in an Excel worksheet:

  1. Information about the existing image, such as its size and position should be collected first.
  2. Then remove the existing image.
  3. Now, add a new image with the old image's 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 excelEngine = new ExcelEngine())
            {
                // Initialize IApplication
                IApplication application = excelEngine.Excel;
                application.DefaultVersion = ExcelVersion.Xlsx;
 
                // Open existing workbook
                IWorkbook workbook = application.Workbooks.Open("../../Data/Sample.xlsx");
                IWorksheet worksheet = workbook.Worksheets[0];
                worksheet["A1"].Text = "The replaced image";
 
                // Gather information about the existing image
                IPictureShape oldImage = worksheet.Pictures[0];
                int leftPosition = oldImage.Left;
                int topPosition = oldImage.Top;
                int height = oldImage.Height;
                int width = oldImage.Width;
 
                // Remove the existing image
                worksheet.Pictures[0].Remove();
 
                // Replace the image with a new one and assign 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
 
            'Open 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"
 
            'Gather information about the 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
 
            'Remove the existing image
            worksheet.Pictures(0).Remove()
 
            'Replace the image with a new one and assign 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.

The following screenshot shows the Excel document with the 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 worksheets or workbooks, organizing and analyzing data through Tables and Pivot Tables, appending multiple records to a 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 XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.

If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion components.

If you have any queries or require clarification, please let us know in the comments below or contact us through our support forumsSupport Tickets, 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  to leave a comment
Access denied
Access denied