0

I am trying to create an application that shows the online trains in picturebox.

but my application spends a lot of memory and sometimes i got the Out of memory exception and sometimes my trains Disappears from the picturebox. To draw online train first time i draw the map of trains (lines ,stations ,...) on picturebox with size x=A and y=b after that i create another picturebox with the same size and put the second picturebox on first picturebox using this code:

    pictureBoxonlineTrain.Parent = pictureBoxMetroMap;

In every second the below function is executed :

       public void DrawOnlineTrain()
            {
                Bitmap map=null;
                if (OnlineTrainList.Count > 0)
                {
                    map = new Bitmap(pictureBoxonlineTrain.Size.Width, pictureBoxonlineTrain.Size.Height);

                    var graph = Graphics.FromImage(map);

                    foreach (TimeTable t in OnlineTrainList.ToList())
                    {
                       // graph.Dispose();
                        Rectangle rectTrainState = new Rectangle(t.XTrainLocation.Value - 3,
                                                                 t.YTrainLocation.Value - 3,
                                                                 15, 15);
                        graph.FillRectangle(RedBrush, rectTrainState);
                        graph.DrawString(t.TrainId.ToString(), pictureBoxonlineTrain.Font, Brushes.White, t.XTrainLocation.Value  -3, t.YTrainLocation.Value -3);

                    }
                }
                pictureBoxonlineTrain.Image = map;
              //  pictureBoxonlineTrain.Image.Save(@"C:\RailwayShiraz\ShirazMetro\ShirazRailWayWeb\Images\Train.jpg");
            } 

And i think it is the reason of my memory exception because every time i create a bitmap and graphic object .My question is how can i change this code as the objects dispose in every loop ?

Ehsan Akbar
  • 6,977
  • 19
  • 96
  • 180
  • the details of code :http://stackoverflow.com/questions/24589544/out-of-memory-exception-in-c-sharp-when-drawing-trains-in-picturebox – Ehsan Akbar Jul 06 '14 at 04:31
  • 1
    `I am trying to create an application that shows the online trains in picturebox` - as always, I strongly recommend you leave archaic winforms behind and use [proper technology](http://msdn.microsoft.com/en-us/library/ms754130(v=vs.110).aspx) instead. You will not have any of these problems as chances are you're just trying to achieve some minimal movement in the UI which winforms is not a suitable technology for. See [My Example](http://stackoverflow.com/a/15469477/643085) of how easy it is to achieve dynamic positioning / moving of UI elements based on Data using current .Net UI technology. – Federico Berasategui Jul 06 '14 at 04:39
  • It is a great idea but i don't have time to change my application to wpf .is it time consuming? – Ehsan Akbar Jul 06 '14 at 04:41
  • 1
    That depends on what you're really trying to achieve. Putting some lines/rectangles/whatever and moving them around in the screen is easy as 1,2,3 in WPF and does not require any of the horrible hacks you're currently using on winforms. See my example. I just uploaded the full source to GitHub. Make sure you click on "Comments" when running the app. – Federico Berasategui Jul 06 '14 at 04:47
  • 1
    also have a look at the code to see how easy it is to simply bind the positions of UI elements in a Canvas to a couple of properties in a relevant ViewModel / Data Model. This means that just by updating the properties with new Data you get WPF to update the UI for you, for free. No horrendous hacks, no procedural code. Just simple properties and DataBinding. – Federico Berasategui Jul 06 '14 at 04:51
  • 1
    Alright, I'll get some sleep right now. If you're interested in this code, you may also want to look at [My Other Example](http://stackoverflow.com/a/15580293/643085) which is basically the same but includes more complex objects and logic. Otherwise, I'll be around tomorrow if you need further help ;) – Federico Berasategui Jul 06 '14 at 05:16

1 Answers1

3

put your Bitmap and Graphics inside a Using statement and it will be disposed

using (map = new Bitmap(pictureBoxonlineTrain.Size.Width, pictureBoxonlineTrain.Size.Height))
{
    using (Graphics graph = Graphics.FromImage(map))
    {
//code goes here
    }
}
Chase Rocker
  • 1,908
  • 2
  • 13
  • 14