Saving array of bitmaps to separate files with C#

642 Views Asked by At

I'm stuck at saving an array of System.Drawing.Bitmap type, each bitmap to separate file. I have an array "survey". This array stores several Lists of double type. For each List i want to create a bitmap and then save it as a bmp file. The line raport[i].Save(Path.Combine(myfilepath, nets[i] + ".bmp")); returns TypeInitializationException - and i don't know why. The piece nets[i] is a dictionary (int, string) with expected file names.

public void save_results()
    {
        System.Drawing.Bitmap[] raport = new System.Drawing.Bitmap[survey.Length];

        for (int i = 0; i < survey.Length; i++)
        {
            raport[i] = new System.Drawing.Bitmap(survey[i].Count, 1000);

            for (int x = 0; x < survey[i].Count; x++)
                for (int y = 0; y < 1000; y++)
                    raport[i].SetPixel(x, y, Color.FromArgb(255, 255, 255));

            for (int x = 0; x < survey[i].Count; x++)
                raport[i].SetPixel(x, (int)(1000 - Math.Floor(survey[i][x] * 1000) >= 1000 ? 999 : 1000 - Math.Floor(survey[i][x] * 1000)), Color.FromArgb(0, 0, 0));

    raport[i].Save(Path.Combine(myfilepath, nets[i] + ".bmp"));
        }
    }
1

There are 1 best solutions below

0
On BEST ANSWER

Finally, the problem was associated with the variable "myfilepath". The variable was 'compiled' from few file paths - and all of those strings should have been static:

    public static string mydoc= Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
    public static string myfilepath_p = Path.Combine(mydoc, "Demeter");
    public static string myfilepath= Path.Combine(myfilepath_p, "regresja_liniowa");

Originally, only the 'final' variable used in the cited code was static, what caused an error.

Rest of the code worked fine.