Camera will capture image and store in a folder(.bmp file).Simultaneously our application needs to get one by one image file from the folder for processing.(captured images will update the folder dynamically.If we use FileSystemWatcher , it may cause problem for us.(We are selecting the directory before processing of the image,so updated images will not process.Is there any solution for this problem)how to get one by one image from the folder at run time?
One solution would be to use a FileSystemWatcher to get notified when a new file is put into the folder. Then, process the file(s).
To get the file names of all BMP files in a folder you can use:
string[] fileNames = Directory.GetFiles("<path>", "*.bmp");
Then you can load the bitmaps normally.
You can use FileSystemWatcher but you need to be aware of a few quirks. The watcher will raise a Created event when a file is first created and one or more Modified events as data is written to the files. There is no Close event so you can't know when the camera has stopped writing to the file.
As long as the file is open for writing, you won't be able to read it for processing and any attempt to read it will raise an exception
There are several ways you can handle this:
You can log all events in a list and periodically process all files in the list. If you fail to open a file, just leave it in the list to process in the next processing round.
You can delay reading the file hoping that the camera will finish writing by the time you try to read the file. If you still receive an exception, wait for a while and then retry.
You can keep track of the Created and Modified events eg. in a dictionary keyed by the file name and try to open the file only if no event is received for a file after a specific timeout.
Polling and timeouts can be implemented using timers.
If you don't like the delay introduced by polling, you can use a separate thread to wait on the list of events and process new events as they appear. You can use the functionality of the BlockingCollection in C# 4 to do this in a relatively simple way.
Use FileSystemWatcher http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx
Any image development will require to know how to load an image in C#.
FromFile
There are two direct ways to read an image file and load it into either a Bitmap or Image. Behold the C# code:
Image myImg = Image.FromFile("path here");
Bitmap myBmp = Bitmap.FromFile("path here");
Alternatively a Bitmap object can also be loaded with:
Bitmap myBmp = new Bitmap("path here");
The code above does not work with Image objects though, so it is best to stick with FromFile.
Dialog Box
Finally, to write an application that loads an image from a file, your C# program needs a dialogbox to select files. Using the .Net OpenFileDialog is simple enough. Just apply the image-loading code to the Filename chosen by the user, so for example:
Bitmap loadedBitmap = Bitmap.FromFile(openFileDialog1.Filename);
Of course, you don't have to necessarily load an image from a file in this manner, but it is a useful thing to know.
For more information HERE
Related
I have a mix python-C# code that scans list of directories and manipulate it files in a loop.
Sometime there is a download directly to the income directory and the program start manipulating the file before the download completed.
Is there any way to detect if the file finish downloading?
A simple way to detect if the file is done downloading is to compare file size. If you always keep a previous "snapshot" of the files in the current directory you will be able to see which files exist and which don't at a given moment in time. Once you see an new file you know that the file has started to download. From this point you can compare the file size of that file and once the previous file size is equal to the current file size you know the file has finished downloading. Each time you would take a new "snapshot" it would be, for example 1ms after the previous. This may not be simple to implement depending on your knowledge of python or C# but I think this algorithm would get you what you want.
When you download, you get file size. You can check file size before writing to file. If file size is same download size then allow writing.
I am describing my work process bellow:
I get image files from a directory.
Creating a PictureBox array for displaying the images.
Creating a Image array from the files that I got from the directory. I am creating this array for making the image source of PictureBox.
I am copying the files to another directory. By this:
File.Copy(allFiles[i],fullPath+fullName+"-AA"+nameString+ext);
Now I want to delete the files from the directory. For this I am doing this:
File.Delete(allFiles[i]);
But its giving me this error:
The process cannot access the file 'C:\G\a.jpg' because it is being used by another process.
Please tell me how to resolve this? I didn't attach the full code here because it'll be large. Please ask me if you want to see any part of my code.
Chances are you are loading the image directly from the file. For example,
PictureBox[i] = Image.FromFile(allFiles[i]);
If you look up the documentation for the Image.FromFile method, you will find that it actually locks the file until the Image is disposed. (In fact, most other loading methods in the Image class also locks the file until the Image is disposed.)
So to work around this problem, copy the picture file contents to memory and load it from there. For example,
PictureBox[i] = Image.FromStream(new MemoryStream(File.ReadAllBytes(allFiles[i])));
That way, the file itself will remain unlocked and you can freely move/delete it.
Of course it's being used.
Copy the files to another directory first (Step 4),
Delete the old directory
then do the third step (assigning it to a array and loading it to PictureBox) from the newly copied directory.
Alternative:
You must close the stream handler before deleting the files..
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
{
using (PictureBox[i] = Image.FromStream(fs))
{
...
}
This one will be interesting...
I have seen many asp.net thumbnail generation tutorials / sample code, but no one has considered the problem of concurrency access when generating thumbnail image dynamically, when one or more user access the same page when the thumbnail needs to be generated.
A simple case, i have a site with property images (houses etc.), images are stored in a folder, the thumbnails are generated (for gallery) when someone first time accesses particular offer, then a handler makes the thumbnails from original larger images, the handler generate each thumbnail only once and then use the generated image in further requests.
What happens if two users access this page in the same time, the handler could run twice on the same file or more, there could be concurrency problem, file opening errors and so on (file needs to be opened for thumbnail generation).
Normally one user gets the thumbnail and other get a blank box without image till they refresh the page (since the first user triggered the thumbnail creation)
So the question is, how to avoid this situations ?
Normally if you are only opening the original image file for reading in order to generate the thumbnail there is no problem accesing it concurrently. Multiple users can open the same file for reading at the same time. Problems arise if you start writing at the same time.
I need to create a proxy-like solution that "forwards" an uploaded file to somewhere else. The problem I am facing is that with large uploads the file needs to be fully uploaded before my forwarding code gets control of it in Page_Loaded of an ASP.NET application.
Is there a way to get control of a Stream of a file being uploaded as soon as the upload begins? The idea is to start forwarding as soon as first file bytes start coming in without waiting for the entire upload to finish.
Can IHttpModule be used to achieve this goal?
Some time ago, I had to deal with images and some heavy files upload. We used this project:
http://silverlightfileupld.codeplex.com/
It is basically a silverlight file uploader, we added some feautures like image compression. Also the key part, is that we used this uploader to send chunks of the file instead of the whole file, so the new file was recreated as a temp file and then renamed. Maybe you can follow that approach and in the ashx file, you can transfer the bytes to the real server.
Hope this helps
I have a list box of images which I am adding programatically from my my pictures directory. I want to be able to put images in the directory and the UI displays it automatically. I also want to be able to remove photos from the directory and have the UI update as well but the problem is that the images are being used by the application so I am not able to remove the images from the directory. Is there a way to unlock the files so I can rename and remove files from the directory?
Are you using data binding like Image Source = {Binding FilePath} ? If so, you need to load the image with CacheOption = BitmapCacheOption.OnLoad, you can do it declaratively with a ValueConverter, like this one:
http://soulsolutions.com.au/wpf-reference-an-image-without-a-file-lock/
Do you load images with Image.FromFile? I've noticed this behavior a long time ago. Try opening the files with File.Open, loading the images with Image.FromStream, then close the stream.