Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C语言中的内存不足异常#_C#_Out Of Memory - Fatal编程技术网

C# C语言中的内存不足异常#

C# C语言中的内存不足异常#,c#,out-of-memory,C#,Out Of Memory,我是C#的新手。 因此,我不太确定我的程序有什么问题。 该程序可以处理小图像,但当它处理大约A4大小的大图像时,会显示“内存不足异常”。 然而,如果程序不能处理大图像,它将是无用的。 我怎样才能解决这个问题? 谢谢 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; using System.Drawing.Drawing2D; us

我是C#的新手。 因此,我不太确定我的程序有什么问题。 该程序可以处理小图像,但当它处理大约A4大小的大图像时,会显示“内存不足异常”。 然而,如果程序不能处理大图像,它将是无用的。 我怎样才能解决这个问题? 谢谢

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;


namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            //Bitmap objects

            //input image
            Bitmap bmOrg = (Bitmap)Bitmap.FromFile(@"C:\B.png");  
            Bitmap bmTransparentLayover = new Bitmap(bmOrg.Width, bmOrg.Height);

            //Create Graphic Objects.
            Graphics gOriginal = Graphics.FromImage(bmOrg);
            Graphics gTransparentLayover = Graphics.FromImage(bmTransparentLayover);

            //Set Transparent Graphics back ground to an "odd" color 
            //     that hopefully won't be used to
            //Be changed to transparent later.
            gTransparentLayover.FillRectangle
                                ( Brushes.Pink, 
                                  new Rectangle
                                  (0, 
                                   0, 
                                   bmTransparentLayover.Width, 
                                   bmTransparentLayover.Height
                                  )
                                );

            //Draw "transparent" graphics that will look through 
            //  the overlay onto the original.
            //Using LimeGreen in hopes that it's not used.

            Point[] points = new Point[5];
            points[0] = new Point(130, 140);
            points[1] = new Point(130, 370);
            points[2] = new Point(420, 370);
            points[3] = new Point(420, 140);
            points[4] = new Point(130, 140);
            System.Drawing.Drawing2D.GraphicsPath gp = new
            System.Drawing.Drawing2D.GraphicsPath();
            gp.AddPolygon(points);
            gTransparentLayover.FillPath(Brushes.LimeGreen, gp);

            //Now make the LimeGreen Transparent to see through overlay.
            bmTransparentLayover.MakeTransparent(Color.LimeGreen);

            //draw the overlay on top of the original.
            gOriginal.DrawImage(bmTransparentLayover, 
             new Rectangle(0, 0, bmTransparentLayover.Width, bmTransparentLayover.Height));

            //Create new image to make the overlays background tranparent
            Bitmap bm3 = new Bitmap(bmOrg);
            bm3.MakeTransparent(Color.Pink);

            //Save file.
            //to save the output image 
            bm3.Save(@"save.png",System.Drawing.Imaging.ImageFormat.Png);  

            Image img = new Bitmap(480, 480);

            //the background image 
            img = Image.FromFile(@"a.png");  
            Graphics g = Graphics.FromImage(img);

            //to save the combined image 
            g.DrawImage(Image.FromFile(@"save.png"), new Point(-50, -70));
            img.Save(@"final.png", ImageFormat.Png); 
        }
    }
}

我怀疑您的主要问题是您同时使用了大量位图和图形实例

可能值得尝试重新组织您的代码,以便在任何时候都尽可能少地显示这些内容,使用.Dispose()删除您已完成的项目

它是一个9992x8750图像

是的,您正处于32位进程的危险区域。该映像需要一大块连续地址空间,334兆字节。当您在程序启动后尽早加载映像时,您将很容易得到这一点。在那一点上,你可以得到大约650兆字节的数据

但是,当您的程序分配和释放内存并加载两个程序集时,这一点就不那么重要了。地址空间变得支离破碎,分配之间的漏洞越来越小。只需加载一个带有笨拙基址的DLL,就会突然将最大的漏洞减少两倍多。问题不在于可用的虚拟内存总量,它可能是一个GB或更多,而在于最大可用块的大小。一段时间后,90兆字节的分配失败并不完全罕见。仅仅是一次分配可能失败,你仍然可以分配一堆,比如说,50兆字节的块


没有解决地址空间碎片的方法,也无法修复位图类存储像素数据的方式。但首先,指定64位版本的Windows作为程序的要求。它提供了大量的虚拟内存,碎片化不是问题。如果您想深入了解它,那么您可以使用SysInternals'深入了解VM布局。小心您可能遇到的信息过载。

尝试在Windows 64位上工作,这是大多数内存问题的解决方案。

与回答您的问题无关,您必须意识到您必须关闭并处理图形对象。使用语句用
填充代码片段。那就行了。A4大小没有任何意义。我可以创建一个只有4个像素的A4大小的图像:)不确定问题出在哪里,但尝试在每个实现IDisposable(位图、图形)的对象周围添加语句。当您查看任务管理器时,进程使用了多少内存?失败的大图像的整体像素尺寸是多少?作为32位位图,它的大小为9992*8750*(32/8),约为349 MB