I like the concept of using the canvas to generate images stored in a database. How could we resize the image to fit inside the canvas?
I have this in my repaint script:
[code]from javax.imageio import ImageIO
from java.net import URL
selectedID = event.source.parent.selectedID
blobData = system.db.runQuery(“SELECT partPicture FROM PartsPicture WHERE partID = ‘%s’” % selectedID)
g = event.graphics
if len(blobData) > 0:
data = blobData[0][0]
# open up a temp file
# store the file name in a custom property on the paintable canvas
# the paintable canvas repaint event will use this as input stream
filename = system.file.getTempFile(“jpg”)
system.file.writeFile(filename, data)
urlFile = ‘file:%s’ % filename
event.source.parent.getComponent(‘Paintable Canvas’).fileName = urlFile #Note: custom property on paintable canvas ‘fileName’ [string]
url = event.source.fileName
imageStream = URL(url)
image = ImageIO.read(imageStream)
#### Scale graphics to actual component size (doesn't do anything for me)
dX = (event.width-1) / 100.0
dY = (event.height-1) / 100.0
g.drawImage(image, 1, 1, event.source)
g.scale(dX,dY)[/code]
The image variable in the code returns a BufferedImage which has an inherited method called getScaledInstance. You can use this to scale the image. In this example, I scale the width of the image to 320 pixels and maintain the aspect ratio for the height (a 640x480 image will be scaled to a 320x240 image):
[quote=“adamaustin”]The image variable in the code returns a BufferedImage which has an inherited method called getScaledInstance. You can use this to scale the image. In this example, I scale the width of the image to 320 pixels and maintain the aspect ratio for the height (a 640x480 image will be scaled to a 320x240 image):
[code]
from javax.imageio import ImageIO
from java.net import URL
url = event.source.fileName
imageStream = URL(url)
image = ImageIO.read(imageStream)