0

In the paint event i draw a rectangle and then adding the pixels coordinates inside the rectangle area to a List:

private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (cloudPoints != null)
            {
                if (DrawIt)
                {
                    e.Graphics.DrawRectangle(pen, rect);
                    pointsAffected = cloudPoints.Where(pt => rect.Contains(pt));

                    CloudEnteringAlert.pointtocolorinrectangle = pointsAffected.ToList();
                    Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height, PixelFormat.Format32bppArgb);
                    CloudEnteringAlert.Paint(e.Graphics, 1, 200, bmp);
                }
            }   
        }

I'm drawing a rectangle thats the rect(Rectangle) variable and assigning to pointsAffected List only the pixels coordinates that are inside the rect area ! cloudPoints contain all the pixels coordinates all over the image !!! but pointsAffected contain only the coordinates of pixels inside the rectangle area.

Then the mouse up event:

private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            StreamWriter w = new StreamWriter(@"c:\diff\diff.txt");
            pixelscoordinatesinrectangle = new List<Point>();
            pixelscoordinatesinrectangle = pointsAffected.ToList();
            DrawIt = false;
            for (int i = 0; i < trackBar1FileInfo.Length; i++)
            {
                DrawIt = true;
                trackBar1.Value = i;
                LoadPictureAt(trackBar1.Value, sender);
                pictureBox1.Load(trackBar1FileInfo[i].FullName);
                ConvertedBmp = ConvertTo24(trackBar1FileInfo[trackBar1.Value].FullName);
                ConvertedBmp.Save(ConvertedBmpDir + "\\ConvertedBmp.bmp");
                mymem = ToStream(ConvertedBmp, ImageFormat.Bmp);
                backTexture = TextureLoader.FromStream(D3Ddev, mymem);
                scannedCloudsTexture = new Texture(D3Ddev, 512, 512, 1, Usage.Dynamic, Format.A8R8G8B8, Pool.Default);
                Button1Code();
                pictureBox1.Refresh();
                newpixelscoordinates = new List<Point>();
                newpixelscoordinates = pointsAffected.ToList();
                if (pixelscoordinatesinrectangle != null && newpixelscoordinates != null)
                {
                    IEnumerable<Point> differenceQuery =
                    pixelscoordinatesinrectangle.Except(newpixelscoordinates);

                    // Execute the query.
                    foreach (Point s in differenceQuery)
                        w.WriteLine("The following points are not the same" + s);
                }
                else
                {
                    am1 = pixelscoordinatesinrectangle.Count;
                    am2 = newpixelscoordinates.Count;
                }
                //MessageBox.Show(pixelscoordinatesinrectangle.Count.ToString());
            }
            w.Close();
        }

Once i draw the rectangle when finish drawing it i'm creating a new List instance and store the pixels in the rectangle area in the pixelscoordinatesinrectangle List.

Then i loop over trackBar1FileInfo that contains for example 5000 images files names from the hard disk.

This is the problem part:

pictureBox1.Refresh();
                newpixelscoordinates = new List<Point>();
                newpixelscoordinates = pointsAffected.ToList();
                if (pixelscoordinatesinrectangle != null && newpixelscoordinates != null)
                {
                    IEnumerable<Point> differenceQuery =
                    pixelscoordinatesinrectangle.Except(newpixelscoordinates);

                    // Execute the query.
                    foreach (Point s in differenceQuery)
                        w.WriteLine("The following points are not the same" + s);
                }
                else
                {
                    am1 = pixelscoordinatesinrectangle.Count;
                    am2 = newpixelscoordinates.Count;
                }

I'm doing refresh for the pictureBox1 so it will go one each image to the paint event and will create a new List pointsAffected will have each time a different pixels coordinates.

So newpixelscoordinates should be with new pixels coordinates each loop itertion.

Then i'm comparing both Lists newpixelscoordinates and pixelscoordinatesinrectangle for a different items.

And write those who are not the same to a text file.

So i'm getting a very large text file with many pixels coordinates that are not the same in both Lists.

The problems are:

  1. Does the comparison i'm doing is right ? I want to compare one list index against other listi ndex.

    For example in the List newpixelscoordinates in index 0 if i have x = 233 y = 23 and in the List pixelscoordinatesinrectangle in index 0 there is x = 1 y = 100 then write this as not the same to the text file.

    What i want to do is to check the whole List items against the other List items and if some of the items are not the same write this items to the text file.

Next itertion new image new Lists with new pixels coordinates do the same comparison.

The List pixelscoordinatesinrectangle is not changing it's storing the first pixels coordinates when i drawed the rectangle first time. Only the List newpixelscoordinates change each time, Should change each itertion.

  1. The exception i'm getting is on the line:

    newpixelscoordinates = new List();

I added this line since i thought maybe i didn't clear it each time but it didn't help.

Before adding this line the exception was on the line:

newpixelscoordinates = pointsAffected.ToList();

The exception is: OutOfMemoryException: Out of memory

System.OutOfMemoryException was unhandled
  HResult=-2147024882
  Message=Out of memory.
  Source=System.Drawing
  StackTrace:
       at System.Drawing.Graphics.CheckErrorStatus(Int32 status)
       at System.Drawing.Graphics.DrawImage(Image image, Int32 x, Int32 y, Int32 width, Int32 height)
       at System.Drawing.Graphics.DrawImage(Image image, Rectangle rect)
       at System.Windows.Forms.PictureBox.OnPaint(PaintEventArgs pe)
       at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
       at System.Windows.Forms.Control.WmPaint(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(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)
  InnerException: 
3
  • 2
    (sorry, can't resist,) I suspect you've run out of memory.
    – Jodrell
    Commented Jan 29, 2015 at 17:45
  • 1
    Double check all of your bounds. You can get OOM exceptions by having an invalid rectangle/bounds. Like having width/height <= 0. You also don't seem to be disposing of much as @Matt just pointed out in the answer. Don't dispose Graphics though unless you want flickering or nothing to draw.
    – TyCobb
    Commented Jan 29, 2015 at 17:50
  • Indeed....in C#/VB.net, a lot of resource management is taken care of by the framework by default, however there's still a good number of unmanaged resources (particularly when you're dealing with drawing stuff out to the screen). As @TyCobb said, make sure you're done with you're completely done with your objects before you dispose of them, but DO dispose of them, else you'll run into exactly what you've bumped into. On your MouseUp event for example, I'd look into disposing a bunch of stuff at the same point you call w.Close(); Commented Jan 29, 2015 at 17:54

1 Answer 1

1

in your code, some objects have to be disposed to free the memory, like the Bitmap object in function pictureBox1_Paint, StreamWriter object in pictureBox1_MouseUp. Probably there're more.

For managed object which implement the IDisposable interface , you have to dispose them to free the umanaged memory in time. The GC isspecifically not designed to manage unmanaged resources, the usage of unmanage memory won't increase the GC pressure and won't trigger GC to act, that's why you got out of memory exception.

Compile your programing with code analysis, it will tell which objects neeed to be disposed in the warnings. Put them in the using block.

1
  • 2
    Also, you can use 'using' statements to automatically dispose these IDisposable objects when you're done with them.
    – ryanyuyu
    Commented Jan 29, 2015 at 17:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.