I'm drawing some data using Winforms (with a C# solution).
I plot error bars using chart series (with the chart.Series[item].Points.AddXY()
method). However, I link the error bars' middle points with a drawing in the Paint
event handler, since series doesn't seem to be drawable with custom pens. The Paint
event looks like the following (it has some custom shenanigans for offsetting each curve along the x-axis):
private void Chart_Paint(object sender, PaintEventArgs e)
{
var g = e.Graphics;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
for (int line = 0; line < _meanLines.Count; line++)
{
Pen pen = _pens[line];
_xOffset = LinesOffset(_meanLines.Count, line, _xOffsetRatio);
List<(float x, float y)> points = _meanLines[line].Select(mean => (float.Parse(mean.x) * (1 + _xOffset), mean.y)).ToList();
List<Point> pointsList = points.Select(p => new Point((int)chart.ChartAreas[0].AxisX.ValueToPixelPosition(p.x), (int)chart.ChartAreas[0].AxisY.ValueToPixelPosition(p.y))).ToList();
for (int p = 1; p < pointsList.Count; p++)
{
g.DrawLine(pen, pointsList[p - 1], pointsList[p]);
}
}
}
Everything works fine, and my chart looks like I want it to look: chart as displayed when the software is running.
I then want to export my chart as an EMF file (to be later be included in LaTex), using:
chart.SaveImage($@"C:\Users\User\Documents\DataPlotter\{figureName}.emf", ChartImageFormat.Emf);
But then my lines drawn in the Paint
event handler are missing: chart as exported in emf.
Is there a way to keep these lines in an emf export?
I tried exporting the chat as a bitmap to check if the drawn lines were exported, and it worked (the following code saved the aforementioned image with the lines displayed):
using (Bitmap bmp = new Bitmap(chart.Size.Width, chart.Size.Height))
{
chart.DrawToBitmap(bmp, new Rectangle(0, 0, chart.Size.Width, chart.Size.Height));
using (Graphics g = Graphics.FromImage(bmp))
{
g.DrawLine(new Pen(Color.Black, 2), new Point(0, 0), new Point(chart.Size.Width, chart.Size.Height));
chart.SaveImage($@"C:\Users\User\Documents\DataPlotter\{figureName}_trueEmf.emf", ChartImageFormat.Emf); // The drawn lines are missing
bmp.Save($@"C:\Users\User\Documents\DataPlotter\{figureName}_png.png", System.Drawing.Imaging.ImageFormat.Png); // Every drawn line appear
bmp.Save($@"C:\Users\User\Documents\DataPlotter\{figureName}_bmpEmf.emf", System.Drawing.Imaging.ImageFormat.Emf); // Every drawn line appear but saved as a bitmap
}
}
But I still can't find a way to export these lines in a vectorial image. I can export an emf file using the last line with the bmp.Save
method, but it's not drawn using vectors (I can't import it to Inkscape and save an eps file for instance).
I feel like there's no way to make DrawLine()
draw vectors, but I don't know if .NET provides something else for drawing vectors (appart from using a chart.Series
object, but I can't find a way to do so while using a custom pen...
Does anyone have an idea?
Thanks!