0

Hi I have an application in which I have to save images from three different IP Cameras whenever a button is pressed.

I am using a class that has all the members that I need to save the images from the IP camera namely the BitmapImage and the DateTime of when the photo was saved.

I have the following problem. I need to save a certain amount of photos of each camera every couple hundred milliseconds. And I am currently testing it by saving 50 photos of each camera every 200ms to a ConcurrentQueue and then the items gets saved from the ConcurrentQueue to file. After I have taken about 110 photos altogether of all three cameras then it just saves blank images.

I think my problem is that the program memory is too full, so I need to clear an item from the memory when ever I save the item with the TryDequeue() method of the ConcurrentQueue.

Can anyone please advise me or give me maybe some links that can help me to save this problem so that I can save as many photos as I want to of each camera and that it will not run out of memory after a certain amount photos?

A button is pressed and then it goes into a for loop where it calls the following method.

private void EnqueuePhotos1()
    {
        IPCamera1 ipCam1Enqueue = new IPCamera1();
        BitmapImage cam1Image = new BitmapImage();
        cam1Image.BeginInit();
        cam1Image.CacheOption = BitmapCacheOption.OnLoad;
        cam1Image.CreateOptions = BitmapCreateOptions.IgnoreImageCache;
        cam1Image.UriSource = null;
        cam1Image.UriSource = new Uri("http://" + ipCam1IP + "/image?res=full&x0=0&y0=0&x1=1600&y1=1200&quality=21&doublescan=0", UriKind.Absolute);
        while (cam1Image.IsDownloading) { ; }
        cam1Image.EndInit();
        ipCam1Enqueue.IPCamImage = cam1Image;
        ipCam1Enqueue.TimeTook = DateTime.Now;
        ipCam1ConQ.Enqueue(ipCam1Enqueue);
    }

for a certain amount of times depending on how many photos the user wants to take.

Just before the for loop I start my timer to check every 100ms if there is something on the ConcurrentQueue and then if something is found it calls the following function.

private void GetPhotos1()
    {
        IPCamera1 ipCam1Dequeue = new IPCamera1();
        while (ipCam1ConQ.TryDequeue(out ipCam1Dequeue))
        {
            cam1Photos++;
            cam1ImgLoc = cam1Location + "\\Image " + cam1Photos + ".jpg";
            FileStream cam1Stream = new FileStream(cam1ImgLoc, FileMode.Create);
            JpegBitmapEncoder cam1Encoder = new JpegBitmapEncoder();
            cam1Encoder.Frames.Add(BitmapFrame.Create(ipCam1Dequeue.IPCamImage));
            cam1Encoder.Save(cam1Stream);
            cam1Stream.Dispose();
        }
    }
4
  • 1
    "I think my problem is that the program memory is too full" => why?
    – Jon
    Commented Jun 5, 2013 at 8:21
  • you may also check if you don't have a problem of Large Object Heap fragmentation, that may be the case if keeping the photos on memory. simple-talk.com/dotnet/.net-framework/…
    – polkduran
    Commented Jun 5, 2013 at 8:38
  • 1
    How should be someone able to answer your question without knowing how your application is working (lifecycle management)? Generally, you could profile the application to get an idea, where the potential memory leak is, although I'm not sure that there is one. .NET Memory Profiler is IMHO a good tool for this job.
    – DHN
    Commented Jun 5, 2013 at 8:49
  • Just a small note: Why are you allocating a new IPCamera1 in the GetPhotos1, if you propably will directly overwrite it in the TryDequeue method? Also i'm not sure if IsDownloading does what you think it does especially considering you are using OnLoad. And finally: Remember that BitmapImage is mostly for xaml, so maybe WriteableBitmap fits your need a bit better.
    – dowhilefor
    Commented Jun 5, 2013 at 10:04

1 Answer 1

1
using (FileStream cam1Stream = new FileStream(cam1ImgLoc, FileMode.Create))
{
    // do stuff...
}

Resources defined in a way like this are automagically disposed after the statements in the using statement are executed.

0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.