[]
        
(Showing Draft Content)

Image Cell

Image cells display an image as data within a cell.


To create an image in a cell, use the ImageCellType class in the GrapeCity.Wpf.SpreadSheet.CellType namespace.

The value of an image cell should be an ImageSource representing the source of an image or binary data that represents an image. For example, the cell value may be a string that specifies the path to an image file. If the cell value is not an ImageSource type or binary data, which represents an image, you can set an ImageConverter to convert the value into an ImageSource. To create an ImageConverter, use the IImageSourceConverter interface.

There are two ways to set up an image cell in a spreadsheet.

Using XAML view

The following example code sets up an image cell in XAML and then defines the image path in code.

<Window.Resources>
        <local:SpreadFilePathToImageConverter  x:Key ="MyImageConverter"/>
</Window.Resources>
<Grid>
      <gss:GcSpreadSheet  x:Name ="spreadSheet1"  HorizontalAlignment ="Stretch"  VerticalAlignment ="Stretch" >
            <gss:GcSpreadSheet.Sheets>
                <gss:SheetInfo  ColumnCount ="20"  RowCount ="50">
                    <gss:SheetInfo.Columns >
                        <gss:ColumnInfo  Width ="250">
                            <gss:ColumnInfo.CellType>
                                <CT:ImageCellType  Stretch ="Fill"  ImageConverter ="{StaticResource MyImageConverter}"/>
                            </gss:ColumnInfo.CellType>
                        </gss:ColumnInfo>
                    </gss:SheetInfo.Columns>
                </gss:SheetInfo >
            </gss:GcSpreadSheet.Sheets>
      </gss:GcSpreadSheet>
</Grid>

C#

// Set the value of the image cell.
spreadSheet1.Workbook.ActiveSheet.Cells["A2"].Value = "D:\\image.png";

VB

' Set the value of the image cell.
spreadSheet1.Workbook.ActiveSheet.Cells("A2").Value = "D:\image.png"

Using Code view

The following code loads the "picture.png" file from the D drive of the application into an image cell, C2. Here, the SpreadFilePathToImageConverter class converts the file path into a BitmapImage.

C#

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    // Set ImageCellType.
    ImageCellType img = new ImageCellType();
    img.Stretch = Stretch.Fill;
    img.ImageConverter = new SpreadFilePathToImageConverter();
    spreadSheet1.Workbook.ActiveSheet.Cells["C2"].CellType = img;
    // Set the value.
    spreadSheet1.Workbook.ActiveSheet.Cells["C2"].Value = "D:\\picture.png";
}

public class SpreadFilePathToImageConverter : IImageSourceConverter
{
    public ImageSource GetImageSource(object value)
    {
        if (value != null)
        {
            BitmapImage bmi = new BitmapImage(new Uri(value.ToString(), UriKind.Absolute));
            return bmi;
        }
        else return null;
    }
}

VB

Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
  ' Set ImageCellType.
  Dim img As ImageCellType = New ImageCellType()
  img.Stretch = Stretch.Fill
  img.ImageConverter = New SpreadFilePathToImageConverter()
  spreadSheet1.Workbook.ActiveSheet.Cells("C2").CellType = img
  ' Set the value.
  spreadSheet1.Workbook.ActiveSheet.Cells("C2").Value = "D:\picture.png"
End Sub

Public Class SpreadFilePathToImageConverter
        Inherits IImageSourceConverter
        Public Function GetImageSource(value As Object) As ImageSource
            If value IsNot Nothing Then
                Dim bmi As BitmapImage = New BitmapImage(New Uri(value.ToString(), UriKind.Absolute))
                Return bmi
            Else
                Return Nothing
            End If
        End Function
End Class