SilverLight保存统计图到本地
参考网址:
http://support2.dundas.com/Default.aspx?article=1389
http://www.visifire.com/blog/2009/11/12/exporting-visifire-silverlight-chart-as-image/
需要一个dll文件FJ.Core.dll,下载地址csdn上面可以搜到(给那小子刷分了)
然后下面是仿制的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Controls.DataVisualization.Charting;
using System.Collections.Generic;
using System.Windows.Media.Imaging;
using System.IO;
using FluxJpeg.Core;
namespace SLChatSaveImg
{
public partial class MainPage : UserControl
{
public class Complex
{
public string Key { set; get; }
public int Value1 { set; get; }
public int Value2 { set; get; }
public int Value3 { set; get; }
public int Value4 { set; get; }
}
public MainPage()
{
InitializeComponent();
listadd();
}
private List<Complex> listadd()
{
return new List<Complex>() {
new Complex{Key="key1",Value1=1,Value2=6,Value3=6,Value4=-2},
new Complex{Key="key2",Value1=2,Value2=7,Value3=5,Value4=-3},
new Complex{Key="key3",Value1=3,Value2=8,Value3=4,Value4=-4},
new Complex{Key="key4",Value1=4,Value2=9,Value3=3,Value4=-5},
new Complex{Key="key5",Value1=1,Value2=10,Value3=2,Value4=-6},
new Complex{Key="key6",Value1=3,Value2=1,Value3=4,Value4=-3}
};
}
private void button1_Click(object sender, RoutedEventArgs e)
{
chart.Series.Clear();
if ("1".Equals(textBox1.Text))
{
ColumnSeries columnSeries = new ColumnSeries();
columnSeries.Title = "33333333";
columnSeries.IndependentValueBinding =
new System.Windows.Data.Binding("Key");
columnSeries.DependentValueBinding =
new System.Windows.Data.Binding("Value3");
columnSeries.ItemsSource = listadd();
chart.Series.Add(columnSeries);
ColumnSeries columnSeries2 = new ColumnSeries();
columnSeries2.Title = "4444444444";
columnSeries2.IndependentValueBinding =
new System.Windows.Data.Binding("Key");
columnSeries2.DependentValueBinding =
new System.Windows.Data.Binding("Value4");
columnSeries2.ItemsSource = listadd();
chart.Series.Add(columnSeries2);
LineSeries lineSeries = new LineSeries();
lineSeries.Title = "1111111111";
textBox1.Text = "LineSeries";
lineSeries.IndependentValueBinding =
new System.Windows.Data.Binding("Key");
lineSeries.DependentValueBinding =
new System.Windows.Data.Binding("Value1");
lineSeries.ItemsSource = listadd();
chart.Series.Add(lineSeries);
LineSeries lineSeries2 = new LineSeries();
lineSeries2.Title = "22222222";
lineSeries2.IndependentValueBinding =
new System.Windows.Data.Binding("Key");
lineSeries2.DependentValueBinding =
new System.Windows.Data.Binding("Value2");
lineSeries2.ItemsSource = listadd();
chart.Series.Add(lineSeries2);
}
}
/// <summary>
/// Save Visifire chart as Image
/// </summary>
/// <param name="Chart">Visifire.Charts.Chart</param>
private void SaveToImage(Chart chart)
{
try
{
WriteableBitmap bitmap = new WriteableBitmap(chart, null);
if (bitmap != null)
{
SaveFileDialog saveDlg = new SaveFileDialog();
saveDlg.Filter = "JPEG Files (*.jpeg)|*.jpeg";
saveDlg.DefaultExt = ".jpeg";
if ((bool)saveDlg.ShowDialog())
{
using (Stream fs = saveDlg.OpenFile())
{
MemoryStream stream = GetImageStream(bitmap);
//Get Bytes from memory stream and write into IO stream
byte[] binaryData = new Byte[stream.Length];
long bytesRead = stream.Read(binaryData, 0, (int)stream.Length);
fs.Write(binaryData, 0, binaryData.Length);
}
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Note: Please make sure that Height and Width of the chart is set properly.");
System.Diagnostics.Debug.WriteLine(ex.Message);
}
}
/// <summary>
/// Get image MemoryStream from WriteableBitmap
/// </summary>
/// <param name="bitmap">WriteableBitmap</param>
/// <returns>MemoryStream</returns>
public static MemoryStream GetImageStream(WriteableBitmap bitmap)
{
byte[][,] raster = ReadRasterInformation(bitmap);
//return new MemoryStream(raster);
return EncodeRasterInformationToStream(raster, ColorSpace.RGB);
}
/// <summary>
/// Reads raster information from WriteableBitmap
/// </summary>
/// <param name="bitmap">WriteableBitmap</param>
/// <returns>Array of bytes</returns>
public static byte[][,] ReadRasterInformation(WriteableBitmap bitmap)
{
int width = bitmap.PixelWidth;
int height = bitmap.PixelHeight;
int bands = 3;
byte[][,] raster = new byte[bands][,];
for (int i = 0; i < bands; i++)
{
raster[i] = new byte[width, height];
}
for (int row = 0; row < height; row++)
{
for (int column = 0; column < width; column++)
{
int pixel = bitmap.Pixels[width * row + column];
raster[0][column, row] = (byte)(pixel >> 16);
raster[1][column, row] = (byte)(pixel >> 8);
raster[2][column, row] = (byte)pixel;
}
}
return raster;
}
/// <summary>
/// Encode raster information to MemoryStream
/// </summary>
/// <param name="raster">Raster information (Array of bytes)</param>
/// <param name="colorSpace">ColorSpace used</param>
/// <returns>MemoryStream</returns>
public static MemoryStream EncodeRasterInformationToStream(byte[][,] raster, ColorSpace colorSpace)
{
ColorModel model = new ColorModel { colorspace = ColorSpace.RGB };
FluxJpeg.Core.Image img = new FluxJpeg.Core.Image(model, raster);
//Encode the Image as a JPEG
MemoryStream stream = new MemoryStream();
FluxJpeg.Core.Encoder.JpegEncoder encoder = new FluxJpeg.Core.Encoder.JpegEncoder(img, 100, stream);
encoder.Encode();
// Back to the start
stream.Seek(0, SeekOrigin.Begin);
return stream;
}
private void button3_Click(object sender, RoutedEventArgs e)
{
SaveToImage(chart);
}
}
}