CopyPastor

Detecting plagiarism made easy.

Score: 0.7535341382026672; Reported for: String similarity Open both answers

Possible Plagiarism

Plagiarized on 2016-07-21
by Christlin Panneer

Original Post

Original - Posted on 2016-07-21
by lokusking



            
Present in both answers; Present only in the new answer; Present only in the old answer;

For the first requirement: "I only want the program to process image files"
private static void fileSystemWatcher1_Changed(object sender, FileSystemEventArgs e) { string strFileExt = getFileExt(e.FullPath); // filter file types if (Regex.IsMatch(strFileExt, @"\.png|\.jpg", RegexOptions.IgnoreCase)) { //here Process the image file } }
For the second requirement: "Out of Memory Exception"
Here what happens is, when the file is created (only file name and some attributes) the system is calling the created event. Then the file changed event is also called
So you have to do the processing in the changed event. Also to prevent duplicate calling you have to add a filter to your watcher.
**The following is the complete code.**
private void fileSystemWatcher1_Changed(object sender, System.IO.FileSystemEventArgs e) { FileInfo fileInfo = new FileInfo(e.FullPath); string strFileExt = fileInfo.Extension; // filter file types if (Regex.IsMatch(strFileExt, @"\.png|\.jpg", RegexOptions.IgnoreCase)) { //here Process the image file try { using (Bitmap test = new Bitmap(Bitmap.FromFile(e.FullPath))) { //Do your code here. } } catch (Exception error) { Console.WriteLine("File Error"); } } } private void Form1_Load(object sender, EventArgs e) { fileSystemWatcher1.Path = @"C:\Users\Christlin\Desktop\res"; //To Prevent duplicated calling of changed event fileSystemWatcher1.NotifyFilter = NotifyFilters.FileName | NotifyFilters.Size; }



A bit cleaner solution than a Try-Catch might be this one. Im using this code without any exceptions raised.
private static bool IsImage(string path) { try { var result = false; using (var stream = new FileStream(path, FileMode.Open)) { stream.Seek(0, SeekOrigin.Begin); var jpg = new List<string> { "FF", "D8" }; var bmp = new List<string> { "42", "4D" }; var gif = new List<string> { "47", "49", "46" }; var png = new List<string> { "89", "50", "4E", "47", "0D", "0A", "1A", "0A" }; var imgTypes = new List<List<string>> { jpg, bmp, gif, png }; var bytesIterated = new List<string>(); for (var i = 0; i < 8; i++) { var bit = stream.ReadByte().ToString("X2"); bytesIterated.Add(bit); var isImage = imgTypes.Any(img => !img.Except(bytesIterated).Any()); if (isImage) { result = true; break; } } } return result; } catch (UnauthorizedAccessException) { return false; } }
**Usage of code**
foreach (var file in Directory.EnumerateFiles(@"pathToFlowersFolder")) { Console.WriteLine($"File: {file} Result:{IsImage(file)}"); }
**Edit**
After playing around i got an IO-Exception (File already in use)<br> After reading [this](https://stackoverflow.com/questions/3577889/system-ioexception-when-opening-file-with-file-openread) i'd offer you the following solution:
private void button1_Click(object sender, EventArgs e) { var watcher = new FileSystemWatcher(); watcher.Created += new FileSystemEventHandler(fileSystemWatcher1_Changed); watcher.Path = @"c:\temp"; watcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.Size; watcher.EnableRaisingEvents = true; } private void fileSystemWatcher1_Changed(object sender, System.IO.FileSystemEventArgs e) { Thread.Sleep(100); // <- give the Creator some time. Increase value for greate pause if (IsImage(e.FullPath)) { Console.WriteLine("success----------->" + e.FullPath); } }
**Note**
This piece of code properly works on my machine. My HDD is an SSD, so you might need to increase the thread-sleeping time. It properly works for all images (jpg, bmp, gif, png) up to a size of 7 Mb (im quite sure and greater).
If this code doesnt works for you, please post the exception rather than uploading your code.

        
Present in both answers; Present only in the new answer; Present only in the old answer;