本文介绍了“GDI +中发生了一般性错误”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 当我尝试保存位图(我已经打开并编辑过)时,我得到GDI +中发生了一般错误。它只在某些时候发生。我不知道为什么会这样,请帮忙。它发生在第162行。 提前致谢, Jordan 使用系统; 使用 System.Collections.Generic; 使用 System.ComponentModel; 使用 System.Data; 使用 System.Drawing; 使用 System.Linq; 使用 System.Text; 使用 System.Windows.Forms; 使用 System.IO; 使用 System.Drawing.Imaging; 命名空间 WindowsFormsApplication1 { public partial class Form1:Form { public const double dratio = 16 ; public const int 比率= 16 ; public FileInfo FI = null ; int mouseX = 0 ; int mouseY = 0 ; bool lMouse = false ; bool rMouse = false ; 颜色[,,,] map = 新颜色[ 16 , 16 , 8 , 8 ]; 颜色[,] buf = 新颜色[ 8 , 8 ]; public Form1() { InitializeComponent(); } private void panel1_MouseMove( object sender,MouseEventArgs e) { Panel panel =(Panel)sender; 使用(Graphics view = panel.CreateGraphics()) { 使用(位图b = 新位图(panel.Width,panel.Height)) { 使用(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。值,( 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 真; 其他 返回 false ; } private void panel1_Paint( object sender,PaintEventArgs e) { Graphics g =((Panel)sender).CreateGraphics(); g.Clear(Color.White); if (lMouse&& mouseInBounds())map [( int )numericUpDown1。值,( 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 ; } } 私有 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 ); 返回; } 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 {位图文件= 新位图( 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( 保存到: + FI.FullName, 完成!,MessageBoxButtons.OK,MessageBoxIcon.Information); toolStripProgressBar1.Value = 0 ; file.Dispose(); } } 私有 void saveAsToolStripMenuItem_Click( object sender,EventArgs e) { SaveFileDialog SFD = new SaveFileDialog() ; SFD.Filter = Minecraft PNG字体文件(* .png)| * .png; if (SFD.ShowDialog()== DialogResult.OK) { FI = new FileInfo(SFD.FileName); saveToolStripMenuItem_Click( null , null ); } } 私有 void openToolStripMenuItem_Click( object sender,EventArgs e) { OpenFileDialog OFD = new OpenFileDialog() ; OFD.Filter = Minecraft PNG字体文件(* .png)| * .png; if (OFD.ShowDialog()== DialogResult.OK) { Bitmap b = 新位图(Image.FromFile(OFD.FileName)); if ((b.Width% 128 == 1 || b.Width% 128 == 1 )&& b.Width!= b.Height) { if (MessageBox.Show( 文件似乎不是字体文件,可能无法正确加载。是否要继续加载此文件?, 未知文件格式,MessageBoxButtons.YesNo,MessageBoxIcon.Warning)== DialogResult.Yes) { FI = new FileInfo(OFD.FileName); b = new 位图(b, 128 , 128 ); loadFont(b); } } 其他 { FI = new FileInfo(OFD.FileName); if (b.Width!= 128 || b.Height!= 128 )b = new 位图(b, 128 , 128 ); loadFont(b); } b.Dispose(); } } private void loadFont(位图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 ); } } } 解决方案 检查保存文件的文件夹权限。 你应该有权保存文件。 另外,确保文件不存在并在另一个程序中打开 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,Jordanusing 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); } }} 解决方案 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. 这篇关于“GDI +中发生了一般性错误”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 14:30