Displaying images in a list box keeps files locked - c#

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.

Related

How to move file loaded into windows forms imagebox to another folder?

I am working on a program in C# and Windows Forms that will display an image in .tiff format in an imagebox and the user will decide with one of the 3 buttons to which folder to move the displayed image.
The problem is that I can't find a method that will move the loaded file to another folder with the button. Does anyone know the solution to this problem?
And my second, optional question, is it possible that after moving the .tiff file to another folder, another one will be automatically loaded into the imagebox? All .tiff will be in the same folder.
Here is all that I already have GITHUB LINK

C#, How to Traverse all drive and folder to copy image and paste them in a specific folder?

my project was a image gallery using asp.net.
Initially when then the project load first time i need to copy all the images in the computer and paste them in a folder named Data.
here is my project link:
https://www.dropbox.com/s/sr3qhaamiiwk0di/Gallary_temp.zip?dl=0
https://drive.google.com/file/d/0BynGaI0gi3mrOEtweWlWOEdmSFE/view?usp=sharing
Of course, code must be in c#
Instead of copying the image from hard drive to your location every time, it is better you save the file location in DB and call them when ever you require.

Delete an Image in the MediaLibrary

I was wondering if it is possible to programmatically delete an image within the MediaLibrary. Currently I am using the CameraCaptureTask to capture an image and save, but I would like to be able to delete that image as well. Also, editing an image would be another consideration. Note, I am working with Windows Phone 8.
No, you can't programmatically delete images from the users Pictures MediaLibary.
"There is no way to delete any content from the user's library. Once you've saved a picture, the user will always have that picture unless they personally go delete it."
(Source Delete image from MediaLibrary.SavePictures).
If you need the ability to delete stored images you could store them in isolated storage and then have an option to publish them to the Media Library.

Processing bitmap files dynamically from folder using c#

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

Automatically add and delete images in WP7 app storage

I want to make a "recent pages" section in my WP7 app which will show thumbnails of 6 recent browsed pages. How to make a method which saves only 6 image files in the storage and when new ones come replace old ones with it?
Assuming that you define "new" based on the date/time that the image file in IsolatedStorage was created you could determine this by querying GetCreationTime on the file.
You can use IsolatedStorageFile.GetFileNames to determine how many / which files exist. Note: you probably want to create these files in a specific folder so you don't have to worry about other files in IsolatedStorage.

Categories

Resources