1

I am building a chart program which displays up to 12 different graphs. All graphs should be visible at the same time

It should be possible to alter the active graph by mouse input.

I build the chart like this:

using (Graphics G = Graphics.FromImage(graph_canvas.Image))
{
    chart.draw_statics(G);
}
static_graph = graph_canvas.Image;

chart.draw_statics(G) contains several funtions to draw legends, grid,... and the 11 non-active graphs

I save this image in static_graph

So far so good

When the mouse moves, the intention is that the mouse pointer changes to the graph's colour and that it displays the coordinates of the mouse position.

so in mousemove to save time and memory, I take the saved image and draw the active graph to that instead of redrawing all graphs.

private void graph_canvas_MouseMove(object sender, MouseEventArgs e)
{
    Point mouse_pos = e.Location;
    chart.set_mouse_pos(mouse_pos);

    graph_canvas.Image = (Image)static_graph.Clone();
    using (Graphics G = Graphics.FromImage(graph_canvas.Image))
    {
        chart.draw_actives(G);
    }
    graph_canvas.Invalidate();
}


public void draw_actives(Graphics surface)
{
    G = surface;
    draw_mouse();
}

private void draw_mouse()
{
    G.DrawLine(mouse_pen, new PointF(mouse_pos.X - 10, mouse_pos.Y), new PointF(mouse_pos.X + 10, mouse_pos.Y));
    G.DrawLine(mouse_pen, new PointF(mouse_pos.X, mouse_pos.Y-10), new PointF(mouse_pos.X, mouse_pos.Y+10));
}

when the form is in fullscreen I get an error message

A first chance exception of type 'System.OutOfMemoryException' occurred in System.Drawing.dll

Blob
  • 23
  • 2
  • 1
    That's funny to try `to save [...] memory` and end up with a OutOfMemory error :) I would look around the Clone() line. – Thomas Ayoub Jan 20 '16 at 15:09
  • Thanks Thomas! That did the trick! Changed the clone() line to graph_canvas.Image = new Bitmap(static_graph); and build the static_graph bitmap like this: graph_canvas.DrawToBitmap(static_graph, new Rectangle(new Point(graph_canvas.Top, graph_canvas.Left), graph_canvas.Size)); – Blob Jan 21 '16 at 08:52

1 Answers1

0

The issue might be around the Clone() call which require more and more memory each time you move your mouse. I suggest you to change for:

graph_canvas.Image = new Bitmap(static_graph);
Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
  • Sure, sorry I thought my rep was too low to accept the answer. since it was to vote up the answer... – Blob Jan 22 '16 at 08:21