C# GDI 中的外部异常+

发布于 2024-07-20 16:42:42 字数 2684 浏览 4 评论 0原文

我想运行这段代码,

        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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

幻想少年梦 2024-07-27 16:42:42

我过去也遇到过类似的问题,我的克隆位图有一些伪影。 研究这个问题一段时间后,我偶然发现了 这个帖子 这有帮助。

尝试用以下内容替换您的 Clone()

Bitmap grayImage = (Bitmap)img.Clone();

Bitmap grayImage = new Bitmap(img);

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()

Bitmap grayImage = (Bitmap)img.Clone();

with this:

Bitmap grayImage = new Bitmap(img);
一影成城 2024-07-27 16:42:42

很久以前,在我的一个队友的系统上处理图像(通过从 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.

回眸一遍 2024-07-27 16:42:42

如果您只是覆盖所有内容(或左上角的矩形),为什么还要克隆另一个图像?

Why are you cloning another image if you're just overwriting everything (or the top-left rectangle)?

半葬歌 2024-07-27 16:42:42

我不知道这个错误,但这可能是 LockBits 的情况......我会看看是否可以举一个例子。

下面是一个将数据数组写入 ARGB 位图的简化示例:

    // fake data
    int[,] data = new int[100, 150];
    int width = data.GetLength(0), height= data.GetLength(1);
    for (int x = 0; x < width; x++)
        for (int y = 0; y < height; y++)
            data[x, y] = x + y;

    // process it into a Bitmap
    Bitmap bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb);
    BitmapData bd = bmp.LockBits(new Rectangle(0, 0, width, height),
       ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
    unsafe {
        byte* root = (byte*)bd.Scan0;
        for (int y = 0; y < height; y++) {
            byte* pixel = root;
            for (int x = 0; x < width; x++) {
                byte col = (byte)data[x, y];
                pixel[0] = col;
                pixel[1] = col;
                pixel[2] = col;
                pixel[3] = 255;
                pixel += 4;
            }
            root += bd.Stride;
        }
    }
    bmp.UnlockBits(bd);
    bmp.Save("foo.bmp"); // or show on screen, etc

此方法应该比 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:

    // fake data
    int[,] data = new int[100, 150];
    int width = data.GetLength(0), height= data.GetLength(1);
    for (int x = 0; x < width; x++)
        for (int y = 0; y < height; y++)
            data[x, y] = x + y;

    // process it into a Bitmap
    Bitmap bmp = new Bitmap(width, height, PixelFormat.Format32bppArgb);
    BitmapData bd = bmp.LockBits(new Rectangle(0, 0, width, height),
       ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
    unsafe {
        byte* root = (byte*)bd.Scan0;
        for (int y = 0; y < height; y++) {
            byte* pixel = root;
            for (int x = 0; x < width; x++) {
                byte col = (byte)data[x, y];
                pixel[0] = col;
                pixel[1] = col;
                pixel[2] = col;
                pixel[3] = 255;
                pixel += 4;
            }
            root += bd.Stride;
        }
    }
    bmp.UnlockBits(bd);
    bmp.Save("foo.bmp"); // or show on screen, etc

This approach should be a lot faster than SetPixel.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文