Display 'System.Drawing.BitMap' type image in a column in DataTable in C#

Posted by: abhishek1991rana on 7 July 2021, 10:02 pm EST

  • Posted 7 July 2021, 10:02 pm EST

    I am trying to load BitMap images(several small ones) to a column in a datatable (C#) which is later binded with Far point spread.

    I have created the new column in Datatable as[1]: ```

    Table.Columns.Add(“Level”, typeof(System.Byte));

    
    I have also tried [2]: ```
    Table.Columns.Add("Level", typeof(System.Drawing.Bitmap));
    

    and I have the image pulled at runtime as: ‘frmBitmap.DefInstance.imgDynamic.Images[sBitmap]’ where sBitmap is some string which pulls the specific image from the ‘imgDynamic’ ImageList. The type of the image is: System.Drawing.Bitmap

    But when I assign this image i.e., frmBitmap.DefInstance.imgDynamic.Images[sBitmap] to column field, I don’t see the image instead I see ‘System.Byte[]’ as ouput. I have tried setting up tne BitMap image to datatable as:

    var imageConverter = new ImageConverter();
    Table.Rows["Level"] = imageConverter.ConvertTo(frmBitmap.DefInstance.imgDynamic.Images[sBitmap], System.Type.GetType("System.Byte[]"));
    

    My Question is: How Exactly can we load the BITMAP image in a column in datatable in C#???

  • Posted 7 July 2021, 11:02 pm EST

    Hi,

    You will need to set the CellType of the sheet’s Column to ImageCellType so that the Bitmap/Byte is rendered as an Image.

    If you don’t set the correct cell type then the cell value is rendered by taking the value’s ToString() result (which is the type name in this case, System.Drawing.Bitmap and System.Byte).

    Regards,

    Jitender

  • Posted 7 July 2021, 11:20 pm EST

    Hello Jitender,

    Thanks for the response.

    However, I had already implemented the logic using ‘ImageCellType’ and that works fine but ‘ImageCellType’ is specific to far point spread.

    My intensions are to add the required image to the datatable column first and then set the datatable as source to far point spread. So that, my changes remain intact to the datatable and far point spread is just used to render the data from the datatable.

    Please let me know, if my thoughts are possible to achieve or you see any issues with the design approach.

    Thanks :slight_smile:

  • Posted 8 July 2021, 7:15 pm EST

    Hi,

    Spread does not automatically infer Image CellType so you would need to set it on the required column after setting the Data Source.

    You can do this based on the Table’s column type:

    fpSpread1.ActiveSheet.DataSource = table;
    foreach (DataColumn column in table.Columns)
    {
        if (column.DataType == typeof(Bitmap))
        {
            fpSpread1.ActiveSheet.Columns[column.Ordinal].CellType = new ImageCellType();
        }
    }
    

    Regards,

    Jitender

Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels