当我运行我的应用程序时,以下错误被抛到了我的脸上“GDI +中发生了一般错误。” 我环顾四周,看到人们有类似的错误,但没有找到真正的解决方案,或者实施屁股真的很痛苦。那些没有得到解决方案的人还没有发布他们的代码。
所以我认为我不妨试一试并抛出另一个关于如何修复此错误的线程。这是我的代码和错误
Random r = new Random();
DirectoryInfo di = new DirectoryInfo(folderbrowser.SelectedPath);
FileInfo[] fi = di.GetFiles().Where(f => extensions.Contains(f.Extension.ToLower())).ToArray();
for (int i = 0; i < imageCount; i++)
{
int img1 = r.Next(imageCount);
int img2 = r.Next(imageCount);
while (img2 == img1)
img2 = r.Next(imageCount);
pic1.Image = Image.FromFile(fi[img1].FullName);
pic2.Image = Image.FromFile(fi[img2].FullName);
Image i1 = pic1.Image;
Image i2 = pic2.Image;
Bitmap bitmap = new Bitmap(i1.Width + i2.Width, 1080);
using (Graphics g = Graphics.FromImage(bitmap))
{
g.DrawImage(i1, 0, 0);
g.DrawImage(i2, i2.Width, 0);
}
bitmap.Save(@"C:\TEST\Image_"+i.ToString());
}
这是错误代码
System.Runtime.InteropServices.ExternalException was unhandled
Message=A generic error occurred in GDI+.
Source=System.Drawing
ErrorCode=-2147467259
StackTrace:
at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
at System.Drawing.Image.Save(String filename, ImageFormat format)
at System.Drawing.Image.Save(String filename)
at WallpaperMerger.Form1.btn_merge_Click(Object sender, EventArgs e) in C:\Users\PeppeJ\documents\visual studio 2010\Projects\WallpaperMerger\WallpaperMerger\Form1.cs:line 113
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at WallpaperMerger.Program.Main() in C:\Users\PeppeJ\documents\visual studio 2010\Projects\WallpaperMerger\WallpaperMerger\Program.cs:line 18
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
那么你们在StackOverflow上有什么想法?我是否和其他船一样,必须键入一个非常复杂的代码来解决这个问题,或者我犯了一个简单的错误并且很容易纠正?
如果你想自己测试一下,如果这可能有所帮助,这里有Debug Build的链接
只需勾选“使用多个文件”,选择包含图像文件的文件夹(例如C:\ Users \ Public \ Pictures \ Sample Pictures)。点击合并。
我想补充一点,我在这个函数中使用了Bitmap.Save,它运行完美。
Bitmap bitmap = new Bitmap(pic1.Image.Width + pic2.Image.Width, 1080);
using (Graphics g = Graphics.FromImage(bitmap))
{
g.DrawImage(pic1.Image, 0, 0);
g.DrawImage(pic2.Image, pic1.Image.Width, 0);
}
if (savepicfile.ShowDialog() == DialogResult.OK)
bitmap.Save(savepicfile.FileName);
在上面的函数文件中加载如下:
DialogResult result = pic1file.ShowDialog();
if (result == DialogResult.OK)
pic1.Image = Image.FromFile(pic1file.FileName);
值得一提的是“picX”是一个“System.Windows.Forms.PictureBox”对象。
答案 0 :(得分:5)
根据我的经验,99%的时间此错误是由以下问题引起的:
偶尔我会遇到它:
我先检查前两件事。如果可以排除这一点,我可以看到你没有在你提供的代码中正确处理一些对象。我会清理它。如果你没有在你应该的时候调用Dispose(),那么GDI +与.NET的GC并不是特别合作。
var r = new Random();
var di = new DirectoryInfo(folderbrowser.SelectedPath);
var fi = di.GetFiles().Where(f => extensions.Contains(f.Extension.ToLower())).ToArray();
for (var i = 0; i < imageCount; i++)
{
var img1 = r.Next(imageCount);
var img2 = r.Next(imageCount);
while (img2 == img1)
{
img2 = r.Next(imageCount);
}
pic1.Image = Image.FromFile(fi[img1].FullName);
pic2.Image = Image.FromFile(fi[img2].FullName);
var i1 = pic1.Image;
var i2 = pic2.Image;
using (var bitmap = new Bitmap(i1.Width + i2.Width, 1080))
using (var g = Graphics.FromImage(bitmap))
{
g.DrawImage(i1, 0, 0);
g.DrawImage(i2, i2.Width, 0);
bitmap.Save(@"C:\TEST\Image_"+i.ToString());
}
}
编辑:更多想法(嗯,真正澄清上面的数字2)......
C:\TEST
目录中的图像,则可以看到此错误。调试想法:
答案 1 :(得分:2)
我找到了答案,目录“\ TEST \”不存在。当我创建它时,它完美无缺。
答案 2 :(得分:2)
我遇到了这个确切的问题,并修复了将bitmap包装在using子句中的问题。还需要处理位图
using (Bitmap bmp = new Bitmap())
{
// Bitmap operation
}
答案 3 :(得分:1)
在处理Graphics对象之前尝试执行bitmap.Save:
Bitmap bitmap = new Bitmap(i1.Width + i2.Width, 1080);
using (Graphics g = Graphics.FromImage(bitmap))
{
g.DrawImage(i1, 0, 0);
g.DrawImage(i2, i2.Width, 0);
bitmap.Save(@"C:\TEST\Image_"+i.ToString());
}