Spread Windows Forms 17
Spread Windows Forms 17.0 Product Documentation / Developer's Guide / Cells / Adding Image in a Cell
In This Topic
    Adding Image in a Cell
    In This Topic

    Spread for Winforms provides the ability to insert images in a cell as a value. This helps to add images in a cell without converting the cell into a ImageCellType class object.

    To add images using ImageCellType, refer to Setting an Image Cell topic.

    You can use one of the following methods to display images in a cell. These methods help to set the image object to the cell value, set a local image file path, or set a base64-encoded image string in cells:

    C#
    Copy Code
    private void CellImageAsAttribute_Load(object sender, EventArgs e)
    {
        IWorksheet TestActiveSheet = fpSpread1.AsWorkbook().ActiveSheet;
        
        fpSpread1.AsWorkbook().WorkbookSet.CalculationEngine.CalcFeatures = CalcFeatures.All;
        fpSpread1.LegacyBehaviors = FarPoint.Win.Spread.LegacyBehaviors.None;
    
        // Displaying cell image using attribute
        GrapeCity.CalcEngine.RichValue<Country> ct = new GrapeCity.CalcEngine.RichValue<Country>(new Country()
        {
            Name = "India",
            Capital = "New Delhi",
        });
        ct.ShowDetailsIcon = true;
    
        TestActiveSheet.Cells["A1"].Value = ct;
    
        TestActiveSheet.Columns[0].ColumnWidth = 100;
        TestActiveSheet.Columns[2].ColumnWidth = 100;
    }
    
    [System.Reflection.DefaultMember("Data")]
    [CellImage("Name")]
    public class Country
    {
        public string Name { get; set; }
        [DisplayName("Capital Name")]
        public string Capital { get; set; }
        public string ContentType => "image/png";
    
        [GrapeCity.CalcEngine.CellValueDataType(GrapeCity.CalcEngine.PrimitiveValueType.Image)]
        public Image Data
        {
            get
            {
                return Image.FromFile(@"D:\apple.jpg");
            }
        }
    }
    
    Visual Basic
    Copy Code
    Private Sub CellImageAsAttribute_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim TestActiveSheet As IWorksheet = FpSpread1.AsWorkbook().ActiveSheet
    
        FpSpread1.AsWorkbook().WorkbookSet.CalculationEngine.CalcFeatures = CalcFeatures.All
        FpSpread1.LegacyBehaviors = FarPoint.Win.Spread.LegacyBehaviors.None
    
        'Displaying cell image using attribute
        Dim ct As GrapeCity.CalcEngine.RichValue(Of Country) = New GrapeCity.CalcEngine.RichValue(Of Country)(New Country() With {
            .Name = "India",
            .Capital = "New Delhi"
        })
        ct.ShowDetailsIcon = True
    
        TestActiveSheet.Cells("A1").Value = ct
    
        TestActiveSheet.Columns(0).ColumnWidth = 100
        TestActiveSheet.Columns(2).ColumnWidth = 100
    End Sub
    
    <System.Reflection.DefaultMember("Data")>
    <CellImage("Name")>
    Public Class Country
        Public Property Name As String
        <DisplayName("Capital Name")>
        Public Property Capital As String
    
        Public ReadOnly Property ContentType As String
            Get
                Return "image/png"
            End Get
        End Property
    
        <GrapeCity.CalcEngine.CellValueDataType(GrapeCity.CalcEngine.PrimitiveValueType.Image)>
        Public ReadOnly Property Data As Image
            Get
                Return Image.FromFile("D:\apple.jpg")
            End Get
        End Property
    End Class
    

    Note: The data type return by string only supports local image file path.

    Display Image and Cell Value

    If you want to display the image along with a cell value, it can be achieved by using the CellImageAttribute class members. Its constructor method takes the following parameters:

    Parameter Description
    member

    A string value indicating the member specified cell inline image data.

    isField

    A Boolean value indicating whether the member is a field. Default is false.

    The following GIF illustrates an image displayed along with a cell value according to the image attributes set in Spread.

    C#
    Copy Code
    private void CellImageAndValue_Load(object sender, EventArgs e)
    {
        IWorksheet TestActiveSheet = fpSpread1.AsWorkbook().ActiveSheet;
        fpSpread1.AsWorkbook().WorkbookSet.CalculationEngine.CalcFeatures = CalcFeatures.All;
        fpSpread1.LegacyBehaviors = FarPoint.Win.Spread.LegacyBehaviors.None;
    
        // Displaying both image and cell value
        RichValue<Country> ct = new RichValue<Country>(new Country()
        {
            Name = "Apple",
        });
        ct.ShowDetailsIcon = true;
        TestActiveSheet.Cells["A1"].Value = ct;
    
        TestActiveSheet.Rows[0].RowHeight = 100;
        TestActiveSheet.Columns[0].ColumnWidth = 150;
        TestActiveSheet.Columns[2].ColumnWidth = 100;
    }
    
    [System.Reflection.DefaultMember("Name")]
    [CellImage("Image")]
    public class Country
    {
        public string Name { get; set; }
        [CellValueDataType(PrimitiveValueType.Image)]
        public string Image
        {
            get
            {
                return @"D:\apple.jpg";
            }
        }
    }
    
    Visual Basic
    Copy Code
    Private Sub CellImageAndValue_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim TestActiveSheet As IWorksheet = FpSpread1.AsWorkbook().ActiveSheet
    
        FpSpread1.AsWorkbook().WorkbookSet.CalculationEngine.CalcFeatures = CalcFeatures.All
        FpSpread1.LegacyBehaviors = FarPoint.Win.Spread.LegacyBehaviors.None
    
        'Displaying both image and cell value
        Dim ct As RichValue(Of Country) = New RichValue(Of Country)(New Country() With {
            .Name = "Apple"
        })
        ct.ShowDetailsIcon = True
    
        TestActiveSheet.Cells("A1").Value = ct
    
        TestActiveSheet.Rows(0).RowHeight = 100
        TestActiveSheet.Columns(0).ColumnWidth = 150
        TestActiveSheet.Columns(2).ColumnWidth = 100
    End Sub
    
    <System.Reflection.DefaultMember("Name")>
    <CellImage("Image")>
    Public Class Country
        Public Property Name As String
    
        <CellValueDataType(PrimitiveValueType.Image)>
        Public ReadOnly Property Image As String
            Get
                Return "D:\apple.jpg"
            End Get
        End Property
    End Class
    

    Note: This class has a higher priority than CellValueDataTypeAttribute class.

    Spread for WinForms also provides the GC.IMAGE function to place an image in a cell. For more information about this function, refer to the Image Sparkline topic. You can also use the IMAGE function, which inserts images into cells from a source location.

    Insert/Paste Picture in Cell

    Spread for Winforms allows you to use the InsertPictureInCell or PastePictureInCell members of the IRange interface to insert or paste an in-place picture into a cell. Note that it works with flat style mode (no LegacyBehaviors.Style) only. You can insert a picture into a cell using ribbonBar and paste the same using the context menu.

    Insert Picture in a Cell

    Using Code

    The following example code inserts a picture using the IRange.InsertPictureInCell API.

    C#
    Copy Code
    fpSpread1.AsWorkbook().ActiveSheet.Cells[7, 2].InsertPictureInCell(@”picture.jpg”);
    
    Visual Basic
    Copy Code
    FpSpread1.AsWorkbook().ActiveSheet.Cells(7, 2).InsertPictureInCell("picture.jpg")
    

    At Runtime

    Follow the steps below to insert a picture using the ribbonBar.

    1. Run the code below to load a spread control with the ribbonBar.
      C#
      Copy Code
      ribbonBar1.Attach(fpSpread1);
      
      Visual Basic
      Copy Code
      ribbonBar1.Attach(FpSpread1)
      
    2. Choose the Insert option from the menu.
    3. In the Illustrations group, click on Picture and select Place in Cell.
      You can add images to the worksheet and have them float on top by using the Place over Cells option.
    4. Select a picture and click OK.

    The following GIF illustrates how to insert a picture into a cell using ribbonBar.

    Paste Picture in a cell

    Using code

    The following example code pastes a picture using the IRange.PastePictureInCell API.

    1. Copy a picture to Clipboard.
    2. Run the code below to load a spread control with the ribbonBar having RichClipboard property as True.
      C#
      Copy Code
      fpSpread1.Features.RichClipboard = true;
      fpSpread1.AsWorkbook().ActiveSheet.ActiveCell.PastePictureInCell();
      
      Visual Basic
      Copy Code
      FpSpread1.Features.RichClipboard = True
      FpSpread1.AsWorkbook().ActiveSheet.ActiveCell.PastePictureInCell()
      

    At runtime

    The following steps show how to paste a picture using ribbonBar.

    1. Copy a picture to Clipboard.
    2. Run the code below to load a spread control with the ribbonBar having RichClipboard property as True.
      C#
      Copy Code
      fpSpread1.Features.RichClipboard = true;
      ribbonBar1.Attach(fpSpread1);
      
      Visual Basic
      Copy Code
      FpSpread1.Features.RichClipboard = True
      ribbonBar1.Attach(FpSpread1)
      
    3. Right-click on the cell where you want to paste the copied picture.
    4. On the Context menu, select the Paste Picture in Cell(C) option.