0

I'm trying to create a basic image conversion tool, which converts images to a .jpeg format, and then further down the line to a Base64 value for some SQL inserts.

I'm currently getting the error in the title when running the code.

The below is a little messy, as I've been trying to fix this for a while and this section has gone through many iterations. I've looked at similar queries, but none have seemed to fit the scenario or fixed the issue.

My experience in C# is limited, so apologies if I have follow-up questions regarding the implementation of a fix if one comes about.

        private void imageLocationBtn_Click(object sender, EventArgs e)
        {
            byte i = 0;
            //Open folder browser
            FolderBrowserDialog openFileDialog1 = new FolderBrowserDialog();
            //Take image names and put them into the list box
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string strFilename = openFileDialog1.SelectedPath;

                imageLocationTxtBx.Text = strFilename;

                //Display File Names found
                string root = strFilename;
                string[] fileEntries = Directory.GetFiles(root);
                foreach (string fileNameRoot in fileEntries)
                {
                    string fileName = fileNameRoot.Substring((fileNameRoot.LastIndexOf('\\') + 2), ((fileNameRoot.LastIndexOf('.')) - (fileNameRoot.LastIndexOf('\\') + 2)));
                    Bitmap bmp1 = new Bitmap(Image.FromFile(fileNameRoot));

                    File.Delete(fileNameRoot);
                    bmp1.Save("C:\\Users\\HobbitForHire\\Pictures\\Conversion Images\\" + fileName + ".Jpeg", ImageFormat.Jpeg);
                }
  • 2
    You can save binary data in a database in VARBINARY(MAX) column (SQL Server) to avoid the extra space taken up by base64 encoding. – Andrew Morton Jan 13 '20 at 10:02
  • 2
    To add to the duplicate: call `bmp1.Save`, and then dispose it, *before* calling `File.Delete`. E.g. `using (Bitmap bmp1 = ...) { bmp1.Save(...); } File.Delete(...)` – canton7 Jan 13 '20 at 10:03
  • Have you got the file open with another program? – ds4940 Jan 13 '20 at 10:04

0 Answers0