Best way to handle images that come from a database?

Posted by: mikedempsey on 8 September 2017, 12:53 pm EST

  • Posted 8 September 2017, 12:53 pm EST

    I need to display a column of images that are loaded from a database.


    I used to do this by writing each image to a file and then using the LoadPicture() method (in the COM version of Spread) to load the cell.
    (This apparently made a copy as I was immediately able to reuse the SAME file.)


    I tried using a similar method with Spread.Net - reusing the same file for each cell, and using System.Image.LoadFile(). However that tells me that the file is in use (presumably by the first cell) as soon as I try to load the second image. Apparently it does not really 'Load' the image it simply keeps a pointer to the file.


    I then tried cloning a second image from the first and Disposing the first image. That failed since the clone apparently also keeps a reference to the file.


    I finally got it to work using the following code but I am not sure how large a resource hit I will take if I create (possibly thousands) of bitmaps in memory.


    Dim img As Image = Image.FromFile(Path.Combine(LOBPath, FName))
    Dim bmp As Bitmap = New Bitmap(img)
    sheet.Cells(row, c).Value = bmp
    img.Dispose()


    Can anyone suggest how to load a large number of images  with the least resource usage. (without any ties to file storage of the images)


    My real concern is that a bitmap requires considerably more memory than a jpg file so I was hoping that I could keep the images compressed - expanding them to bitmaps only in those cells that were rendered.


    Thanks for any suggestions.


     


     


     


     

  • Replied 8 September 2017, 12:53 pm EST

    Hello,


    Since the DataModel of the Spread can hold any object in each cell, you can let the data from the bound DataSource (image field) go directly into the cell as the Spread is being bound. Then, you do not need to write this information to a file. Then, create a custom celltype for that column to override the PaintCell method and paint the Image (Bitmap) into the cell during this override.

Need extra support?

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

Learn More

Forum Channels