位图错误 - 内存不足

时间:2016-11-11 20:55:03

标签: c# bitmap out-of-memory

我尝试通过更改图片的频道顺序来创建一个应用程序来生成多个图像。我使用了这段代码,但是在加载大于1500x1500px的图像时出现内存不足的错误;对于较小的图像,代码可以正常工作

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
                    try
        {
            //Getting The Image From The System
            OpenFileDialog open = new OpenFileDialog();
            open.Filter =
              "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp";

            if (open.ShowDialog() == DialogResult.OK)
            {
                Bitmap img = new Bitmap(open.FileName);
                pictureBox1.Image = img;

                //get image dimension
                int width = img.Width;
                int height = img.Height;
                //3 bitmap for red green blue image

                Bitmap a01img = new Bitmap(img);
                Bitmap a02img = new Bitmap(img);
                Bitmap a03img = new Bitmap(img);
                Bitmap a04img = new Bitmap(img);
                Bitmap a05img = new Bitmap(img);
                Bitmap a06img = new Bitmap(img);
                Bitmap a07img = new Bitmap(img);
                Bitmap a08img = new Bitmap(img);
                Bitmap a09img = new Bitmap(img);
                Bitmap a10img = new Bitmap(img);
                Bitmap a11img = new Bitmap(img);
                Bitmap a12img = new Bitmap(img);
                Bitmap a13img = new Bitmap(img);
                Bitmap a14img = new Bitmap(img);
                Bitmap a15img = new Bitmap(img);
                Bitmap a16img = new Bitmap(img);
                Bitmap a17img = new Bitmap(img);
                Bitmap a18img = new Bitmap(img);
                Bitmap a19img = new Bitmap(img);
                Bitmap a20img = new Bitmap(img);
                Bitmap a21img = new Bitmap(img);
                Bitmap a22img = new Bitmap(img);
                Bitmap a23img = new Bitmap(img);
                Bitmap a24img = new Bitmap(img);
                Bitmap a25img = new Bitmap(img);
                Bitmap a26img = new Bitmap(img);
                Bitmap a27img = new Bitmap(img);
                Bitmap a28img = new Bitmap(img);
                Bitmap a29img = new Bitmap(img);
                Bitmap a30img = new Bitmap(img);
                Bitmap a31img = new Bitmap(img);
                Bitmap a32img = new Bitmap(img);
                Bitmap a33img = new Bitmap(img);
                Bitmap a34img = new Bitmap(img);
                Bitmap a35img = new Bitmap(img);
                Bitmap a36img = new Bitmap(img);
                Bitmap a37img = new Bitmap(img);
                Bitmap a38img = new Bitmap(img);
                Bitmap a39img = new Bitmap(img);
                Bitmap a40img = new Bitmap(img);
                Bitmap a41img = new Bitmap(img);
                Bitmap a42img = new Bitmap(img);
                Bitmap a43img = new Bitmap(img);
                Bitmap a44img = new Bitmap(img);
                Bitmap a45img = new Bitmap(img);
                Bitmap a46img = new Bitmap(img);
                Bitmap a47img = new Bitmap(img);
                Bitmap a48img = new Bitmap(img);
                Bitmap a49img = new Bitmap(img);
                Bitmap a50img = new Bitmap(img);
                Bitmap a51img = new Bitmap(img);
                Bitmap a52img = new Bitmap(img);
                Bitmap a53img = new Bitmap(img);
                Bitmap a54img = new Bitmap(img);
                Bitmap a55img = new Bitmap(img);
                Bitmap a56img = new Bitmap(img);
                Bitmap a57img = new Bitmap(img);
                Bitmap a58img = new Bitmap(img);
                Bitmap a59img = new Bitmap(img);
                Bitmap a60img = new Bitmap(img);
                Bitmap a61img = new Bitmap(img);
                Bitmap a62img = new Bitmap(img);
                Bitmap a63img = new Bitmap(img);
                Bitmap a64img = new Bitmap(img);
                Bitmap a65img = new Bitmap(img);
                Bitmap a66img = new Bitmap(img);



            //red green blue image
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    //get pixel value
                    Color p = img.GetPixel(x, y);

                    //extract ARGB value from p
                    int r = p.R;
                    int g = p.G;
                    int b = p.B;

                    a01img.SetPixel(x, y, Color.FromArgb(r, r, r));
                    a02img.SetPixel(x, y, Color.FromArgb(r, r, g));
                    a03img.SetPixel(x, y, Color.FromArgb(r, r, b));
                    a04img.SetPixel(x, y, Color.FromArgb(r, g, r));
                    a05img.SetPixel(x, y, Color.FromArgb(r, g, g));
                    a06img.SetPixel(x, y, Color.FromArgb(r, g, b));
                    a07img.SetPixel(x, y, Color.FromArgb(r, b, r));
                    a08img.SetPixel(x, y, Color.FromArgb(r, b, g));
                    a09img.SetPixel(x, y, Color.FromArgb(r, b, b));
                    a10img.SetPixel(x, y, Color.FromArgb(g, r, r));
                    a11img.SetPixel(x, y, Color.FromArgb(g, r, g));
                    a12img.SetPixel(x, y, Color.FromArgb(g, r, b));
                    a13img.SetPixel(x, y, Color.FromArgb(g, g, r));
                    a14img.SetPixel(x, y, Color.FromArgb(g, g, g));
                    a15img.SetPixel(x, y, Color.FromArgb(g, g, b));
                    a16img.SetPixel(x, y, Color.FromArgb(g, b, r));
                    a17img.SetPixel(x, y, Color.FromArgb(g, b, g));
                    a18img.SetPixel(x, y, Color.FromArgb(g, b, b));
                    a19img.SetPixel(x, y, Color.FromArgb(b, r, r));
                    a20img.SetPixel(x, y, Color.FromArgb(b, r, g));
                    a21img.SetPixel(x, y, Color.FromArgb(b, r, b));
                    a22img.SetPixel(x, y, Color.FromArgb(b, g, r));
                    a23img.SetPixel(x, y, Color.FromArgb(b, g, g));
                    a24img.SetPixel(x, y, Color.FromArgb(b, g, b));
                    a25img.SetPixel(x, y, Color.FromArgb(b, b, r));
                    a26img.SetPixel(x, y, Color.FromArgb(b, b, g));
                    a27img.SetPixel(x, y, Color.FromArgb(b, b, b));
                    a28img.SetPixel(x, y, Color.FromArgb(0, 0, 0));
                    a29img.SetPixel(x, y, Color.FromArgb(0, 0, g));
                    a30img.SetPixel(x, y, Color.FromArgb(0, 0, b));
                    a31img.SetPixel(x, y, Color.FromArgb(0, g, 0));
                    a32img.SetPixel(x, y, Color.FromArgb(0, g, g));
                    a33img.SetPixel(x, y, Color.FromArgb(0, g, b));
                    a34img.SetPixel(x, y, Color.FromArgb(0, b, 0));
                    a35img.SetPixel(x, y, Color.FromArgb(0, b, g));
                    a36img.SetPixel(x, y, Color.FromArgb(0, b, b));
                    a37img.SetPixel(x, y, Color.FromArgb(g, 0, 0));
                    a38img.SetPixel(x, y, Color.FromArgb(g, 0, g));
                    a39img.SetPixel(x, y, Color.FromArgb(g, 0, b));
                    a40img.SetPixel(x, y, Color.FromArgb(g, g, 0));
                    a41img.SetPixel(x, y, Color.FromArgb(g, b, 0));
                    a42img.SetPixel(x, y, Color.FromArgb(b, 0, 0));
                    a43img.SetPixel(x, y, Color.FromArgb(b, 0, g));
                    a44img.SetPixel(x, y, Color.FromArgb(b, 0, b));
                    a45img.SetPixel(x, y, Color.FromArgb(b, g, 0));
                    a46img.SetPixel(x, y, Color.FromArgb(b, g, g));
                    a47img.SetPixel(x, y, Color.FromArgb(b, b, 0));
                    a48img.SetPixel(x, y, Color.FromArgb(r, r, 0));
                    a49img.SetPixel(x, y, Color.FromArgb(r, r, b));
                    a50img.SetPixel(x, y, Color.FromArgb(r, 0, r));
                    a51img.SetPixel(x, y, Color.FromArgb(r, 0, 0));
                    a52img.SetPixel(x, y, Color.FromArgb(r, 0, b));
                    a53img.SetPixel(x, y, Color.FromArgb(r, b, 0));
                    a54img.SetPixel(x, y, Color.FromArgb(0, r, r));
                    a55img.SetPixel(x, y, Color.FromArgb(0, r, 0));
                    a56img.SetPixel(x, y, Color.FromArgb(0, r, b));
                    a57img.SetPixel(x, y, Color.FromArgb(0, 0, r));
                    a58img.SetPixel(x, y, Color.FromArgb(0, b, r));
                    a59img.SetPixel(x, y, Color.FromArgb(b, r, 0));
                    a60img.SetPixel(x, y, Color.FromArgb(b, 0, r));
                    a61img.SetPixel(x, y, Color.FromArgb(r, g, 0));
                    a62img.SetPixel(x, y, Color.FromArgb(r, 0, g));
                    a63img.SetPixel(x, y, Color.FromArgb(g, r, 0));
                    a64img.SetPixel(x, y, Color.FromArgb(g, 0, r));
                    a65img.SetPixel(x, y, Color.FromArgb(0, r, g));
                    a66img.SetPixel(x, y, Color.FromArgb(0, g, r));
                }
            }

            a01img.Save(@".\a01img.jpg");
            a02img.Save(@".\a02img.jpg");
            a03img.Save(@".\a03img.jpg");
            a04img.Save(@".\a04img.jpg");
            a05img.Save(@".\a05img.jpg");
            a06img.Save(@".\a06img.jpg");
            a07img.Save(@".\a07img.jpg");
            a08img.Save(@".\a08img.jpg");
            a09img.Save(@".\a09img.jpg");
            a10img.Save(@".\a10img.jpg");
            a11img.Save(@".\a11img.jpg");
            a12img.Save(@".\a12img.jpg");
            a13img.Save(@".\a13img.jpg");
            a14img.Save(@".\a14img.jpg");
            a15img.Save(@".\a15img.jpg");
            a16img.Save(@".\a16img.jpg");
            a17img.Save(@".\a17img.jpg");
            a18img.Save(@".\a18img.jpg");
            a19img.Save(@".\a19img.jpg");
            a20img.Save(@".\a20img.jpg");
            a21img.Save(@".\a21img.jpg");
            a22img.Save(@".\a22img.jpg");
            a23img.Save(@".\a23img.jpg");
            a24img.Save(@".\a24img.jpg");
            a25img.Save(@".\a25img.jpg");
            a26img.Save(@".\a26img.jpg");
            a27img.Save(@".\a27img.jpg");
            a28img.Save(@".\a28img.jpg");
            a29img.Save(@".\a29img.jpg");
            a30img.Save(@".\a30img.jpg");
            a31img.Save(@".\a31img.jpg");
            a32img.Save(@".\a32img.jpg");
            a33img.Save(@".\a33img.jpg");
            a34img.Save(@".\a34img.jpg");
            a35img.Save(@".\a35img.jpg");
            a36img.Save(@".\a36img.jpg");
            a37img.Save(@".\a37img.jpg");
            a38img.Save(@".\a38img.jpg");
            a39img.Save(@".\a39img.jpg");
            a40img.Save(@".\a40img.jpg");
            a41img.Save(@".\a41img.jpg");
            a42img.Save(@".\a42img.jpg");
            a43img.Save(@".\a43img.jpg");
            a44img.Save(@".\a44img.jpg");
            a45img.Save(@".\a45img.jpg");
            a46img.Save(@".\a46img.jpg");
            a47img.Save(@".\a47img.jpg");
            a48img.Save(@".\a48img.jpg");
            a49img.Save(@".\a49img.jpg");
            a50img.Save(@".\a50img.jpg");
            a51img.Save(@".\a51img.jpg");
            a52img.Save(@".\a52img.jpg");
            a53img.Save(@".\a53img.jpg");
            a54img.Save(@".\a54img.jpg");
            a55img.Save(@".\a55img.jpg");
            a56img.Save(@".\a56img.jpg");
            a57img.Save(@".\a57img.jpg");
            a58img.Save(@".\a58img.jpg");
            a59img.Save(@".\a59img.jpg");
            a60img.Save(@".\a60img.jpg");
            a61img.Save(@".\a61img.jpg");
            a62img.Save(@".\a62img.jpg");
            a63img.Save(@".\a63img.jpg");
            a64img.Save(@".\a64img.jpg");
            a65img.Save(@".\a65img.jpg");
            a66img.Save(@".\a66img.jpg");

           }
                        }



                    catch (Exception)
        {
            throw new ApplicationException("Failed loading image");
        }
        }

    }
}

1 个答案:

答案 0 :(得分:0)

您应该使用像Picasso这样的图片库。毕加索将确保没有内存不足的错误。