ComponentOne Excel for .NET
In This Topic
    Adding an Image to a Cell
    In This Topic

    Images can be added to a sheet or cell using one of the following methods. Click a link below for detailed steps on performing this task.

    Method 1: Assign an image directly to a cell's XLCell.Value property.

    Using this method, the image is added to the sheet and kept at its original size. The upper left corner of the image coincides with the upper left corner of the specified cell.

    1. Load an existing workbook or add some content to a new workbook.

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Dim wb As New C1XLBook
      wb.Load("C:\Project\WorkBook1.xls")
      

      To write code in C#

      C#
      Copy Code
      C1XLBook wb = new C1XLBook();
      wb.Load(@"C:\Project\WorkBook1.xls");
      
    2. Specify the image and assign it to the cell's Value property.

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Dim img As Image = Image.FromFile("C:\Project\MyImage.bmp")
      Dim sheet As XLSheet = wb.Sheets("Forecasting Report")
      sheet(0, 0).Value = img
      

      To write code in C#

      C#
      Copy Code
      Image img = Image.FromFile(@"C:\Project\MyImage.bmp");
      XLSheet sheet = wb.Sheets("Forecasting Report");
      sheet[0,0].Value = img;
      
    3. Save and open the new book:

      To write code in Visual Basic

      Visual Basic
      Copy Code
      wb.Save("C:\Project\WorkBook1.xls ")
      System.Diagnostics.Process.Start("C:\Project\WorkBook1.xls")
      

      To write code in C#

      C#
      Copy Code
      wb.Save(@"C:\Project\WorkBook1.xls");
      System.Diagnostics.Process.Start(@"C:\Project\WorkBook1.xls");
      

    In this example, the image replaces the value in the first cell, and it appears at its original size in the first cell.


    Method 2: Create an XLPictureShape object, set its properties, and assign it to a cell's XLCell.Value property.

    This second method allows you to customize the image by specifying its size, rotation angle, brightness, contrast, border, and more.
    1. Load an existing workbook or add some content to a new workbook.

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Dim wb As New C1XLBook
      wb.Load("C:\Project\WorkBook1.xls")
      

      To write code in C#

      C#
      Copy Code
      C1XLBook wb = new C1XLBook();
      wb.Load(@"C:\Project\WorkBook1.xls");
      
    2. Create an XLPictureShape object, set some of its properties and assign it to a cell's Value property.

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Dim img As Image = Image.FromFile("C:\Project\MyImage.bmp")
      Dim pic As New XLPictureShape(img, 1500, 1500)
      pic.Rotation = 30.0F
      pic.LineColor = Color.DarkRed
      pic.LineWidth = 100
      ' assign the pic to the first cell of the specified sheet
      Dim sheet As XLSheet = wb.Sheets("Forecasting Report")
      sheet(0, 0).Value = pic
      

      To write code in C#

      C#
      Copy Code
      Image img = Image.FromFile(imageFileName);
      XLPictureShape pic = new XLPictureShape(img, 1500, 1500);
      pic.Rotation = 30.0f;
      pic.LineColor = Color.DarkRed;
      pic.LineWidth = 100;
      // assign the pic to the first cell of the specified sheet
      XLSheet sheet = wb.Sheets("Forecasting Report");
      sheet[0,0].Value = pic;
      
    3. Save and open the book.

      To write code in Visual Basic

      Visual Basic
      Copy Code
      wb.Save("C:\Project\WorkBook1.xls ")
      System.Diagnostics.Process.Start("C:\Project\WorkBook1.xls")
      

      To write code in C#

      C#
      Copy Code
      wb.Save(@"C:\Project\WorkBook1.xls");
      System.Diagnostics.Process.Start(@"C:\Project\WorkBook1.xls");
      

    In this example, the image replaces the value in the first cell, is rotated 30°, and has a dark red border. Since we have specified the horizontal and vertical position of the image, it does not appear in the first cell.


    Method 3: Create an XLPictureShape object, set its properties, and add it to a sheet's ShapeCollection.

    This method uses the XLPictureShape constructor to specify the image boundaries in sheet coordinates. The shape is added directly to the sheet's ShapeCollection, rather than to a specific cell.

    1. Load an existing workbook or add some content to a new workbook.

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Dim wb As New C1XLBook
      wb.Load("C:\Project\WorkBook1.xls")
      

      To write code in C#

      C#
      Copy Code
      C1XLBook wb = new C1XLBook();
      wb.Load(@"C:\Project\WorkBook1.xls");
      
    2. Create an XLPictureShape object, set some of its properties and assign it to a sheet's ShapeCollection.

      To write code in Visual Basic

      Visual Basic
      Copy Code
      Dim img As Image = Image.FromFile("C:\Project\MyImage.bmp")
      Dim pic As New XLPictureShape(img, 3000, 3500, 2500, 900)
      pic.Rotation = 30.0F
      pic.LineColor = Color.DarkRed
      pic.LineWidth = 100
      ' add the pic to specified sheet's ShapeCollection
      Dim sheet As XLSheet = wb.Sheets("Forecasting Report")
      sheet.Shapes.Add(pic)
      

      To write code in C#

      C#
      Copy Code
      Image img = Image.FromFile(@"C:\Project\MyImage.bmp");
      XLPictureShape pic = new XLPictureShape(img, 3000, 3500, 2500, 900);
      pic.Rotation = 30.0f;
      pic.LineColor = Color.DarkRed;
      pic.LineWidth = 100;
      // add the pic to specified sheet's ShapeCollection
      XLSheet sheet = wb.Sheets("Forecasting Report");
      sheet.Shapes.Add(pic)
      
    3. Save and open the book.

      To write code in Visual Basic

      Visual Basic
      Copy Code
      wb.Save("C:\Project\WorkBook1.xls ")
      System.Diagnostics.Process.Start("C:\Project\WorkBook1.xls")
      

      To write code in C#

      C#
      Copy Code
      wb.Save(@"C:\Project\WorkBook1.xls");
      System.Diagnostics.Process.Start(@"C:\Project\WorkBook1.xls");
      

    In this example, the shape was added to the sheet's ShapeCollection; therefore, the image does not replace the value in the first cell. Here we specified the height and width of the image, as well as the horizontal and vertical positioning.