out of memory

LiPsOr

Member
Joined
Mar 9, 2006
Messages
5
Hi Im trying to create an negative image.
This is the code I use to do it. I got it from the Bob Powell FAQ.
C#:
private void button2_Click(object sender, EventArgs e)
        {
            if(this.pictureBox1.Visible == true)
            {
                Image img = this.pictureBox1.Image;

                Bitmap copy = new Bitmap(img.Width,img.Height);

                ImageAttributes ia = new ImageAttributes();


                ColorMatrix cm = new ColorMatrix();
                cm.Matrix00 = cm.Matrix11 = cm.Matrix22 = 0.99f;
                cm.Matrix33 = cm.Matrix44 = 1;
                cm.Matrix40 = cm.Matrix41 = cm.Matrix42 = .04f;

                                
                ia.SetColorMatrix(cm);

                
                Graphics g = Graphics.FromImage(copy);
           
                g.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia);



                g.Dispose();

                img.Dispose();
            }
The problem I have is, when Im click the button I get an out of memory exception at the g.DrawImage line.

Thanks for the help
 
Last edited by a moderator:
What specs are your PC, also what sized Image are you using. I ran the code which you posted without any errors. On a side note, it simply made the image lighter not a negative.
 
Cags said:
What specs are your PC, also what sized Image are you using. I ran the code which you posted without any errors. On a side note, it simply made the image lighter not a negative.

I ran a Pentium 3Ghz with 1GB Ram and a Geforce 6600gt. So I think that isnt the problem.
The images are all not bigger than 800Kb normally pics under 100Kb the resolution is also small.

Its always the same other people try the code and it is allright!
:(
 
Try adding the code to a new project just to check and see if that solves your problem. There doesnt appear to be anything inherantly wrong with that code.
 
could it be that Im missing some dll or Im using the wrong dll?
 
Last edited by a moderator:
Seems unlikely your missing a dll due to the error. Are you using .Net 2.0 or 1.0/1.1? Im using 1.1 and had no trouble at all with the code you have posted.
 
Cags said:
Seems unlikely your missing a dll due to the error. Are you using .Net 2.0 or 1.0/1.1? Im using 1.1 and had no trouble at all with the code you have posted.

Im using 2.0...maybe thats the prob I will try it :-\
 
Hmm, works for me with 1 alteration:

picturebox1.Image = copy;
Before disposing of img.

Otherwise you are disposing of the pictureboxes active image.
 
I think Im going crazy here the hole code plz try it that would be nice

C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Net;
using System.Drawing.Imaging;

namespace ActiveXNet
{
    public partial class ucMain : UserControl
    {

        public ucMain()
        {
            InitializeComponent();
        }


        private void ucMain_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

            openFileDialog.Multiselect = true;
            openFileDialog.Filter = "Alle | *.AVI;*.MPG;*.MPEG;*.WMV;*.MOV;*.QT;*.JPG;*.BMP;*.swf |Videos | *.AVI;*.MPG;*.MPEG;*.WMV;*.MOV;*.QT |Bilder | *.JPG;*.BMP |Flash | *.swf";
            openFileDialog.ShowDialog(this);

            if (openFileDialog.FileNames.Length > 0)
            {
                for (int i = 0; i != openFileDialog.FileNames.Length; i++)
                {

                    string filename = System.IO.Path.GetFileName(openFileDialog.FileNames[i]);

                    listBox1.Items.Add(filename);
                    string filepath = System.IO.Path.GetFullPath(openFileDialog.FileNames[i]);
                    string[] path = { filepath };
                    foreach (string paths in path)
                    {
                        listBox2.Items.Add(paths);
                    }

                    //MessageBox.Show("File Location: " + openFileDialog.FileNames[i]);
                }

            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex != -1)
            {
                this.pictureBox1.Visible = false;
                this.axWindowsMediaPlayer1.Visible = false;
                this.axWindowsMediaPlayer1.Ctlcontrols.stop();

                listBox2.SetSelected(listBox1.SelectedIndex, true);

                string extension = System.IO.Path.GetExtension(listBox2.Text);

                if (extension.ToLower() == ".jpg")
                {
                    this.pictureBox1.ImageLocation = listBox2.Text;
                    this.pictureBox1.Visible = true;
                }
                else if (extension.ToLower() == ".bmp")
                {
                    this.pictureBox1.ImageLocation = listBox2.Text;
                    this.pictureBox1.Visible = true;
                }
                else if (extension.ToLower() == ".swf")
                {
                    this.pictureBox1.ImageLocation = listBox2.Text;
                    this.pictureBox1.Visible = true;
                }
                else if (extension.ToLower() == ".wmv")
                {
                    this.axWindowsMediaPlayer1.URL = listBox2.Text;
                    this.axWindowsMediaPlayer1.Visible = true;
                }
                else if (extension.ToLower() == ".mpg")
                {
                    this.axWindowsMediaPlayer1.URL = listBox2.Text;
                    this.axWindowsMediaPlayer1.Visible = true;
                }
                else if (extension.ToLower() == ".mpeg")
                {
                    this.axWindowsMediaPlayer1.URL = listBox2.Text;
                    this.axWindowsMediaPlayer1.Visible = true;
                }
                else if (extension.ToLower() == ".avi")
                {
                    this.axWindowsMediaPlayer1.URL = listBox2.Text;
                    this.axWindowsMediaPlayer1.Visible = true;
                }
                else if (extension.ToLower() == ".mpg")
                {
                    this.axWindowsMediaPlayer1.URL = listBox2.Text;
                    this.axWindowsMediaPlayer1.Visible = true;
                }
                else if (extension.ToLower() == ".mov")
                {
                    this.axWindowsMediaPlayer1.URL = listBox2.Text;
                    this.axWindowsMediaPlayer1.Visible = true;
                }
                else if (extension.ToLower() == ".qt")
                {
                    this.axWindowsMediaPlayer1.URL = listBox2.Text;
                    this.axWindowsMediaPlayer1.Visible = true;
                }
            }
        }

        private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void Upload_Click(object sender, EventArgs e)
        {
            foreach (string pathname in listBox2.Items)
            {
                MessageBox.Show("File Location: " + pathname);
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {

            if (listBox1.SelectedIndex >= 0)
            {
                listBox2.Items.RemoveAt(listBox1.SelectedIndex);
                listBox1.Items.Remove(listBox1.SelectedItem);
                if (listBox1.Items.Count > 0)
                {
                    listBox1.SelectedIndex = 0;
                }
            }
        }

        private void axWindowsMediaPlayer1_Enter(object sender, EventArgs e)
        {

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void progressBar1_Click(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            if(this.pictureBox1.Visible == true)
            {
                Image img = this.pictureBox1.Image;

                Bitmap copy = new Bitmap(img.Width,img.Height);

                ImageAttributes ia = new ImageAttributes();


                ColorMatrix cm = new ColorMatrix();
                cm.Matrix00 = cm.Matrix11 = cm.Matrix22 = 0.99f;
                cm.Matrix33 = cm.Matrix44 = 1;
                cm.Matrix40 = cm.Matrix41 = cm.Matrix42 = .04f;

                                
                ia.SetColorMatrix(cm);

                
                Graphics g = Graphics.FromImage(copy);
                
                g.DrawImage(img, new Rectangle(0, 0, img.Width, img.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia);


                
                g.Dispose();
                
                img.Dispose();
            }
            
        }
    }
}
 
img = this.pictureBox1.Image;

Here img is now a reference to the bitmap in the picturebox.

img.Dispose();

this disposes of the bitmap that img refers to. So pictureBox1.Image is now disposed. When windows tries to paint pictureBox1, it throws an error.

Also you draw a modified version of img into the bitmap called copy. But do nothing with it...

So what you probably want to do is:

g.Dispose();
picturebox1.image = copy;
img.dispose();

Now when windows paints, it will draw the bitmap copy, which still exists.
 
Back
Top