0

I've written a quick winform app that can take some entered text, generate images based on all the system fonts and then consolidate those images into a single image to give examples of the fonts. For ease, I've separated the two functions, one to generate the images and another to consolidate them so I can remove fonts I don't want in the single image. It was all working yesterday, but now when it comes to saving the consolidated image ("complete.save("consolidated.png");) it gives the useless GDI+ error. I've checked paths and access, all are fine and correct. Nothing is locking the image, so I'm totally at a loss as what is causing this. Any ideas? Code below

StringBuilder sb = new StringBuilder();

        List<string> files = FileSystemUtilities.ListFiles("fonts");

        int height = 0;
        int width = 0;

        Bitmap test = new Bitmap(1000, 1000);
        Graphics gTest = Graphics.FromImage(test);

        Font font = new Font("Arial", 128);

        int numWidth = 0;

        int count = 1;

        foreach (var file in files)
        {
            sb.AppendFormat("{0}\r", FileSystemUtilities.GetFileName(file).Replace(".png", string.Empty));

            Bitmap bitmap = new Bitmap(file);

            height = height + bitmap.Height + 10;

            if (width < bitmap.Width)
            {
                width = bitmap.Width;
            }

            SizeF numSize = gTest.MeasureString(Convert.ToString(count), font);

            if (numWidth < numSize.Width)
            {
                numWidth = Convert.ToInt32(numSize.Width + 1);
            }

            bitmap.Dispose();

            count++;
        }

        test.Dispose();
        gTest.Dispose();

        numWidth = numWidth + 10;
        count = 1;
        Bitmap complete = new Bitmap(width + numWidth, height);
        Graphics g = Graphics.FromImage(complete);

        g.FillRectangle(Brushes.White, 0, 0, complete.Width, complete.Height);

        int y = 0;
        foreach (var file in files)
        {
            Bitmap bitmap = new Bitmap(file);

            g.DrawString(Convert.ToString(count) + ".", font, Brushes.Black, 0, y);

            g.DrawImage(bitmap, numWidth, y);

            y = y + bitmap.Height + 10;

            bitmap.Dispose();

            count++;
        }

        string filename = "consolidated.png";

        if (File.Exists(filename))
        {
            File.Delete(filename);
        }

        g.Dispose();
        complete.Save("consolidated.png");
        complete.Dispose();

        TextFileUtilities.WriteTextFile("consolidated.txt", sb.ToString());
mikelomaxxx14
  • 133
  • 12

1 Answers1

0

Turns out it was due to excessive height... I couldn't see it. Thank you for all that commented and helped me out.

mikelomaxxx14
  • 133
  • 12