简体   繁体   中英

draw charts with mouse input

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

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM