简体   繁体   中英

Out of Memory exception while loading image to BitMap

I am working on app, that will edit images. But it crashes because of system.outofmemoryexception , when I try to load the image from disc. My RAM is pretty empty, but what I observed is, that app crashes, when the memory reach 30MB in memory. So maybe I need to allocate more memory? Here is the code, where the exception is called:

using(Bitmap DocasnyObrazek = (Bitmap)Bitmap.FromFile(otevreniSouboru.FileName)) {...} Here is full code for opening an loading image:

namespace Editor_Obrázků_2._0
{
    public partial class Form1 : Form
    {
        Bitmap Obrazek;//datová složka pro obrázek
        Color[,] Barvy; //datová složka pro barvy
        private void OtevriSoubor() //otevření souboru
        {
            OpenFileDialog otevreniSouboru = new OpenFileDialog();//deklarace metody
            otevreniSouboru.Filter = "Obrázky (*.bmp, *.jpg|*.bmp; *.jpg";//povolené přípony souboru
            otevreniSouboru.ShowDialog();//zobrazí okno pro vybrání souboru
            if (otevreniSouboru.FileName != "") //jestli existuje soubor tak otevřít
            {
                using(Bitmap DocasnyObrazek = (Bitmap)Bitmap.FromFile(otevreniSouboru.FileName))//using se stará o pamět, Obrázek bude přetypován na Bitmap
                {
                    if (DocasnyObrazek.Width <= 500 && DocasnyObrazek.Height <= 500) //pokud je menší než 500x500
                    {
                        if (Obrazek != null)
                        {
                            Obrazek.Dispose();
                            Obrazek = null;
                        }
                        Obrazek = (Bitmap)DocasnyObrazek.Clone();//přetypování a nahrání dat    "clone" neodkazuje na data ale předává je
                        Barvy = new Color[Obrazek.Width, Obrazek.Height]; //pole pro barvy
                        for (int sloupec = 0; sloupec < Obrazek.Width; sloupec++)//nahrání barev
                            for (int radek = 0; radek < Obrazek.Height; radek++)
                            {
                                Barvy[sloupec, radek] = Obrazek.GetPixel(sloupec, radek);
                            }
                        this.seznamEfektů.Enabled = true;
                    }
                    else//obrázek je příliš velký nelze načíst
                    {
                        MessageBox.Show("Obrázek je příliš velký", "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Error);

                    }
                }
            }
        }
        public Form1()
        {
            InitializeComponent();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            OtevriSoubor();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            if (Obrazek != null)
            {
                e.Graphics.DrawImage(Obrazek, 10, 10);
            }
        }
    }
}

I found out, why this wasn't working. In the documentation the outofmemoryexception refers to problem with the jpg or bmp file, not problem with memory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM