Append DataVizualisation Chart Control to text file

93 views Asked by At

I'm developing a system that takes data input from textboxes, and on a button click, saves these values to the respective listbox ready to be written to a text file once the process is complete.

The next stage has been using this data to create graphs, which has gone successfully but I'm now looking for a way to add these onto the end of my text file so it's all included in one place. I originally tried it like this (included in the total 'saveToFile' function):

        consoleFile.WriteLine(chartBP.Text); //chart title
        chartBP.SaveImage((fileName), System.Drawing.Imaging.ImageFormat.Jpeg);
        consoleFile.WriteLine("\n\n");

This appeared to work ok but threw a run-time error stating that the file could not be accessed because it was being used by another process.

I don't think I'm far off where I need to be, but I don't have enough experience with charts to know what to try next. Does anyone have any idea how to make this work, or another method that wouldn't produce the error? Any help is greatly appreciated!

1

There are 1 answers

0
jsanalytics On BEST ANSWER

If your problem is just to get rid of the exception, then you could do this:

            consoleFile.WriteLine(chartBP.Text);
            consoleFile.Close();

            FileStream imgFile = File.Open(filename, FileMode.Append);
            chartBP.SaveImage(imgFile, ChartImageFormat.Jpeg);
            imgFile.Close();

            consoleFile = new StreamWriter(filename, true);
            consoleFile.WriteLine("\n\n");
            consoleFile.Close();

But remember, you're not going to see any images in your text file, just a messy stream of characters corresponding to the binary image, displayed as text.