How to export a FusionChart to Image in ASP.Net
We are using the free version of fusion charts and want to know is there a way to export the generated chart to image without using any third party components. We are using the ASP.Net for generating the开发者_运维知识库 charts.
SOLUTION
After a research on this topic, I have found a useful solution for this.
For that we need to have a freeware from Bytecout to convert swf to image. This can be downloaded from this link.
Then we need to register the COM dlls and add the reference to our project. Refer to the below for further usage
Protected Sub btnSwf2ImgConverter_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim swfobj As New SWFToImage.SWFToImageObject
swfobj.InitLibrary("demo", "demo")
swfobj.InputSWFFileName = Server.MapPath("dumps/swf/") & "FCF_MSColumn3D.swf"
If ddlImageExtension.SelectedValue = "jpg" Then
swfobj.ImageOutputType = TImageOutputType.iotJPG
ext = ".jpg"
Else
If ddlImageExtension.SelectedValue = "gif" Then
swfobj.ImageOutputType = TImageOutputType.iotGIF
ext = ".gif"
Else
If ddlImageExtension.SelectedValue = "png" Then
swfobj.ImageOutputType = TImageOutputType.iotPNG
ext = ".png"
Else
If ddlImageExtension.SelectedValue = "bmp" Then
swfobj.ImageOutputType = TImageOutputType.iotBMP
ext = ".bmp"
End If
End If
End If
End If
filenm = "FCF_Column3D" & ext
swfobj.Execute_Begin()
'swfobj.Execute()
swfobj.Execute_SetVariable("dataXML", "<graph animation='0' formatNumber='1' divLineColor='ff5904' divLineAlpha='20' alternateHGridAlpha='5' canvasBorderColor='666666' baseFontColor='666666' showAlternateHGridColor='1' AlternateHGridColor='ff5904' anchorSides='3' rotateNames='1' caption='Daily Reports' yAxisMinValue='0' yAxisMaxValue='1' xAxisName='Day-Month-Year' yAxisName='Percentage' decimalPrecision='2' numdivlines='8' numVdivlines='16' showhovercap='1' formatNumberScale='0'> <categories> <category name='1-Jun-2010' showName='1' /> <category name='2-Jun-2010' showName='1' /> <category name='3-Jun-2010' showName='1' /> <category name='4-Jun-2010' showName='1' /> <category name='5-Jun-2010' showName='1' /> <category name='6-Jun-2010' showName='1' /> <category name='7-Jun-2010' showName='1' /> <category name='8-Jun-2010' showName='1' /> <category name='9-Jun-2010' showName='1' /> <category name='10-Jun-2010' showName='1' /> <category name='11-Jun-2010' showName='1' /> <category name='12-Jun-2010' showName='1' /> <category name='13-Jun-2010' showName='1' /> <category name='14-Jun-2010' showName='1' /> <category name='15-Jun-2010' showName='1' /> <category name='Aggregate' showName='1' /></categories><dataset showValues='0' alpha='80' showAnchors='1' seriesname='Answered Percent' color='#f07979' lineThickness='2'><set name='Month' value='97.6'/><set name='Month' value='98.2'/><set name='Month' value='99.3'/><set name='Month' value='99.6'/><set name='Month' value='95.2'/><set name='Month' value='99.3'/><set name='Month' value='99.3'/><set name='Month' value='98.8'/><set name='Month' value='97.5'/><set name='Month' value='96.3'/><set name='Month' value='98.6'/><set name='Month' value='99.1'/><set name='Month' value='99.3'/><set name='Month' value='99.6'/><set name='Month' value='99.2'/><set name='Month' value='98.38'/></dataset></graph>")
swfobj.Execute_SetVariable("chartWidth", "750")
swfobj.Execute_SetVariable("chartHeight", "600")
swfobj.ImageWidth = 600
swfobj.ImageHeight = 500
Threading.Thread.Sleep(10000)
swfobj.Execute_GetImage()
swfobj.SaveToFile(Server.MapPath("dumps/images/" & filenm))
swfobj.Execute_End()
Call DownloadImage(filenm)
End Sub
Public Sub DownloadImage(ByVal fn As String)
Image1.ImageUrl = "~/dumps/images/" & fn
Dim fi As New FileInfo(Server.MapPath("dumps/images/" & fn))
HttpContext.Current.Response.Clear()
HttpContext.Current.Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", fn))
HttpContext.Current.Response.ContentType = "image/" & DropDownList1.SelectedValue
Response.WriteFile(fi.FullName)
Response.Flush()
fi.Delete()
Response.End()
End Sub
Here animation='0' plays a very important role, it specifies the fusion chart swf that, it must be loaded without animation. Also it is advisable if we sleep the code for 1 or 2 sec the swf would be loaded completely and image is converted to the selected format.
Hope this would be useful for someone like me. :-)
- Screen capture.
- License it.
Of course, there's also the free Asp.net charting controls (here). Here's an example of how to export with those tools.
FusionCharts Team has developed a .NET assembly which will enable you to export chart at server without loading the chart in the browser.
You can have it by writing to FusionCharts Support at http://www.fusioncharts.com/contact/.
This only works for FusionCharts v3 charts.
精彩评论