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