C# GDI 中的外部异常+
我想运行这段代码,
Bitmap grayImage = (Bitmap)img.Clone();
for (int x = 0; x < arr.GetLength(0); x++)
{
for (int y = 0; y < arr.GetLength(1); y++)
{
int col = arr[x, y];
Color grau = Color.FromArgb(col, col, col);
grayImage.SetPixel(x, y, grau);
}
}
如果我运行代码,我会在这一行中遇到异常:grayImage.SetPixel(x, y, grau);
以下是异常详细信息:
System.Runtime.InteropServices.ExternalException wurde nicht behandelt。 Message="GDI+ 中发生一般错误。" 来源=“系统.绘图” 错误代码=-2147467259 堆栈跟踪: 在 System.Drawing.Bitmap.SetPixel(Int32 x, Int32 y, Color 颜色) 在 D:\Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Bild.cs 中的 Metalldetektor.Bild.ArrToPic(Int32[,] arr, Image img):第 44 行 在 D:\Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 中的 Metalldetektor.Form1.button2_Click(Object sender, EventArgs e):第 58 行 在 System.Windows.Forms.Control.OnClick(EventArgs e) 在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent) 在 System.Windows.Forms.Control.WmMouseUp(Message&m, MouseButtons 按钮, Int32 点击) 在 System.Windows.Forms.Control.WndProc(Message&m) 在 System.Windows.Forms.ButtonBase.WndProc(Message&m) 在 System.Windows.Forms.Button.WndProc(Message&m) 在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m) 在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam) 在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID,Int32原因,Int32 pvLoopData) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 原因,ApplicationContext 上下文) 在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 原因,ApplicationContext 上下文) 在 D:\Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Program.cs 中的 Metalldetektor.Program.Main() 处:第 19 行 在 System.AppDomain._nExecuteAssembly(程序集,String[] args) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在 System.Threading.ExecutionContext.Run(ExecutionContextexecutionContext,ContextCallback 回调,对象状态) 在 System.Threading.ThreadHelper.ThreadStart() InnerException:
我不知道该怎么办,请帮忙!
I want to run this piece of code
Bitmap grayImage = (Bitmap)img.Clone();
for (int x = 0; x < arr.GetLength(0); x++)
{
for (int y = 0; y < arr.GetLength(1); y++)
{
int col = arr[x, y];
Color grau = Color.FromArgb(col, col, col);
grayImage.SetPixel(x, y, grau);
}
}
if I run the code I get an exception in this line: grayImage.SetPixel(x, y, grau);
Here are the Exception Details:
System.Runtime.InteropServices.ExternalException wurde nicht behandelt.
Message="A generic error occurred in GDI+."
Source="System.Drawing"
ErrorCode=-2147467259
StackTrace:
at System.Drawing.Bitmap.SetPixel(Int32 x, Int32 y, Color color)
at Metalldetektor.Bild.ArrToPic(Int32[,] arr, Image img) in D:\Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Bild.cs:line 44
at Metalldetektor.Form1.button2_Click(Object sender, EventArgs e) in D:\Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:line 58
at System.Windows.Forms.Control.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.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(Int32 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 Metalldetektor.Program.Main() in D:\Documents\Visual Studio 2008\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Program.cs:line 19
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
I don't know what to do so please help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我过去也遇到过类似的问题,我的克隆位图有一些伪影。 研究这个问题一段时间后,我偶然发现了 这个帖子 这有帮助。
尝试用以下内容替换您的 Clone()
:
I had a similar problem in the past where my cloned bitmap had some artifacts. After researching this problem for a while, I stumbled upon this thread which helped.
Try replacing your Clone()
with this:
很久以前,在我的一个队友的系统上处理图像(通过从 SQL Server 读取二进制数据创建图像)时出现了一些错误。 相同的代码在其他机器上运行良好。 原来他安装了一些图形驱动程序的更新,导致了问题。
A long time ago there was some error when processing image (creating image by reading binary data from SQL server) on one of my teammate's system. The same code worked fine on other machines. It turned out that he had installed some update for graphics driver and that caused problems.
如果您只是覆盖所有内容(或左上角的矩形),为什么还要克隆另一个图像?
Why are you cloning another image if you're just overwriting everything (or the top-left rectangle)?
我不知道这个错误,但这可能是 LockBits 的情况......我会看看是否可以举一个例子。
下面是一个将数据数组写入 ARGB 位图的简化示例:
此方法应该比
SetPixel
快很多。I don't know about the bug, but this might be a case for LockBits... I'll see if I can whip up an example.
Here's a simplified example writing an array of data to an ARGB bitmap:
This approach should be a lot faster than
SetPixel
.