Display Image

I’m sure this has been answered before but I couldn’t find it via search.

I know how to upload an image to my SQL Server yet, not sure what the best way to display that image is? It’s in Varbinary(max) format

I’m not sure if there’s already something in place for this or if I need to build it out, not a big deal either way.

Thanks

You’ll have to build it. I’ve found that the best way to do this is using the paintable canvas component. Here’s an example script you would put in it’s paint event:

[code]g = event.graphics
name = event.source.ImageName

cachedName = event.source.getClientProperty(“img-bytes-cached-name”)
bytes = event.source.getClientProperty(“img-bytes-cached”)
if name!=cachedName or not bytes:
# Image not cached - load from database
bytes = system.db.runScalarQuery(“SELECT data FROM image_test WHERE imgname=’%s’”%name)
event.source.putClientProperty(“img-bytes-cached”,bytes)
event.source.putClientProperty(“img-bytes-cached-name”,name)

if bytes:
from javax.imageio import ImageIO
from java.io import ByteArrayInputStream
image = ImageIO.read(ByteArrayInputStream(bytes))
g.drawImage(image,0,0,event.source)[/code]

To get this to work, you’ll want to put a custom property on the component called “ImageName”, and modify the query inside that script to work with your table.