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.
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:
The following image shows a picture inserted inside a cell by providing an image file path.
C# |
Copy Code
|
---|---|
IWorksheet TestActiveSheet = fpSpread1.AsWorkbook().ActiveSheet; fpSpread1.AsWorkbook().WorkbookSet.CalculationEngine.CalcFeatures = CalcFeatures.All; fpSpread1.LegacyBehaviors = FarPoint.Win.Spread.LegacyBehaviors.None; // Displaying cell image using value // Image type TestActiveSheet.Cells["A1"].Value = Image.FromFile(@"D:\apple.jpg"); // Byte type // TestActiveSheet.Cells["A3"].Value = File.ReadAllBytes("D:\\apple.jpg"); // Stream type // TestActiveSheet.Cells["A5"].Value = new FileStream("D:\\apple.jpg", FileMode.Open); |
Visual Basic |
Copy Code
|
---|---|
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 value 'Image type TestActiveSheet.Cells("A1").Value = Image.FromFile("D:\apple.jpg") 'Byte type 'TestActiveSheet.Cells("A3").Value = File.ReadAllBytes("D:\\apple.jpg") 'Stream type 'TestActiveSheet.Cells("A5").Value = New FileStream("D:\apple.jpg", FileMode.Open) |
The following GIF illustrates the use of attributes to display an image in a cell as well as show additional information.
Show Code
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.
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.