You have some strange stuff going on in your code.
For example these lines:
picturebox1.Image = new Bitmap(Image.FromFile(filepath));
Bitmap image1 = new Bitmap(picturebox1.Image);
image1.Save(Application.StartupPath + "/CanFiles/" + fileName + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
should be
Bitmap image1 = new Bitmap(Image.FromFile(filepath));
Bitmap image1 = Image.FromFile(filepath);
image1.Save(Path.Combine(Application.StartupPath, "CanFiles", fileName, ".jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);
image1.Dispose();
I see no reason to to use the PictureBox in a loop. The image will just flicker.
Also it would be easier to read and more efficient doing it this way:
string extension = Path.GetExtension(file).ToUpper();
if (extension == ".PNG" || extension == ".TIFF" ||
extension == ".JPG" || extension == ".TIF" ||
extension == ".GIF" || extension == ".JFIF" ||
extension == ".JPE" || extension == ".DIB" ||
extension == ".BMP" || extension == ".JPEG")
[UPDATE]
Change this line
Bitmap image1 = new Bitmap(Image.FromFile(filepath));
to
Bitmap image1 = (Bitmap)Image.FromFile(filepath);
Otherwise you create two images and release only one.
(I missed this little "special" construct at first)
(Added typecast for Bitmap)
[UPDATE2]
Your code should look something like this now. Does it?
private void Select_Path_Click(object sender, EventArgs e)
{
string directoryPath = Path.Combine(Application.StartupPath, "CanFiles");
if (!Directory.Exists(directoryPath))
Directory.CreateDirectory(directoryPath);
foreach (string file in Directory.GetFiles(folderBrowserDialog1.SelectedPath))
{
string extension = Path.GetExtension(file).ToUpper();
if (extension == ".PNG" || extension == ".TIFF" ||
extension == ".JPG" || extension == ".TIF" ||
extension == ".GIF" || extension == ".JFIF" ||
extension == ".JPE" || extension == ".DIB" ||
extension == ".BMP" || extension == ".JPEG")
{
string filepath = Path.GetFullPath(file);
Image image1 = Image.FromFile(filepath);
string fileName = Path.ChangeExtension(file, ".jpg");
string fileName = Path.ChangeExtension(Path.GetFileName(file), ".jpg");
image1.Save(Path.Combine(directoryPath, fileName), System.Drawing.Imaging.ImageFormat.Jpeg);
image1.Dispose();
}
}
MessageBox.Show("Image Successfully Converted!");
}
[UPDATE3]
I have tested this code now and I have copied 2000 images about 5 to 8 MB each from one directory to another without a problem.
The memory consumption stays stable.
You might want to reconsider if you should "convert" also .jpg and .jpeg or if you should add code to just copy the files if they are of this type.
It will use less CPU and will be faster.
Something like:
else if (extension == ".JPG" || extension == ".JPEG")
{
File.Copy(oldPath, newPath);
}