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
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++)
{
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);
}
}
}