Last active
August 25, 2016 15:15
-
-
Save matthewjberger/b9dac5e1c3283287fd5ad1f4901f4e1d to your computer and use it in GitHub Desktop.
This will make a bitmap out of an oxyplot plot model.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using OxyPlot; | |
using OxyPlot.Axes; | |
using OxyPlot.Wpf; | |
using System.IO; | |
using System.Drawing; | |
using System.Drawing.Imaging; | |
using System.Threading; | |
namespace OxyPlot2 | |
{ | |
class Program | |
{ | |
static Bitmap GetBitmap(System.Windows.Media.Imaging.BitmapSource source) | |
{ | |
Bitmap bmp = new Bitmap( | |
source.PixelWidth, | |
source.PixelHeight, | |
System.Drawing.Imaging.PixelFormat.Format32bppPArgb); | |
BitmapData data = bmp.LockBits( | |
new Rectangle(Point.Empty, bmp.Size), | |
ImageLockMode.WriteOnly, | |
PixelFormat.Format32bppPArgb); | |
source.CopyPixels( | |
System.Windows.Int32Rect.Empty, | |
data.Scan0, | |
data.Height * data.Stride, | |
data.Stride); | |
bmp.UnlockBits(data); | |
return bmp; | |
} | |
[STAThread()] | |
static void Main(string[] args) | |
{ | |
var plotModel = new PlotModel { Title = "OxyPlot Demo" }; | |
plotModel.Axes.Add(new OxyPlot.Axes.LinearAxis { Position = AxisPosition.Bottom }); | |
plotModel.Axes.Add(new OxyPlot.Axes.LinearAxis { Position = AxisPosition.Left, Maximum = 10, Minimum = 0 }); | |
var series1 = new OxyPlot.Series.LineSeries | |
{ | |
MarkerType = MarkerType.Circle, | |
MarkerSize = 4, | |
MarkerStroke = OxyColors.White | |
}; | |
series1.Points.Add(new DataPoint(0.0, 6.0)); | |
series1.Points.Add(new DataPoint(1.4, 2.1)); | |
series1.Points.Add(new DataPoint(2.0, 4.2)); | |
series1.Points.Add(new DataPoint(3.3, 2.3)); | |
series1.Points.Add(new DataPoint(4.7, 7.4)); | |
series1.Points.Add(new DataPoint(6.0, 6.2)); | |
series1.Points.Add(new DataPoint(8.9, 8.9)); | |
plotModel.Series.Add(series1); | |
using (MemoryStream stream = new MemoryStream()) | |
{ | |
stream.Position = 0; | |
Thread t = new Thread(new ThreadStart(() => | |
{ | |
System.Windows.Media.Imaging.BitmapSource bmp = PngExporter.ExportToBitmap(plotModel, 800, 600, OxyColors.White, 96); | |
GetBitmap(bmp).Save("test.bmp"); | |
})); | |
t.SetApartmentState(ApartmentState.STA); | |
t.Start(); | |
t.Join(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment