我试图使用Tomas Petricek在这个网站上写的解决方案,但不是用于字符串而是用于图像。不幸的是,它不起作用。这是我的代码:
型号:
let ChartModelCumulativePerformance =
let numMths = 60
let trackOfDates numMths = firstDate.AddMonths(numMths)
let trackHF = [for x in 0 .. 59 ->(trackOfDates x, HFReturns.[x])]
let trackBchk = [for x in 0 .. 59 ->(trackOfDates x, BchkReturns.[x])]
Chart.Line(trackHF,Name="Hedge Fund") |> ignore
Chart.Line(trackBchk,Name="Benchmark") |> ignore
let myChart = Chart.Combine(
[Chart.Line(trackHF,Name="Hedge Fund")
Chart.Line(trackBchk,Name="Benchmark")])
myChart.CopyAsBitmap()
member this.CreateCumulativePerformanceGraph() = ChartModelCumulativePerformance
控制器:
let dataChart = res.CreateCumulativePerformanceGraph()
let (?<-) (viewData:ViewDataDictionary) (name:string) (value: Image) = viewData.Add(name, value)
this.ViewData?chartCumulative <- dataChart
this.View("HFAnalysis") :> ActionResult
查看
<img src =@ViewBag.chartCumulative height="80" width="80"/ alt="" />
或
<img src =@ViewData["chartCumulative"] height="80" width="80"/ alt="" />
否则,你知道其他方法吗?提前感谢您的帮助
答案 0 :(得分:1)
最终你有一个HTML问题,而不是这里的F#问题。
如果要将图像数据直接放入src
标记的img
属性中,则必须将其转换为base-64字符串,然后根据相应的元数据为其添加前缀。到Data URI standard。
我会将您的位图转换为PNG(缩小文件大小),然后获取图像的字节数组并将其传递给Convert.ToBase64String
。最终你会得到这样的东西:
<img width="80" height="80" src="data:image/png;base64,R0lGODlhEAAQAMQAAORHHOVSKudfOulrSOp3WOyDZu6QdvCchPGolfO0o/XBs/fNwfjZ0frl3/zy7////wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAkAABAALAAAAAAQABAAAAVVICSOZGlCQAosJ6mu7fiyZeKqNKToQGDsM8hBADgUXoGAiqhSvp5QAnQKGIgUhwFUYLCVDFCrKUE1lBavAViFIDlTImbKC5Gm2hB0SlBCBMQiB0UjIQA7" />
答案 1 :(得分:0)
非常感谢Joel !!!! 这是有兴趣的人的代码:
型号:
let ChartModelCumulativePerformance =
//... code for building a Chart
let myChart = myChart.CopyAsBitmap()
let ms = new MemoryStream()
let tempVar = myChart.Save(ms,Imaging.ImageFormat.Png)
let imageData = ms.ToArray()
let imageBase64 = Convert.ToBase64String(imageData)
String.Format("data:image/png;base64,{0}", imageBase64)
控制器:
let dataChart = res.CreateCumulativePerformanceGraph()
let (?<-) (viewData:ViewDataDictionary) (name:string) (value: String) = viewData.Add(name, value)
this.ViewData?chartCumulative <- dataChart
this.View("HFAnalysis") :> ActionResult
查看: