I’m working in OnRender where it gets passed into as a parameter.
public override void OnRender(DrawingContext dc)
{
base.OnRender(dc);
if (ShowGrid)
{
if (HorizontalGridSpacing > 0)
{
// Draw horizontal grid lines
for (int y = 0; y < this.Height; y += HorizontalGridSpacing)
{
if (y > 0 && y != this.Height - 1)
{//Only draw label if not at start or end of graph
int Text1Width, Text1Height, Text2Width, Text2Height;
string Text1 = (MaxYValue1 - ((MaxYValue1 - MinYValue1) / (this.Height - 1)) * y).ToString("f1");
string Text2 = (MaxYValue2 - ((MaxYValue2 - MinYValue2) / (this.Height - 1)) * y).ToString("f1");
defaultFont.ComputeExtent(Text1, out Text1Width, out Text1Height);
defaultFont.ComputeExtent(Text2, out Text2Width, out Text2Height);
dc.DrawText(Text1, defaultFont, GraphPen1.Color, 0, y - (Text1Height / 2));
dc.DrawText(Text2, defaultFont, GraphPen2.Color, this.Width - Text2Width, y - (Text2Height / 2));
dc.DrawLine(GridPen, Text1Width + 2, y, this.Width - Text2Width - 2, y);
}
else
{
dc.DrawLine(GridPen, 0, y, this.Width, y);
}
}
}
if (VerticalGridSpacing > 0)
{
// Draw vertical grid lines
for (int x = 0; x < this.Width; x += VerticalGridSpacing)
{
int TextXWidth = 0, TextXHeight = 0;
if (x != 0 && x != this.Width - 1 && LabelX[x] != null)
{//Only draw label if not at start or end of graph
defaultFont.ComputeExtent(LabelX[x], out TextXWidth, out TextXHeight);
Bitmap Label = new Bitmap(TextXWidth, TextXWidth);
Label.DrawRectangle(Color.Black, 0, 0, 0, Label.Width, Label.Height, 0, 0, Background, 0, 0, Background, 0, 0, 256);
Label.DrawText(LabelX[x], defaultFont, GridPen.Color, 0, 0);
Bitmap rotated = new Bitmap(TextXHeight, TextXWidth);
dc.PushClippingRectangle(x - (TextXHeight / 2), this.Height - TextXWidth, TextXHeight, TextXWidth);
//Problem here:
dc.RotateImage(270, x - (TextXHeight / 2), this.Height - TextXWidth, Label, 0, 0, TextXWidth, TextXWidth, ushort.MaxValue);
dc.PopClippingRectangle();
}
dc.DrawLine(GridPen, x, 0, x, this.Height - TextXWidth);
}
}
}
// Draw the graph lines
for (int i = 1; i < PointIndex; i++)
{
dc.DrawLine(GraphPen1, PointsX[i - 1], PointsY1[i - 1], PointsX[i], PointsY1[i]);
dc.DrawLine(GraphPen1, PointsX[i - 1], PointsY1[i - 1] - 1, PointsX[i], PointsY1[i] - 1);
dc.DrawLine(GraphPen2, PointsX[i - 1], PointsY2[i - 1], PointsX[i], PointsY2[i]);
dc.DrawLine(GraphPen2, PointsX[i - 1], PointsY2[i - 1] - 1, PointsX[i], PointsY2[i] - 1);
}
}