Export Image from Visualization

from System.Drawing import Bitmap, Graphics, Rectangle, Point
from System.IO import Path
from Spotfire.Dxp.Application.Visuals import VisualContent

#1. Prepare canvas as a blank bitmap
w=640
h=480
bm = Bitmap(w,h)
g = Graphics.FromImage(bm)
r = Rectangle(Point(0,0), bm.Size)

#2. Paint visualization into canvas
#vis is a script parameter
vis.As[VisualContent]().Render(g, r)

#3. Save your canvas 
#tempFolder = Path.GetTempPath()
tempFilename = Path.GetTempFileName() + ".bmp"
bm.Save(tempFilename)
print "image saved as " + tempFilename

9 comments:

Anonymous said...

The font of the saved image differs from what's in dxp. How to set the image font?

Anonymous said...

how can I export the title of the visual?

Scott Barclay said...

Text, such as axis labels, comes out blurry. Any way to account for this or any other solutions to get visualizations out and maintain clean looking text?

Jose Leviaguirre said...

Hello Scott,

You will need to play with the graphics settings to fine tune the output. Please share your findings!

Chris Dugas said...

Still can't get this to work on text areas. Anyone ever have any luck on that?

Trevor Hubbard said...

Figured it out, you need to turn on anti-aliasing.

Example below:
vc = crosstop.As[VisualContent]()
crosstop_r = Document.ActivePageReference.GetVisualBounds(crosstop)
bm = Bitmap(crosstop_r.Width, crosstop_r.Height)
crosstop_ratio = (float(crosstop_r.Width)/float(crosstop_r.Height))
g = Graphics.FromImage(bm)
g.TextRenderingHint = g.TextRenderingHint.AntiAlias
vc.Render(g, crosstop_r)
crosstopfile = Path.GetTempFileName()
bm.Save(crosstopfile, ImageFormat.Png)

Blake said...

@Trevor Hubbard - Do you declare ImageFormat earlier?

Blake said...

Took a minute to realize that I needed to import ImageFormat
from System.Drawing.Imaging import ImageFormat

alach11 said...

Hey Blake + Trevor,

Enabling anti-aliasing didn't make a difference on my image outputs. My text is still bold/blurry. Do you have any other ideas?

Cheers,
Alex