Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I get "A generic error occurred in GDI+" when I try to save a bitmap(that I have opened and edited). It only happens some of the time. I have no idea why this is happening, please help. It occurs at line 162.

Thanks in advance,
Jordan

C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public const double dratio = 16;
        public const int ratio = 16;

        public FileInfo FI = null;

        int mouseX = 0;
        int mouseY = 0;

        bool lMouse = false;
        bool rMouse = false;

        Color[,,,] map = new Color[16, 16, 8, 8];
        Color[,] buf = new Color[8, 8];

        public Form1()
        {
            InitializeComponent();
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            Panel panel = (Panel)sender;
            using (Graphics view = panel.CreateGraphics())
            {
                using (Bitmap b = new Bitmap(panel.Width, panel.Height))
                {
                    using (Graphics g = Graphics.FromImage(b))
                    {
                        g.Clear(Color.White);
                        mouseX = (int)Math.Floor(e.X / dratio);
                        mouseY = (int)Math.Floor(e.Y / dratio);
                        if (lMouse && mouseInBounds()) map[(int)numericUpDown1.Value, (int)numericUpDown2.Value, mouseX, mouseY] = Color.Black;
                        else if (rMouse && mouseInBounds()) map[(int)numericUpDown1.Value, (int)numericUpDown2.Value, mouseX, mouseY] = Color.White;
                        for (int x = 0; x < 8; x++)
                            for (int y = 0; y < 8; y++)
                            {
                                Pen pen = Pens.Black;
                                if (map[(int)numericUpDown1.Value, (int)numericUpDown2.Value, x, y] == Color.Black) pen = Pens.White;
                                g.FillRectangle(new SolidBrush(map[(int)numericUpDown1.Value, (int)numericUpDown2.Value, x, y]), x * ratio, y * ratio, ratio, ratio);
                                if (gridToolStripMenuItem.Checked)
                                g.DrawRectangle(pen, x*ratio, y*ratio, ratio, ratio);
                            }
                        g.DrawRectangle(Pens.Red, mouseX*ratio, mouseY*ratio, ratio, ratio);
                        view.DrawImage(b, 0, 0);
                    }
                }
            }
        }

        private bool mouseInBounds()
        {
            if (mouseX >= 0 && mouseX < 8 && mouseY >= 0 && mouseY < 8) return true;
            else return false;
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = ((Panel)sender).CreateGraphics();
            g.Clear(Color.White);
            if (lMouse && mouseInBounds()) map[(int)numericUpDown1.Value, (int)numericUpDown2.Value, mouseX, mouseY] = Color.Black;
            else if (rMouse && mouseInBounds()) map[(int)numericUpDown1.Value, (int)numericUpDown2.Value, mouseX, mouseY] = Color.White;
            for (int x = 0; x < 8; x++)
                for (int y = 0; y < 8; y++)
                {
                    Pen pen = Pens.Black;
                    if (map[(int)numericUpDown1.Value, (int)numericUpDown2.Value, x, y] == Color.Black) pen = Pens.White;
                    g.FillRectangle(new SolidBrush(map[(int)numericUpDown1.Value, (int)numericUpDown2.Value, x, y]), x * ratio, y * ratio, ratio, ratio);
                    if (gridToolStripMenuItem.Checked)
                        g.DrawRectangle(pen, x * ratio, y * ratio, ratio, ratio);
                }
            g.Dispose();
        }

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            switch (e.Button)
            {
                case MouseButtons.Left:
                    map[(int)numericUpDown1.Value, (int)numericUpDown2.Value, mouseX, mouseY] = Color.Black;
                    lMouse = true;
                    break;
                case MouseButtons.Right:
                    map[(int)numericUpDown1.Value, (int)numericUpDown2.Value, mouseX, mouseY] = Color.White;
                    rMouse = true;
                    break;
            }
        }

        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            switch (e.Button)
            {
                case MouseButtons.Left:
                    lMouse = false;
                    break;
                case MouseButtons.Right:
                    rMouse = false;
                    break;
            }
        }

        private void gridToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem item = (ToolStripMenuItem)sender;
            if (item.Checked) { item.Checked = false; panel1_Paint(panel1, null); return; }
            item.Checked = true;
            panel1_Paint(panel1, null);
        }

        private void numericUpDown_ValueChanged(object sender, EventArgs e)
        {
            panel1_Paint(panel1, null);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (numericUpDown1.Value != 15) numericUpDown1.Value++;
            else if (numericUpDown2.Value != 15) { numericUpDown1.Value = 0; numericUpDown2.Value++; }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (numericUpDown1.Value != 0) numericUpDown1.Value--;
            else if (numericUpDown2.Value != 0) { numericUpDown1.Value = 15; numericUpDown2.Value--; }
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (FI == null) saveAsToolStripMenuItem_Click(null, null);
            else
            {
                Bitmap file = new Bitmap(128, 128);
                for(int x1 = 0; x1 < 16; x1++)
                    for(int y1 = 0; y1 < 16; y1++)
                        for(int x2 = 0; x2 < 8; x2++)
                            for(int y2 = 0; y2 < 8; y2++)
                                if (map[x1, y1, x2, y2] == Color.Black)
                                {
                                    file.SetPixel((x1 * 8) + x2, (y1 * 8) + y2, Color.White);
                                    toolStripProgressBar1.Value++;
                                }
                                else
                                {
                                    file.SetPixel((x1 * 8) + x2, (y1 * 8) + y2, Color.Transparent);
                                    toolStripProgressBar1.Value++;
                                }
                file.Save(FI.FullName, ImageFormat.Png);
                MessageBox.Show("Saved To:" + FI.FullName, "Done!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                toolStripProgressBar1.Value = 0;
                file.Dispose();
            }
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog SFD = new SaveFileDialog();
            SFD.Filter = "Minecraft PNG Font Files(*.png)|*.png";
            if (SFD.ShowDialog() == DialogResult.OK)
            {
                FI = new FileInfo(SFD.FileName);
                saveToolStripMenuItem_Click(null, null);
            }
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog OFD = new OpenFileDialog();
            OFD.Filter = "Minecraft PNG Font Files(*.png)|*.png";
            if (OFD.ShowDialog() == DialogResult.OK)
            {
                Bitmap b = new Bitmap(Image.FromFile(OFD.FileName));
                if ((b.Width % 128 == 1 || b.Width % 128 == 1) && b.Width != b.Height)
                {
                    if (MessageBox.Show("File does not appear to be a font file, it may not load correctly. Would you like to continue loading this file?", "Unkown File Format", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                    {
                        FI = new FileInfo(OFD.FileName);
                        b = new Bitmap(b, 128, 128);
                        loadFont(b);
                    }
                }
                else
                {
                    FI = new FileInfo(OFD.FileName);
                    if (b.Width != 128 || b.Height != 128)
                        b = new Bitmap(b, 128, 128);
                    loadFont(b);
                }
                b.Dispose();
            }
        }

        private void loadFont(Bitmap b)
        {
            for (int x1 = 0; x1 < 16; x1++)
                for (int y1 = 0; y1 < 16; y1++)
                    for (int x2 = 0; x2 < 8; x2++)
                        for (int y2 = 0; y2 < 8; y2++)
                        {
                            //MessageBox.Show(b.GetPixel((x1*8)+x2, (y1*8)+y2)+" "+Color.White);
                            if (b.GetPixel((x1 * 8) + x2, (y1 * 8) + y2).Equals(Color.FromArgb(255, 255, 255, 255)))
                            {
                                map[x1, y1, x2, y2] = Color.Black;
                                toolStripProgressBar1.Value++;
                            }
                            else
                            {
                                map[x1, y1, x2, y2] = Color.White;
                                toolStripProgressBar1.Value++;
                            }
                            toolStripProgressBar1.Value = 0;
                            numericUpDown1.Value = 0;
                            numericUpDown2.Value = 0;
                        }
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Array.Clear(map, 0, map.Length);
            panel1_Paint(panel1, null);
        }
    }
}
Posted

1 solution

Check the folder permissions where you are saving the file.
You should have permissions to save the file.

In addition, ensure that the file does not already exist and is open in another program.
 
Share this answer
 
Comments
Sicppy 16-Mar-14 7:24am    
Thanks, not sure if it fixed it yet, the problem stopped when I created, a new file and messed around with it, but I'll keep experimenting, I'll let you know as soon as I find out!
Abhinav S 16-Mar-14 7:31am    
Seemed to have been an issue with the file being open in another program.
Sicppy 16-Mar-14 17:12pm    
Does not appear to have occurred again, so I'll accept your answer for now, however it showed up in another context and it seemed to be an error with the object being disposed and then being used again

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900