<commit_before>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace PictureSorter
{

    public class saveDirectoryButton : System.Windows.Forms.Button
    {
        public int ID;
        public string folderPath;

        private void InitializeComponent()
        {
            this.SuspendLayout();
            this.ResumeLayout(false);

        }
    }

    public partial class Form1 : Form
    {
        List<string> pictureList;
        Dictionary<int, saveDirectoryButton> saveDirectoryButtons;
        int activeIndex;

        static List<string> ImageFormats = new List<string>
        {
            ".jpeg",
            ".jpg",
            ".png",
            ".gif",
            ".bmp",
            ".tiff",
        };

        public Form1()
        {
            InitializeComponent();          
        }

        private bool CheckValidImage(string filePath)
        {
            //this currently causes the file to lock, preventing us from deleting it later
            try
            {
                Image image = Image.FromFile(filePath);
            }
            catch (OutOfMemoryException ex)
            {
                return false;
            }
            return true;
        }

        private bool CheckValid(string filePath)
        {
            return File.Exists(filePath); //&& CheckValidImage(filePath);
        }

        private void SetActivePicture(List<string> pictureList, int index)
        {
            bool zoomed = false;
            activePictureBox.Image = null;
            //activePictureBox.Dispose();
            Image image;
            using (FileStream fs = new FileStream(pictureList[index], FileMode.Open, FileAccess.ReadWrite, FileShare.Delete))
            {
                image = Image.FromStream(fs);                
            }
            //Image image = Image.FromFile(pictureList[index]);
            activeIndex = index;
            activePictureBox.Image = image;
            if (activePictureBox.Size.Height < image.Height || activePictureBox.Size.Width < image.Width)
            {
                activePictureBox.SizeMode = PictureBoxSizeMode.Zoom;
                zoomed = true;
            }
            else
            {
                activePictureBox.SizeMode = PictureBoxSizeMode.CenterImage;
            }
            tooltipStrip.Text = "File: " + Path.GetFileName(pictureList[index]) +
                "   Height: " + image.Height + "   Width: " + image.Width;
            if (zoomed)<commit_msg>Changed the CheckValidImage to use a file stream too, seems to ensure it does not lock<commit_after>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace PictureSorter
{

    public class saveDirectoryButton : System.Windows.Forms.Button
    {
        public int ID;
        public string folderPath;

        private void InitializeComponent()
        {
            this.SuspendLayout();
            this.ResumeLayout(false);

        }
    }

    public partial class Form1 : Form
    {
        List<string> pictureList;
        Dictionary<int, saveDirectoryButton> saveDirectoryButtons;
        int activeIndex;

        static List<string> ImageFormats = new List<string>
        {
            ".jpeg",
            ".jpg",
            ".png",
            ".gif",
            ".bmp",
            ".tiff",
        };

        public Form1()
        {
            InitializeComponent();
        }

        private bool CheckValidImage(string filePath)
        {            
            try
            {
                //this caused the file to lock
                //Image image = Image.FromFile(filePath);
                Image image;
                using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    image = Image.FromStream(fs);
                }
            }
            catch (ArgumentException ex)
            {
                return false;
            }
            //catch (OutOfMemoryException ex)
            //{
            //    return false;
            //}
            return true;
        }

        private bool CheckValid(string filePath)
        {
            return File.Exists(filePath) && CheckValidImage(filePath);
        }

        private void SetActivePicture(List<string> pictureList, int index)
        {
            bool zoomed = false;
            activePictureBox.Image = null;
            //activePictureBox.Dispose();
            Image image;
            using (FileStream fs = new FileStream(pictureList[index], FileMode.Open, FileAccess.ReadWrite, FileShare.Delete))
            {
                image = Image.FromStream(fs);                
            }
            //Image image = Image.FromFile(pictureList[index]);
            activeIndex = index;
            activePictureBox.Image = image;
            if (activePictureBox.Size.Height < image.Height || activePictureBox.Size.Width < image.Width)
            {
                activePictureBox.SizeMode = PictureBoxSizeMode.Zoom;
                zoomed = true;
            }
            else
            {
                activePictureBox.SizeMode = PictureBoxSizeMode.CenterImage;
            }
            tooltipStrip.Text = "File: " + Path.GetFileName(pictureList[index]) +
                "   Height: " + image.Height + "   Width: " + image.Width;
            if (zoomed)
{'fraction_non_alphanumeric': 0.08734465254682304, 'fraction_numerical': 0.0007001575354454753, 'mean_word_length': 1.5283185840707965, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2187851', 'n_tokens_mistral': 1445, 'n_tokens_neox': 1393, 'n_words': 323}