zoukankan      html  css  js  c++  java
  • visifire 在SilverLight下导出图片

    http://www.visifire.com/documentation/Visifire_Documentation/Common_Tasks/Exporting_Chart_as_Image_in_Silverlight.htm

    在 WPF 下面可以直接调用截图

    Point p = mychart2.PointToScreen(new Point(0, 0));

                System.Drawing.Bitmap screenBitmap = new System.Drawing.Bitmap(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width, System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height);

                System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(screenBitmap);



                int x1 = Double2Int(p.X);

                int y1 = Double2Int(p.Y);


                int x2 = Double2Int(p.X + mychart2.Width);

                int y2 = Double2Int(p.Y + mychart2.Height);


                System.Drawing.Size s = new System.Drawing.Size(Double2Int(mychart2.Width - 1), Double2Int(mychart2.Height));


                g.CopyFromScreen(x1, y1, x1, y1, s);

                g.Dispose();


                Microsoft.Win32.SaveFileDialog sfd = new Microsoft.Win32.SaveFileDialog();

                sfd.Title = "保存";

                //sfd.CreatePrompt = true;

                sfd.OverwritePrompt = true;

                sfd.Filter = "BMP文件|*.bmp";

                if (sfd.ShowDialog()==true)

                {


                    string fileName = sfd.FileName;


                    //SaveFile(fileName);

                    //screenBitmap.Save("C:\\aaa.bmp");

                    screenBitmap.Save(fileName);

                }

     在SilverLight 下

     FJ.Core.dll 下载 

    Exporting Chart as Image in Silverlight

    In this topic, we are going to explain how to export Chart as Image from managed code in Silverlight. This is possible by using a WriteableBitmap class in Silverlight and a third party library called FJ.Core.dll. You can download the FJ.Core binary from here.

     

    For information about creating Visifire Chart in a Silverlight application, please refer to Managed Code Silverlight Sample. Once you create a Silverlight application, add Visifire binary references and FJ.Core.dll reference to the project.

     

    Open the MainPage.xaml. Add a Chart and a Button inside it as shown below:

     

    <UserControl x:Class="SLSave2Image.MainPage"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

        xmlns:vc="clr-namespace:Visifire.Charts;assembly=SLVisifire.Charts"

        mc:Ignorable="d" Width="500" Height="480">

        <Grid x:Name="LayoutRoot">

            <StackPanel>

                <vc:Chart Name="MyChart" Width="500" Height="300" Theme="Theme1">

                    <vc:Chart.Titles>

                        <vc:Title Text="Visifire Chart"/>

                    </vc:Chart.Titles>

                    

                    <vc:Chart.Series>

                        <vc:DataSeries RenderAs="Column" LabelEnabled="True">

                            <vc:DataSeries.DataPoints>

                                <vc:DataPoint AxisXLabel="Jan" YValue="35"/>

                                <vc:DataPoint AxisXLabel="Feb" YValue="32"/>

                                <vc:DataPoint AxisXLabel="Mar" YValue="27"/>

                                <vc:DataPoint AxisXLabel="Apr" YValue="17"/>

                                <vc:DataPoint AxisXLabel="May" YValue="16"/>

                            </vc:DataSeries.DataPoints>

                        </vc:DataSeries>

                    </vc:Chart.Series>

                </vc:Chart>

     

                <Button Name="ButtonSave" Height="25" Width="160" Content="Save Chart"/>

            </StackPanel>

        </Grid>

    </UserControl>

     

    Now open MainPage.xaml.cs page and add following namespaces.

     

    using FluxJpeg.Core;

    using FluxJpeg.Core.Encoder;

    using System.Windows.Media.Imaging;

    using System.IO;

    using Visifire.Charts;

     

    Now attach an event handler for Click event of Button inside the Page constructor.

     

    public MainPage()

    {

         InitializeComponent();

              

         ButtonSave.Click += new RoutedEventHandler(ButtonSave_Click);

    }

     

    void ButtonSave_Click(object sender, RoutedEventArgs e)

    {

         SaveToImage(MyChart);

    }

     

    Inside the ButtonSave event handler declare a function called SaveToImage() which will accept Chart as parameter.

     

    Now inside the function definition, create a WriteableBitmap out of a Chart and collect the raster information from it. Then encode the raster information to file stream using JpegEncoder present in FJ.Core library as shown below. Finally save the file stream as image into hard drive.

     

    /// <summary>

    /// Save Visifire chart as Image

    /// </summary>

    /// <param name="visifire_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 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;

    }

     

    Now you can run the application. Once you click on a Button, a save file dialog window will appear using which you can save the image of the chart. 

  • 相关阅读:
    【ASP.NET开发】ASP.NET(MVC)三层架构知识的学习总结
    网络工作室暑假后第二次培训资料(SQLServer存储过程和ADO.NET访问存储过程)整理(一)
    【ASP.NET开发】ASP.NET对SQLServer的通用数据库访问类 分类: ASP.NET 20120920 11:17 2768人阅读 评论(0) 收藏
    网络工作室暑假后第一次培训资料(ADO.NET创建访问数据集)整理 分类: ASP.NET 20121005 20:10 911人阅读 评论(0) 收藏
    网络工作室暑假后第二次培训资料(SQLServer存储过程和ADO.NET访问存储过程)整理(二)
    RabbitMQ(二)队列与消息的持久化
    实用的与坐标位置相关的js
    prototype、JQuery中跳出each循环的方法
    PHP获取当前日期所在星期(月份)的开始日期与结束日期
    Document Viewer解决中文乱码
  • 原文地址:https://www.cnblogs.com/lmarsy/p/1663452.html
Copyright © 2011-2022 走看看