Excel for WPF | ComponentOne
C1Excel Task-Based Help / Adding an Image to a Cell (WPF)
In This Topic
    Adding an Image to a Cell (WPF)
    In This Topic

    Images can be added to a sheet or cell using one of the following methods.

    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.
      Visual Basic
      Copy Code
      Dim wb As New C1XLBook
      wb.Load("C:\Project\WorkBook1.xls")
      

       

      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.
      Visual Basic
      Copy Code
      Dim img As WriteableBitmap = new WriteableBitmap(new BitmapImage(new Uri("MyImage.bmp", UriKind.Relative)));
      Dim sheet As XLSheet = wb.Sheets("Forecasting Report")
      sheet(0, 0).Value = img
      

       

      C#
      Copy Code
      WriteableBitmap img = new WriteableBitmap(new BitmapImage(new Uri("MyImage.bmp", UriKind.Relative)));
      XLSheet sheet = wb.Sheets["Forecasting Report"];
      sheet[0,0].Value = img;
      
    3. Save and open the new book:
      Visual Basic
      Copy Code
      wb.Save("C:\Project\WorkBook1.xls ")
      System.Diagnostics.Process.Start("C:\Project\WorkBook1.xls")
      

       

      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.

    Excel File

    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.
      Visual Basic
      Copy Code
      Dim wb As New C1XLBook
      wb.Load("C:\Project\WorkBook1.xls")
      

       

      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.
      Visual Basic
      Copy Code
      Dim img As WriteableBitmap
       = new WriteableBitmap(new BitmapImage(new Uri("MyImage.bmp", UriKind.Relative)));
      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
      

       

      C#
      Copy Code
      WriteableBitmap img = new WriteableBitmap(new BitmapImage(new Uri("MyImage.bmp", UriKind.Relative)));
      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.
      Visual Basic
      Copy Code
      wb.Save("C:\Project\WorkBook1.xls ")
      System.Diagnostics.Process.Start("C:\Project\WorkBook1.xls")
      

       

      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.

    Excel File

    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.
      Visual Basic
      Copy Code
      Dim wb As New C1XLBook
      wb.Load("C:\Project\WorkBook1.xls")
      

       

      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.
      Visual Basic
      Copy Code
      Dim img As WriteableBitmap
       = new WriteableBitmap(new BitmapImage(new Uri("MyImage.bmp", UriKind.Relative)));
      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)
      

       

      C#
      Copy Code
      WriteableBitmap img = new WriteableBitmap(new BitmapImage(new Uri("MyImage.bmp", UriKind.Relative)));
      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.
      Visual Basic
      Copy Code
      wb.Save("C:\Project\WorkBook1.xls ")
      System.Diagnostics.Process.Start("C:\Project\WorkBook1.xls")
      

       

      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.

    Excel File Output