Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

How to use BlockingCollection for image caching

$
0
0

I'm hoping someone may please be able to assist me with a programming question. I currently have an application that acquires a bitmap image of an item. An image is acquired from a camera about every 5 seconds. I need to store (cache in memory) the most recent 4 images.  I was thinking of creating a collection for storing these bitmaps. When I add the bitmap I will also add the unique "key" identifying the image. I'd like things to run asynchronously and was thinking of using a BlockingCollection such that I can add/remove things from the collection (via a producer-consumer method. However, I have no experience with this and no idea how to get started.

After these images are taken and cached into my collection, another image is taken of the same item with a different camera. An inspection is performed on this second image. If it is determined a "fail", I need to save this second image out to disk AND save the previous image (from the first camera) which I need to retrieve from the collection by the unique ID. This is why I need to cache them and be able to retrieve them.

NOTE: The cameras are NOT next to one another such that the second image is acquired immediately after the first. Up to 2 or 3 items may have gone past the first camera before they reach the second camera.

My idea is to

1). In my GUI thread I want to create an instance of a collection class to store the bitmap images and along with their unique keys. When an item is added to the collection the add method will determine the collection count and automatically remove the oldest one.

2). At certain instances (a "fail" at the second camera as described above) I will need to check/grab an image from the collection. I would like to use the TryTake method so that it will obtain the bitmap (based on the key that I provide) from the collection. I don't want the grabbing of the image from the collection to interfere with the possibly of adding one at the exact same moment.

3). Lastly, once I've obtained the image from the collection, I need to save that image out to disk along with the second image. I would like the save process to run on its own thread as well so that it doesn't impact performance or the ability of the app to need to work with the collection during a save process.

I'm wondering if using a BlockingCollection is the correct approach or if anyone else has any other suggestions? I really don't know how to get started on this and only have a small test that allows me to load bitmaps and place them into properties of a class (which I called cImageCache) to add the key (String) and bitmap (bitmap object).  I am now trying to make a blockingcollection of this cImageCache.  Any coding examples/help in either C# or VB.NET would be greatly appreciated! Thank you very much for your time.

Kindest Regards


Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>