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.

    <FONT size=2>

    </FONT><FONT color=#0000ff size=2>Dim</FONT><FONT size=2> img </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> Image = Image.FromFile(Path.Combine(LOBPath, FName))
    </FONT><FONT color=#0000ff size=2>Dim</FONT><FONT size=2> bmp </FONT><FONT color=#0000ff size=2>As</FONT><FONT size=2> Bitmap = </FONT><FONT color=#0000ff size=2>New</FONT><FONT size=2> Bitmap(img)
    sheet.Cells(row, c).Value = bmp


    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


    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