External/OxyPlot/OxyPlot.WindowsForms/PngExporter.cs
author moel.mich
Sat, 08 Jun 2013 16:53:22 +0000
changeset 391 5be8f2773237
permissions -rw-r--r--
Added the source code of OxyPlot as of commit d190d7748a73 (6.5.2013).
     1 // --------------------------------------------------------------------------------------------------------------------
     2 // <copyright file="PngExporter.cs" company="OxyPlot">
     3 //   The MIT License (MIT)
     4 //
     5 //   Copyright (c) 2012 Oystein Bjorke
     6 //
     7 //   Permission is hereby granted, free of charge, to any person obtaining a
     8 //   copy of this software and associated documentation files (the
     9 //   "Software"), to deal in the Software without restriction, including
    10 //   without limitation the rights to use, copy, modify, merge, publish,
    11 //   distribute, sublicense, and/or sell copies of the Software, and to
    12 //   permit persons to whom the Software is furnished to do so, subject to
    13 //   the following conditions:
    14 //
    15 //   The above copyright notice and this permission notice shall be included
    16 //   in all copies or substantial portions of the Software.
    17 //
    18 //   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
    19 //   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
    20 //   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
    21 //   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
    22 //   CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
    23 //   TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
    24 //   SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    25 // </copyright>
    26 // <summary>
    27 //   The png exporter.
    28 // </summary>
    29 // --------------------------------------------------------------------------------------------------------------------
    30 namespace OxyPlot.WindowsForms
    31 {
    32     using System.Drawing;
    33     using System.Drawing.Imaging;
    34 
    35     using OxyPlot.WindowsForms;
    36 
    37     /// <summary>
    38     /// The png exporter.
    39     /// </summary>
    40     public static class PngExporter
    41     {
    42         /// <summary>
    43         /// The export.
    44         /// </summary>
    45         /// <param name="model">
    46         /// The model.
    47         /// </param>
    48         /// <param name="fileName">
    49         /// The file name.
    50         /// </param>
    51         /// <param name="width">
    52         /// The width.
    53         /// </param>
    54         /// <param name="height">
    55         /// The height.
    56         /// </param>
    57         /// <param name="background">
    58         /// The background.
    59         /// </param>
    60         public static void Export(PlotModel model, string fileName, int width, int height, Brush background = null)
    61         {
    62             using (var bm = new Bitmap(width, height))
    63             {
    64                 using (Graphics g = Graphics.FromImage(bm))
    65                 {
    66                     if (background != null)
    67                     {
    68                         g.FillRectangle(background, 0, 0, width, height);
    69                     }
    70 
    71                     var rc = new GraphicsRenderContext { RendersToScreen = false };
    72                     rc.SetGraphicsTarget(g);
    73                     model.Update();
    74                     model.Render(rc, width, height);
    75                     bm.Save(fileName, ImageFormat.Png);
    76                 }
    77             }
    78         }
    79 
    80     }
    81 }